m (Added duplicate flag) |
JohnBeckett (talk | contribs) m (Reverted edits by 222.247.172.70 (talk | block) to last version by Fritzophrenic) |
||
(8 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
− | {{duplicate|981|369|388|271|660}} |
+ | {{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= |
+ | |created=2005 |
|complexity=basic |
|complexity=basic |
||
|author=JimD |
|author=JimD |
||
|version=5.7 |
|version=5.7 |
||
|rating=28/20 |
|rating=28/20 |
||
⚫ | |||
− | |text= |
||
+ | |category2= |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
+ | Whatever method you use to find the line numbers they can be used as a range for a substitute command: :xx,yy s/^/# / |
||
⚫ | |||
⚫ | |||
⚫ | |||
− | + | 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. |
||
⚫ | |||
⚫ | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
− | |||
⚫ | |||
− | |||
− | |||
⚫ | |||
− | |||
− | |||
− | |||
⚫ | |||
<pre> |
<pre> |
||
− | map gC :'a,. s/^/ */^M:. s/\(.*\)/\1^V^V^M **************\//^M:'a s/\(.*\)/\/**************^V^V^M\1/^M |
+ | map gC :'a,. s/^/ */^M:. s/\(.*\)/\1^V^V^M **************\//^M:'a s/\(.*\)/\/**************^V^V^M\1/^M |
</pre> |
</pre> |
||
⚫ | |||
− | |||
⚫ | |||
<pre> |
<pre> |
||
⚫ | |||
− | |||
+ | * |
||
⚫ | |||
− | + | * .... |
|
− | + | * .... |
|
⚫ | |||
− | * .... |
||
⚫ | |||
</pre> |
</pre> |
||
⚫ | |||
⚫ | |||
− | The example is a little crude --- it doesn't indent the comment block at all, |
||
⚫ | 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). |
||
⚫ | |||
⚫ | |||
⚫ | 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 ( |
||
− | |||
− | |||
− | '''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> |
<pre> |
||
− | filetype on |
+ | filetype on |
− | augroup vimrc_filetype |
+ | augroup vimrc_filetype |
− | autocmd! |
+ | autocmd! |
− | autocmd FileType c call s:MyCSettings() |
+ | autocmd FileType c call s:MyCSettings() |
− | ... |
+ | ... |
− | autocmd FileType vim call s:MyVimSettings() |
+ | autocmd FileType vim call s:MyVimSettings() |
− | augroup end |
+ | augroup end |
− | " Clear all comment markers (one rule for all languages) |
+ | " Clear all comment markers (one rule for all languages) |
− | map _ :s/^\/\/\\|^--\\|^ |
+ | map _ :s/^\/\/\\|^--\\|^> \\|^[#"%!;]//<CR>:nohlsearch<CR> |
− | function! s:MyCSettings() |
+ | function! s:MyCSettings() |
− | + | ... |
|
− | + | " Insert comments markers |
|
− | + | map - :s/^/\/\//<CR>:nohlsearch<CR> |
|
− | endfunction |
+ | endfunction |
− | function! s:MyVimSettings() |
+ | function! s:MyVimSettings() |
− | + | ... |
|
− | + | " Insert comments markers |
|
− | + | map - :s/^/\"/<CR>:nohlsearch<CR> |
|
− | endfunction |
+ | endfunction |
</pre> |
</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. |
− | - 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 " |
+ | 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> |
<pre> |
||
− | vmenu PopUp.Comments.AddSQLComments :s/^/--/ |
+ | vmenu PopUp.Comments.AddSQLComments :s/^/--/<CR> |
− | vmenu PopUp.Comments.RemoveSQLComments :s/^--// |
+ | vmenu PopUp.Comments.RemoveSQLComments :s/^--//<CR> |
− | vmenu PopUp.Comments.AddCStyleComments |
+ | vmenu PopUp.Comments.AddCStyleComments <Esc>`>i*/<Esc>`<i/*<Esc> |
</pre> |
</pre> |
||
− | 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. |
||
− | |||
⚫ | |||
<pre> |
<pre> |
||
− | vmap ,; :s/^/;;;/ |
+ | vmap ,; :s/^/;;;/<CR>:noh<CR> |
− | " visually select block of text to comment, in Lisp |
+ | " visually select block of text to comment, in Lisp |
</pre> |
</pre> |
||
− | Something 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> |
<pre> |
||
− | vmap ,cl :s/^;;;// |
+ | vmap ,cl :s/^;;;//<CR>:noh<CR> |
" uncomment visually selected text |
" uncomment visually selected text |
||
</pre> |
</pre> |
||
− | '''Anonymous''' |
||
− | , October 31, 2005 20:08 |
||
---- |
---- |
||
− | <!-- parsed by vimtips.py in 0.509165 seconds--> |
||
+ | 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: |
||
+ | <pre> |
||
+ | #!/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 |
||
+ | </pre> |
||
+ | |||
+ | [[User:Maedox|Maedox]] 15:18, January 13, 2011 (UTC) |
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.
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)