使用命令行ex编辑器时,如何使用itextESC及匹配模式插入自定义文本?
Hey there! Let's tackle your two ex editor questions step by step—super glad you're using this classic tool for text manipulation!
itextESC in the ex Editor First off, when you mention itextESC, I assume you're referring to entering insert mode, typing custom text, and then exiting back to ex command mode. Here's how to do it both interactively and in scripts:
Interactive Use:
- Navigate to your target line (e.g., with
/patternto search for a matching line). - Type
iand press Enter—this drops you into insert mode. - Type your desired text.
- Press the ESC key to exit insert mode and return to the ex command prompt. You can also type a single
.on a new line to confirm the end of insertion (this is the script-friendly method too).
- Navigate to your target line (e.g., with
Script/Batch Use:
If you're writing an ex script to automate this, structure it like this:/target-pattern/i Your custom text goes here .The lone
.on the final line acts exactly like pressing ESC—it tells ex you're done inserting text and ready for the next command.
//commented Lines When Matching 'abc' To insert text above every //commented line associated with a line containing 'abc', we'll use ex's powerful global command (g) combined with address offsets. Here's the exact command sequence:
g/abc/ +//commented -1i Your custom insertion text here .
Let's break this down so you understand each part:
g/abc/: This tells ex to run the following commands on every line that contains 'abc'.+//commented: Starting from the matched 'abc' line, this searches downward to find the first line that contains//commented.-1i: The-1offsets the found//commentedline by one (moving up one line), anditriggers insert mode at that position.- After typing your custom text, the lone
.ends insert mode and saves the change.
If you need to insert multiple lines of text, just add each line before the .:
g/abc/ +//commented -1i First line of custom text Second line here .
And as you already know, run wq afterward to save your changes and exit!
内容的提问来源于stack exchange,提问作者Inian

