Wednesday, April 3, 2013

Pasting in Vim visual block mode

Say you have a section of text and want to paste something at the start of every line. How do you do that easily in vim?
text line 1
text line 2
text line 3
This blog post led me to the answer, I've added this to my vimrc:
" Use CTRL-V for pasting yank contents in insert mode.  Also useful in visual
" block mode for pasting a line of text in front of all other lines
" http://briancarper.net/blog/341/
inoremap <C-v> <C-r>" <Esc>
So I go:
CTRL-V   (visual block mode)
jj       (select first column of lines)
I        (insert mode)
CTRL-V   (paste yanked content - unnamed register)
To get:
this is new. text line 1
this is new. text line 2
this is new. text line 3
You might want to pick a different mapping if you use the existing CTRL-V functionality to insert funky characters. See the vim help (:h i_CTRL-V)

No comments: