abstract
| - It's very common to need to comment out a range of lines, let's say in a shell script. One way to do this is to move to the first line and type: I# (insert # as the first non-blank character of the line) Then use a series of: j. (moving down and repeat the previous command/change) However, this is tedious; using j.j.j. ... Another way is to determine the line numbers of the first and last line. You can use the :f ex command to display these; or you can temporarily use :set nu to enable "number mode" and :set nonu to disable when you're done. Whatever method you use to find the line numbers they can be used as a range for a substitute command: :xx,yy s/^/# / This is a bit tedious because you have to look up the numbers and re-type them. So we want a method that is less repetitive than j.j.j. and involves less manual fussing with line numbers than the ex range/substitute command. The solution is as old as vi/ex itself though it's often overlooked. Move to the first line and set a mark with: ma (where "a" is any letter you choose) Move to the last line and issue the following ex command: :'a,. s/^/# / This works on the range of lines between the mark (single quote, register letter) and the current line (.) substituting each beginning of line with the string "# " (octothorpe, space). More elaborate sequences of pure old ex commands can also be used to create boxes around C/C++ blocks; but they are really horrid to type every time so they have to be mapped to some key sequence and entails consistently using the same register every time. For example: map gC :'a,. s/^/ */^M:. s/\(.*\)/\1^V^V^M **************\//^M:'a s/\(.*\)/\/**************^V^V^M\1/^M maps the sequence gC to a macro which wraps a block of text, from the current line back to the line marked by the "a" in a C style comment like: /************************ * * .... * .... ************************/ The example is a little crude - it doesn't indent the comment block at all, for example; and it could use some extra blank lines. However, it illustrates the point of what we can do even with plain old vi/ex commands.
|