How to paste in a new line with vim?
I often have to paste some stuff on a new line in vim. What I usually do is:
o<Esc>p
Which inserts a new line and puts me in insertion mode, than quits insertion mode, and finally pastes.
Three keystrokes. Not very efficient. Any better ideas?
Solution 1:
Shortly after :help p
it says:
:[line]pu[t] [x] Put the text [from register x] after [line] (default
current line). This always works |linewise|, thus
this command can be used to put a yanked block as
new lines.
:[line]pu[t]! [x] Put the text [from register x] before [line]
(default current line).
Unfortunately it’s not shorter than your current solution unless you combined it with some keyboard map as suggested in a different answer. For instance, you can map it to any key (even p
):
:nmap p :pu<CR>
Solution 2:
Options:
1) Use yy
to yank the whole line (including the end of line character). p
will then paste the line on a new line after the current one and P
(Shift-P) will paste above the current line.
2) Make a mapping: then it's only one or two keys:
:nmap ,p o<ESC>p
:nmap <F4> o<ESC>p
3) The function version of the mapping (unnecessary really, but just for completeness):
:nmap <F4> :call append(line('.'), @")<CR>
" This one may be a little better (strip the ending new-line before pasting)
:nmap <F4> :call append(line('.'), substitute(@", '\n$', '', ''))<CR>
:help let-register
:help :call
:help append()
:help line()
:help nmap
Solution 3:
You can paste a buffer in insert mode using <C-R>
followed by the name of the buffer to paste. The default buffer is "
, so you would do
o<C-R>"
I found that I use <C-R>"
very often and bound that to <C-F>
in my vimrc:
inoremap <C-F> <C-R>"