How to paste in a new line with vim?

VimEditor

Vim Problem Overview


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?

Vim Solutions


Solution 1 - Vim

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 - Vim

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 op :nmap op

  3. The function version of the mapping (unnecessary really, but just for completeness):

    :nmap :call append(line('.'), @")

    " This one may be a little better (strip the ending new-line before pasting) :nmap :call append(line('.'), substitute(@", '\n$', '', ''))

    :help let-register :help :call :help append() :help line() :help nmap

Solution 3 - Vim

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>"

Solution 4 - Vim

This still uses three keystrokes, but I find it easier than Esc:

o<Alt-p>

Since you're in insert mode after hitting o, the Alt modifier will allow you to use a command as if you weren't.

Solution 5 - Vim

Using this plugin: https://github.com/tpope/vim-unimpaired

]p pastes on the line below

[p pastes on the line above

advantages:

  • works on all yanked text (word, line, character, etc)
  • indents the pasted text to match the indentation of the text around it
  • 2 keystrokes instead of 3 and much "easier" strokes
  • fast

Solution 6 - Vim

Personally I've nmapped Enter (CR) like this:

nmap <CR> o<Esc>k

...based on this Vim Wikia article.

This way I can make newlines directly from normal mode, and combining this with wanting to paste to a newline below I'd do:

<CR>jp

You could also skip k in the nmap above, depending on what functionality you prefer from Enter, so it would just be <CR>p.

I've also imapped jj to Esc, which would also assist in this case. Esc is way too far away from the home row for how significant it is in vim.

Not shorter than the other solutions, but I do think it feels less clunky than some of them, and it has other uses too.

Solution 7 - Vim

If you wanted to stay in the insert mode, you can do o ctrl+o p

  • o – insert mode and go to the new line
  • ctrl+o – run a single command like in normal mode
  • p – paste

It's three keystrokes but you stay in insert mode and also o ctrl+o is quite fast so I personally treat it as 2.5 keystrokes.

Solution 8 - Vim

If you're copying a whole line then pasting a whole line, use Y to yank the line or lines, including line break, in the first place, and p to paste. You can also use V, which is visual line mode, in contrast with plain v for visual mode.

Solution 9 - Vim

I have mapping inoremap jj <ESC>. So it is easy to insert new line with ojj and Ojj and then p.

so ojjp paste new a newline. it have one more stroke then o<esc>p but ojjp is easy for me.

Solution 10 - Vim

I found an elegant solution to this. If you are putting the yank register in your OS's clipboard (which is great anyway), with

set clipboard+=unnamed

than you can do o<Ctl-v>.

Besides being fewer strokes, this improves on both o<Esc>p and :pu because it preserves indenting: both of the other options start you at character zero on the new line.

Caveat is that this may or may not be OS dependent. All I know is that it works on recent version of OS X, but clipboard is just one of many ways to get yank in the OS clipboard.

Solution 11 - Vim

If you want to paste in a new line and still keep indentation, create this mapping:

nnoremap <leader>p oq<BS><Esc>p

Prerequisite: you have leader mapped and you have set autoindent in your .vimrc.

Explanation: a new line is created with 'o', 'q' is typed and then back-spaced on (to keep indentation), and 'esc' brings you back to normal mode where you finally paste.

Solution 12 - Vim

If you also want to end in insert mode, it is possible to paste while in insert mode using CTRL-R ". https://stackoverflow.com/a/2861909/461834

Still three keystrokes, but no escape, and you save a keystroke if you want to end in insert anyway.

Solution 13 - Vim

I use the following mapping in my Neovim config:

nnoremap <leader>p m`o<ESC>p``
nnoremap <leader>P m`O<ESC>p``

A little explanation:

  • m`: set a mark in the current cursor position.
  • o<Esc>p: create a new line below and paste the text in this line
  • O<Esc>P: create a new line above and paste the text in this line
  • ``: put the cursor in the original position

See :h mark for more information about marks in Vim.

Solution 14 - Vim

This solution only seems to apply when the block of copied text starts on a new line (as opposed to grabbing a snippet of text somewhere within a line), but you can always start your copy on the last character you want to grab, then navigate to the last character at the end of line prior to the start of your desired copy block. Then when you want to paste it, place the cursor at the end of the line under which you want your text to be pasted and hit p. If I haven't screwed up the explanation, this should provide the effect you're looking for.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionstatic_rttiView Question on Stackoverflow
Solution 1 - VimBombeView Answer on Stackoverflow
Solution 2 - VimDrAlView Answer on Stackoverflow
Solution 3 - VimsoulmergeView Answer on Stackoverflow
Solution 4 - VimNickView Answer on Stackoverflow
Solution 5 - VimcrogersView Answer on Stackoverflow
Solution 6 - VimmiyalysView Answer on Stackoverflow
Solution 7 - VimreconnView Answer on Stackoverflow
Solution 8 - VimPeterView Answer on Stackoverflow
Solution 9 - VimNatwar SinghView Answer on Stackoverflow
Solution 10 - VimenfascinationView Answer on Stackoverflow
Solution 11 - VimJustin WongView Answer on Stackoverflow
Solution 12 - VimjtpereydaView Answer on Stackoverflow
Solution 13 - VimjdhaoView Answer on Stackoverflow
Solution 14 - VimunbrokenrabbitView Answer on Stackoverflow