Vim Tips Wiki
(Marked as a duplicate tip ; Category set to ATI)
m (Added duplicate flag)
Line 1: Line 1:
{{duplicate|369|388|271}}
+
{{duplicate|981|369|388|271|660}}
 
{{review}}
 
{{review}}
 
{{Tip
 
{{Tip
Line 17: Line 17:
   
 
Then use a series of: j. (moving down and repeat the previous command/change)
 
Then use a series of: j. (moving down and repeat the previous command/change)
 
 
   
 
However, this is tedious; using j.j.j. ...
 
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.
 
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/^/# /
 
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.
 
This is a bit tedious because you have to look up the numbers and re-type them.
 
 
   
 
So we want a method that is 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.
 
So we want a method that is 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.
 
   
   
Line 43: Line 33:
   
 
Move to the last line and issue the following ex command: :'a,. s/^/# /
 
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).
 
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).
 
   
   
Line 54: Line 42:
   
   
  +
For example:
For example: map gC :'a,. s/^/ */^M:. s/\(.*\)/\1^V^V^M **************\//^M:'a s/\(.*\)/\/**************^V^V^M\1/^M
 
  +
<pre>
 
 
map gC :'a,. s/^/ */^M:. s/\(.*\)/\1^V^V^M **************\//^M:'a s/\(.*\)/\/**************^V^V^M\1/^M
  +
</pre>
   
   
 
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:
 
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:
   
  +
<pre>
 
   
 
/************************
 
/************************
 
 
*
 
*
 
 
* ....
 
* ....
 
 
* ....
 
* ....
 
 
************************/
 
************************/
  +
</pre>
 
   
   
 
The example is a little crude --- it doesn't indent the comment block at all,
 
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.
 
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.
 
 
 
   
 
}}
 
}}
Line 93: Line 75:
 
----
 
----
 
I use a autocommand to load the appropriate mapping for each language to the same key, like this:
 
I use a autocommand to load the appropriate mapping for each language to the same key, like this:
  +
<pre>
 
 
filetype on
 
filetype on
 
augroup vimrc_filetype
 
augroup vimrc_filetype
Line 116: Line 98:
 
map - :s/^/\"/&lt;CR&gt;:nohlsearch&lt;CR&gt;
 
map - :s/^/\"/&lt;CR&gt;:nohlsearch&lt;CR&gt;
 
endfunction
 
endfunction
  +
</pre>
   
 
So then I can press the '-' key to comment a line and the '_' key to uncomment it.
 
So then I can press the '-' key to comment a line and the '_' key to uncomment it.
Line 142: Line 125:
 
----
 
----
 
I have added the following lines to my vimrc. I have to select a block of text and right-click and then click on the appropriate sub-menu item.
 
I have added the following lines to my vimrc. I have to select a block of text and right-click and then click on the appropriate sub-menu item.
  +
<pre>
 
 
vmenu PopUp.Comments.AddSQLComments :s/^/--/&lt;CR&gt;
 
vmenu PopUp.Comments.AddSQLComments :s/^/--/&lt;CR&gt;
 
vmenu PopUp.Comments.RemoveSQLComments :s/^--//&lt;CR&gt;
 
vmenu PopUp.Comments.RemoveSQLComments :s/^--//&lt;CR&gt;
 
vmenu PopUp.Comments.AddCStyleComments &lt;ESC&gt;`&gt;i*/&lt;ESC&gt;`&lt;i/*&lt;ESC&gt;
 
vmenu PopUp.Comments.AddCStyleComments &lt;ESC&gt;`&gt;i*/&lt;ESC&gt;`&lt;i/*&lt;ESC&gt;
  +
</pre>
 
 
I am still to add the facility to remove C style comments but it should be doable.
 
I am still to add the facility to remove C style comments but it should be doable.
 
Anyone taking up the gauntlet?
 
Anyone taking up the gauntlet?
 
Ideally it should search outwards from the selected area and remove the comments from the first /* ... */ block which surrounds the selected area
 
Ideally it should search outwards from the selected area and remove the comments from the first /* ... */ block which surrounds the selected area
 
   
 
'''Anonymous'''
 
'''Anonymous'''
Line 158: Line 140:
   
 
However I use something like this in my 'rc file.
 
However I use something like this in my 'rc file.
  +
<pre>
 
vmap ,; :s/^/;;;/&lt;CR&gt;:noh&lt;CR&gt;
 
vmap ,; :s/^/;;;/&lt;CR&gt;:noh&lt;CR&gt;
 
" visually select block of text to comment, in Lisp
 
" visually select block of text to comment, in Lisp
  +
</pre>
   
Somthing like this works great for just selecting a bunch of text or even in lisp a block of code to comment out.
+
Something like this works great for just selecting a bunch of text or even in lisp a block of code to comment out.
   
 
And the reverse use something like:
 
And the reverse use something like:
  +
<pre>
 
vmap ,cl :s/^;;;//&lt;CR&gt;:noh&lt;CR&gt;
 
vmap ,cl :s/^;;;//&lt;CR&gt;:noh&lt;CR&gt;
 
" uncomment visually selected text
 
" uncomment visually selected text
  +
</pre>
   
 
'''Anonymous'''
 
'''Anonymous'''

Revision as of 20:26, 15 September 2007

Duplicate tip

This tip is very similar to the following:

These tips need to be merged – see the merge guidelines.

Previous TipNext Tip

Tip: #981 - Commenting out a range of lines

Created: September 2, 2005 4:09 Complexity: basic Author: JimD Version: 5.7 Karma: 28/20 Imported from: Tip#981

Here's a general tip that works for any version of vi, not just vim.


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 #<space> 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 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.

Comments

Yes, this was part of my original vi training, but I hadn't quite mastered it until setting marks had become a part of my repertoire. But once I was comfortable with setting marks, fixing indenting in Vim was a matter of 'a='b (for marks a and b). (Of course, this depends on having the correct a syntax file set, but this is usually automatically set.)

However, for short ranges of basic prefixing comments (# or //), I've come to be very fond of visual block mode and block insert. To do this, start visual block mode (usually with Ctrl-V), and adjust the selection with movement keys as needed. (Again, marks can be used.) Once the selections are made, start block insert with 'I' (or append with 'A'), and type the comment text. When you hit escape, comments will be added to all affect lines. Also, the visual selection can be recalled with gv. The substitute can also be used with a visual selection since the visual selection creates marks of its own (though I use this least often).


Anonymous , September 2, 2005 7:31


I use a autocommand to load the appropriate mapping for each language to the same key, like this:

filetype on 
augroup vimrc_filetype 
 autocmd! 
 autocmd FileType c call s:MyCSettings() 
 ... 
 autocmd FileType vim call s:MyVimSettings() 
augroup end 

" Clear all comment markers (one rule for all languages) 
map _ :s/^\/\/\\|^--\\|^> \\|^[#"%!;]//<CR>:nohlsearch<CR> 

function! s:MyCSettings() 
 ... 
 " Insert comments markers 
 map - :s/^/\/\//<CR>:nohlsearch<CR> 
endfunction 

function! s:MyVimSettings() 
 ... 
 " Insert comments markers 
 map - :s/^/\"/<CR>:nohlsearch<CR> 
endfunction 

So then I can press the '-' key to comment a line and the '_' key to uncomment it.

- Dave Vehrs

dvehrs--AT--gmail.com , September 3, 2005 12:24


You can consider using the boxes utility from http://boxes.thomasjensen.com/

The idea is to filter a number of lines through the boxes command, and it will take care of commenting the lines in or out, depending on its arguments. Especially useful in combination with autocommands, but see boxes' Documentation under the Vim integration chapter.

Advantage: simplicity. Disadvantage: it's an external tool.

david dot-here dereu at advalvas dot-here be , September 5, 2005 2:52


A -VISUAL BLOCK- can also be used to comment out lines. Start by typing "Ctrl-V" and then highlight a strip downwards by hitting "down/j". After that, type capital "I" to insert for all highlighted lines. Type the comment, in this case "#<space>". Then hit "Esc".

Works with vim.

Gerald Lai , September 21, 2005 15:26


I have added the following lines to my vimrc. I have to select a block of text and right-click and then click on the appropriate sub-menu item.

vmenu PopUp.Comments.AddSQLComments :s/^/--/<CR> 
vmenu PopUp.Comments.RemoveSQLComments :s/^--//<CR> 
vmenu PopUp.Comments.AddCStyleComments <ESC>`>i*/<ESC>`<i/*<ESC> 

I am still to add the facility to remove C style comments but it should be doable. Anyone taking up the gauntlet? Ideally it should search outwards from the selected area and remove the comments from the first /* ... */ block which surrounds the selected area

Anonymous , October 6, 2005 21:35


I really like dvehrs sugestion.

However I use something like this in my 'rc file.

vmap ,; :s/^/;;;/<CR>:noh<CR> 
" visually select block of text to comment, in Lisp 

Something like this works great for just selecting a bunch of text or even in lisp a block of code to comment out.

And the reverse use something like:

vmap ,cl :s/^;;;//<CR>:noh<CR> 
" uncomment visually selected text

Anonymous , October 31, 2005 20:08