Vim Tips Wiki
(Marked as a duplicate tip ; Category set to ATI)
m (Reverted edits by 222.247.172.70 (talk | block) to last version by Fritzophrenic)
(9 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{duplicate|369|388|271}}
+
{{duplicate|981|369|388|271|660}}
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=981
 
|id=981
  +
|previous=979
|title=Commenting out a range of lines
 
  +
|next=982
|created=September 2, 2005 4:09
+
|created=2005
 
|complexity=basic
 
|complexity=basic
 
|author=JimD
 
|author=JimD
 
|version=5.7
 
|version=5.7
 
|rating=28/20
 
|rating=28/20
 
|category1=Automated Text Insertion
|text=
 
  +
|category2=
Here's a general tip that works for any version of vi, not just vim.
 
 
}}
 
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. ...
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&#35; (insert &#35;&lt;space&gt; as the first non-blank character of the line)
 
   
 
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.
Then use a series of: j. (moving down and repeat the previous command/change)
 
   
 
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.
However, this is tedious; using j.j.j. ...
 
   
 
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).
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.
 
   
 
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:
  +
<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:
Whatever method you use to find the line numbers they can be used as a range for a substitute command: :xx,yy s/^/&#35; /
 
   
  +
<pre>
 
/************************
  +
*
 
* ....
 
* ....
 
************************/
  +
</pre>
   
 
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==
This is a bit tedious because you have to look up the numbers and re-type them.
 
 
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).
   
 
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/^/&#35; /
 
 
 
 
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 "&#35; " (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 (&#35; 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:
+
I use a autocommand to load the appropriate mapping for each language to the same key, like this:
  +
<pre>
 
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)
filetype on
 
 
map _ :s/^\/\/\\|^--\\|^> \\|^[#"%!;]//<CR>:nohlsearch<CR>
augroup vimrc_filetype
 
autocmd!
 
autocmd FileType c call s:MyCSettings()
 
...
 
autocmd FileType vim call s:MyVimSettings()
 
augroup end
 
   
 
function! s:MyCSettings()
" Clear all comment markers (one rule for all languages)
 
 
...
map _ :s/^\/\/\\|^--\\|^&gt; \\|^[&#35;"%!;]//&lt;CR&gt;:nohlsearch&lt;CR&gt;
 
 
" Insert comments markers
 
map - :s/^/\/\//<CR>:nohlsearch<CR>
 
endfunction
   
function! s:MyCSettings()
+
function! s:MyVimSettings()
...
+
...
" Insert comments markers
+
" Insert comments markers
map - :s/^/\/\//&lt;CR&gt;:nohlsearch&lt;CR&gt;
+
map - :s/^/\"/<CR>:nohlsearch<CR>
endfunction
+
endfunction
  +
</pre>
   
 
So then I can press the '-' key to comment a line and the '_' key to uncomment it.
function! s:MyVimSettings()
 
...
 
" Insert comments markers
 
map - :s/^/\"/&lt;CR&gt;:nohlsearch&lt;CR&gt;
 
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/
 
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.
+
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.
+
Advantage: simplicity.<br>
 
Disadvantage: it's an external tool.
 
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 "&#35;&lt;space&gt;". Then hit "Esc".
+
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.
 
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.
+
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/^/--/<CR>
 
vmenu PopUp.Comments.RemoveSQLComments :s/^--//<CR>
 
vmenu PopUp.Comments.AddCStyleComments <Esc>`>i*/<Esc>`<i/*<Esc>
  +
</pre>
   
 
----
vmenu PopUp.Comments.AddSQLComments :s/^/--/&lt;CR&gt;
 
 
I use something like this in my 'rc file.
vmenu PopUp.Comments.RemoveSQLComments :s/^--//&lt;CR&gt;
 
  +
<pre>
vmenu PopUp.Comments.AddCStyleComments &lt;ESC&gt;`&gt;i*/&lt;ESC&gt;`&lt;i/*&lt;ESC&gt;
 
 
vmap ,; :s/^/;;;/<CR>:noh<CR>
 
" visually select block of text to comment, in Lisp
  +
</pre>
   
 
Something like this works great for just selecting a bunch of text or even in lisp a block of code to comment out.
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
 
   
 
And the reverse use something like:
  +
<pre>
 
vmap ,cl :s/^;;;//<CR>:noh<CR>
 
" uncomment visually selected text
  +
</pre>
   
'''Anonymous'''
 
, October 6, 2005 21:35
 
 
----
 
----
I really like dvehrs sugestion.
 
   
  +
Not really a VIM tip, but I find here-documents very useful in bash for commenting out large chunks of code. It's also easier to delete than if you insert comments (e.g. #) because you only have to delete two lines instead of a # on every line.
However I use something like this in my 'rc file.
 
  +
In my example I redirect to : which means nothing is printed or executed. You can redirect to anything that supports reading stdin. The '<<-' is the redirect, and '-' makes it ignore tabs or spaces at the beginning of lines. ': <<- COMMENT' redirects everything from the next line up to 'COMMENT' on the last line. The keyword can be almost anything.
vmap ,; :s/^/;;;/&lt;CR&gt;:noh&lt;CR&gt;
 
" visually select block of text to comment, in Lisp
 
   
  +
Example bash code:
Somthing like this works great for just selecting a bunch of text or even in lisp a block of code to comment out.
 
  +
<pre>
  +
#!/bin/bash
  +
echo "this will be echoed"
   
  +
: <<- COMMENT
And the reverse use something like:
 
  +
echo "this will not be echoed"
vmap ,cl :s/^;;;//&lt;CR&gt;:noh&lt;CR&gt;
 
  +
echo "neither will this"
" uncomment visually selected text
 
  +
ls -d .[a-z]*
 
  +
# You can even insert tabs and comments.
'''Anonymous'''
 
  +
Or just write whatever
, October 31, 2005 20:08
 
  +
COMMENT
----
 
  +
</pre>
<!-- parsed by vimtips.py in 0.509165 seconds-->
 
   
  +
[[User:Maedox|Maedox]] 15:18, January 13, 2011 (UTC)
[[Category:Automated Text Insertion]]
 

Revision as of 11:30, 10 May 2012

Duplicate tip

This tip is very similar to the following:

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

Tip 981 Printable Monobook Previous Next

created 2005 · complexity basic · author JimD · version 5.7


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).


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.


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.


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.


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 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

Not really a VIM tip, but I find here-documents very useful in bash for commenting out large chunks of code. It's also easier to delete than if you insert comments (e.g. #) because you only have to delete two lines instead of a # on every line. In my example I redirect to : which means nothing is printed or executed. You can redirect to anything that supports reading stdin. The '<<-' is the redirect, and '-' makes it ignore tabs or spaces at the beginning of lines. ': <<- COMMENT' redirects everything from the next line up to 'COMMENT' on the last line. The keyword can be almost anything.

Example bash code:

#!/bin/bash
echo "this will be echoed"

: <<- COMMENT
	echo "this will not be echoed"
	echo "neither will this"
	ls -d .[a-z]*
	# You can even insert tabs and comments.
	Or just write whatever
COMMENT

Maedox 15:18, January 13, 2011 (UTC)