views:

7832

answers:

10

In vim, how would I go about inserting characters at the beginning of each line in a selection? For instance, let's say I want to comment out a block of code by prepending '//' at the beginning of each line (assuming my language's comment system doesn't allow block commenting like /* */). How would I do this?

+21  A: 

This replaces the beginning of each line with "//":

:%s!^!//!

This replaces the beginning of each selected line (use visual mode to select) with "//":

:'<,'>s!^!//!
Tomalak
Thanks! Makes total sense. And removing the text goes as follows: '<,'>s!^//!!
j0rd4n
That's it, exactly. :-)
Tomalak
+21  A: 
  • Use Ctrl+v to select the first column of text in the lines you want to comment.
  • Then hit 'I' and type the text you want to insert.
  • Then hit 'Esc', wait 1 second and the inserted text will appear on every line.
pixelbeat
The only bummer with this is that it appears Ctrl+V is overridden in GVIM.
j0rd4n
Not for me (on linux) it's not
pixelbeat
You can use Ctrl-Q as a replacement in gVim (as :help Ctrl-V explains) but you need to use hjkl to navigate in this mode rather than the arrow keys
Gareth
Sweet! Works like a champ! Thanks guys!
j0rd4n
To be able to use the arrow keys in visual mode on windows just add "set keymodel-=stopsel" to your _vimrc. More at http://vim.wikia.com/wiki/Make_arrow_keys_work_in_visual_mode_under_Windows
agnul
If your ctrl-v is overridden in windows gvim, you should edit the global vimrc to stop including mswin.vim.
graywh
+4  A: 

If you want to get super fancy about it, put this in your .vimrc:

vmap \c :s!^!//!<CR>
vmap \u :s!^//!!<CR>

Then, whenever in visual mode, you can hit \c to comment the block and \u to uncomment it. Of course, you can change those shortcut keystrokes to whatever.

Lucas Oman
+3  A: 

Yet another way:

:'<,'>g/^/norm I//

/^/ is just a dummy pattern to match every line. norm lets you run the normal-mode commands that follow. I// says to enter insert-mode while jumping the cursor to the beginning of the line, then insert the following text (two slashes).

:g is often handy for doing something complex on multiple lines, where you may want to jump between multiple modes, delete or add lines, move the cursor around, run a bunch of macros, etc. And you can tell it to operate only on lines that match a pattern.

Brian Carper
actually the :g command isn't necessary. This will do: :'<,'>norm I//
Leonardo Constantino
And :s is better to use in this case, too.
graywh
+2  A: 

I can recommend the EnhCommentify plugin.

eg. put this to your vimrc:

let maplocalleader=','
vmap <silent> <LocalLeader>c <Plug>VisualTraditional
nmap <silent> <LocalLeader>c <Plug>Traditional
let g:EnhCommentifyBindInInsert = 'No'
let g:EnhCommentifyMultiPartBlocks = 'Yes'
let g:EnhCommentifyPretty = 'Yes'
let g:EnhCommentifyRespectIndent = 'Yes'
let g:EnhCommentifyUseBlockIndent = 'Yes'

you can then comment/uncomment the (selected) lines with ',c'

bene
You can also use Nerd Commenter at http://www.vim.org/scripts/script.php?script_id=1218
Nathan Fellman
+7  A: 

And yet another way:

  • Move to the beginning of a line
  • enter Visual Block mode (CTRL-v)
  • select the lines you want (moving up/down with j/k, or jumping to a line with [line]G)
  • press I (that's capital i)
  • type the comment character(s)
  • press ESC
Steve K
+2  A: 

another way that might be easier for newcomers:

 some█
 code
 here

place the cursor on the first line (as I have attempted to depict above) and type the following (there is a space at the end):

I//

 // █some
 code
 here

press escape and use the digraph:

j.j.

 // some
 // code
 //█here

j is a motion command and . repeats the last editing command you made (in this case I// Much easier although not as cool as the other solutions here.)

thinkhard
A: 

For commenting blocks of code, I like the NERD Commenter plugin.

Select some text:

Shift-V
...select the lines of text you want to comment....

Comment:

,cc

Uncomment:

,cu

Or just toggle the comment state of a line or block:

,c<space>
Kevin
+1  A: 

:%s/^/#/

Adds # at the beginning of every line.

And people will stop bitching about your lack of properly commenting scripts

Jar-jarhead
+1  A: 

The general pattern for search and replace is:

:s/search/replace/

Replaces 'search' with 'replace' for current line

This command will replace each occurrence of 'search' with 'replace' for the current line only. The % is used to search over the whole file. To confirm each replacement interactively append a 'c' for confirm:

:%s/search/replace/c

Interactive confirm replacing 'search' with 'replace' for the entire file

Instead of the % character you can use a line number range (note that the '^' character is a special search character for the start of line):

:14,20s/^/#/

Inserts a '#' character at the start of lines 14-20

If you want to use another comment character (like //) then change your command delimiter:

:14,20s!^!//!

Inserts a '//' character sequence at the start of lines 14-20

Or you can always just escape the // characters like:

:14,20s/^/\/\//

Inserts a '//' character sequence at the start of lines 14-20

cyber-monk