Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to paste in a new line with vim?

Tags:

vim

editor

People also ask

How do I move text to the next line in Vim?

I know that pressing 'o' in normal or visual mode moves the cursor to a new line and switches the mode to insert.

How do I paste at the end of a line in Vim?

Therefore, the key sequence would from command mode, A to get into insert mode at end of line, <space> to insert a space, then Ctrl-v to paste. Alternatively, while in insert mode, use the mouse to put the cursor at the end of a line, <space> then Ctrl-v .

How do I copy and paste an entire line in vi editor?

Directions: Press the ESC key to be sure you are in vi Command mode. Place the cursor on the line you wish to copy. Type yy to copy the line.


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>

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

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

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.


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