Reputation: 37174
In Vim, how do I insert characters at the beginning of each line in a selection?
For instance, 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?
Upvotes: 519
Views: 515346
Reputation: 227
Here’s another way, which I use often due to its advantages.
vip
or <shift>+V
;:
to enter command mode;norm gIxxx <enter>
where xxx
is //
or any other characters you prefer.Upvotes: 0
Reputation: 31718
x
to delete)For further information and reading, check out "Inserting text in multiple lines" in the Vim Tips Wiki.
Upvotes: 941
Reputation: 5560
The general pattern for search and replace is:
:s/search/replace/
Replaces the first occurrence of 'search' with 'replace' for current line
:s/search/replace/g
Replaces all occurrences of 'search' with 'replace' for the current line, 'g' is short for 'global'
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 escape the // characters like:
:14,20s/^/\/\//
Inserts a '//' character sequence at the start of lines 14-20
If you are not seeing line numbers in your editor, simply type the following:
:set nu
Upvotes: 103
Reputation: 71
To insert "ABC" at the begining of each line:
Go to command mode
% norm I ABC
Upvotes: 7
Reputation: 386
Mapping of most voted answer:
1st visual select the desired lines, then execute <leader>zzz
, which values:
vnoremap <leader>zzz <C-V>^I-<Space><Esc>
<C-V>
to enter visual mode^
goes to start of line ( or use '0
' to 1st non blank)I
to insert in block mode-<Space>
to insert '- ' (for example, edit as you need)<Esc>
to apply same insert to all visual block linesOr of last visual selection from normal mode:
nnoremap <leader>zzz gv<C-V>^I-<Space><Esc>
Upvotes: 0
Reputation: 1801
Another way that might be easier for newcomers:
some█
code
here
Place the cursor on the first line, e.g. by
gg
and type the following to get into insert mode and add your text:
I / / Space
// █some
code
here
Press Esc to get back to command mode and use the digraph:
j . j .
// some
// code
//█here
j is a motion command to go down one line and . repeats the last editing command you made.
Upvotes: 49
Reputation: 354
In case someone's multi-line-selection is actually a paragraph, there is no need to manually select the lines. vim can do that for you:
vip
: select and mark the whole paragraphshift-i
: insert text at line beginningescape
: leave insert mode/enter normal mode [line beginnings still selected]escape
: unselect line beginningsUpvotes: 1
Reputation: 5347
Mark the area to be comment as a visual block (<C-V
)
and do c#<ESC>p
c
hange it to "#"If you do it often, define a short cut (example \q
) in your .vimrc
:vmap \q c#<ESC>p
Upvotes: 1
Reputation: 338208
This replaces the beginning of each line with "//":
:%s!^!//!
This replaces the beginning of each selected line (use visual mode to select) with "//":
:'<,'>s!^!//!
Note that gv
(in normal mode) restores the last visual selection, this comes in handy from time to time.
Upvotes: 223
Reputation: 289
This adds #
at the beginning of every line:
:%s/^/#/
And people will stop complaining about your lack of properly commenting scripts.
Upvotes: 28
Reputation: 1974
And yet another way:
Upvotes: 29
Reputation: 15872
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.
Upvotes: 16
Reputation: 1260
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>
Upvotes: 3
Reputation: 12866
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'
Upvotes: 2
Reputation: 72926
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.
Upvotes: 14