How to paste in the line where the cursor is?

VimKeyboard ShortcutsCopy Paste

Vim Problem Overview


The command p pastes below the cursor and P pastes above the cursor. What's the command to paste in the line where cursor is?

Vim Solutions


Solution 1 - Vim

This all depends on the type of data in the register you're pasting. If the data is line-oriented data (yanked with yy for instance) it will be pasted as a whole line above or below the cursor. If the data is character-oriented (yanked with e.g. y2w) then it will be pasted at or before the cursor position in the current line.

See :help linewise-register for more info on the interaction between the type of register and the put command.

Solution 2 - Vim

The Edit menu in gvim lists the following:

  • Paste = "+gP

  • Put Before = [p

  • Put After = ]p

If you're running vim in Windows, you can do the following to get Ctrl+C and Ctrl+V to work as expected:

source $VIMRUNTIME/mswin.vim
behave mswin

Solution 3 - Vim

If you want to keep the current line as it is, you either paste above or below the line.

If you want to overwrite the current line you'll have to delete it first, which means that the following line takes its place, then paste above the new current line.

There are more than one way to do it:

  • "_ddP

    1. "_dd deletes the whole current line in the "black hole register", the following line is now the current line.

    2. P puts the content of the unnamed register above the current line.

  • Vp

    1. V puts you in VISUAL LINE mode and selects visually the whole current line

    2. p replaces the selection with the content of the unnamed register

  • S<C-r>"

    1. S deletes the content of the current line and puts you in INSERT mode

    2. <C-r>" puts the content of the unnamed register

The two last options have an interesting side effect: the content of the previous line is put into the unnamed register which makes it impossible to do multiple pastes with the same content.

Luckily, you can work around this situation:

  • The "black hole register", mentioned in the first solution works, well… like a black hole. Whatever you put into it disappears forever so you can continue using p and P with some degree of confidence that the unnamed register is still the same from paste to paste.

  • Vim gives you access to 26 alphabetic registers that you can use to save macros or… paste stuff repeatedly.

    Taking the second solution as a starting point, you start by yanking a whole line into register "a with "ayy then you do V"ap on another line.

But all of the above assumes that the text you want to paste is an actual line. Vim makes the difference between "line-wise" and "character-wise" : it won't let you paste a line in a character-wise context or the other way around.

Yanking a whole line with yy keeps its line-wiseness or character-wiseness and you won't be able to p between two characters on a same line. For that you need to make sure that what you yank won't be interpreted as line-wise by Vim. For example, let's assume you are on the first character of the first line and want to yank ipsum dolor and put it at its normal place between lorem and sit:

ipsum dolor
lorem  sit amet

You should type "ayee to put your yanked text in register "a, place the cursor where needed and type "aP.

Solution 4 - Vim

To paste in insert mode you just press Control+R. Then enter the register, e.g. Shift++.

To paste in command mode, you press P, however you've to make sure your line doesn't have a new line character (e.g. yanked by 0v$hy), otherwise it'll appear above the cursor.

The same for visual mode, see: How to paste a line in a vertical selection block? at Vim SE

Solution 5 - Vim

You can use D to delete from current cursor position to the end of line, and the p to the new cursor position.

That is to cut and paste a whole line use ^D and p.

Solution 6 - Vim

Shift + v will select the entire line, but you don't want to do that. Instead, press CTRL + v from the beginning of the line which will select by character, then $ to select to the end of the line. yank y and paste p.

Solution 7 - Vim

I needed to "cast" register contents into a certain (characterwise / linewise / blockwise) mode so often, I wrote the UnconditionalPaste plugin for it. It provides gcp, glp, etc. alternatives to the built-in paste commands that force a certain mode (and by now several more variations on this theme, like pasting with joined by commas or queried characters).

With it, you can just use gcp / gcP to paste after / before the cursor position, regardless of how you've yanked the text.

Solution 8 - Vim

  1. divide the line into 2 wherever you want to insert

  2. paste the section between them

  3. merge the 3 lines with j as described here (https://stackoverflow.com/questions/3983406/delete-newline-in-vim)

works, but tedious, and had to think about, look it up => vi and emacs are garbage software

Solution 9 - Vim

(I know this thread is old, just leaving this and hope this may help someone)

Inspired by @wbg's comment above regarding deleting the line feed, I added these to my mappings:

nnoremap <leader>p :let @"=substitute(@", '\n\+$', '', '')<CR>p

inoremap <leader>p <esc>:let @"=substitute(@", '\n\+$', '', '')<CR>pa

This is useful when I have a file with some SQLs (line by line) and I have to yank into the code.

Solution 10 - Vim

vp that's how, type vp in normal mode


How is this not an answer to a 10 year old question, stackoverflow slip'n?

The character under the cursor will be replaced by the the put

To help with and many others scenarios, I've mapped rr for the ability to quickly add a space, or some other 1-off character.

noremap rr i<space><esc>r in my ~/.vimrc

Solution 11 - Vim

I'm not sure there is one. I tried to find documentation and ran across the following three documents:

Unfortunately, all three only have the two commands that you list. Particularly, the third link states that The commands to paste are p and P...

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
QuestionKitchaView Question on Stackoverflow
Solution 1 - VimRandy MorrisView Answer on Stackoverflow
Solution 2 - VimMatthew StrawbridgeView Answer on Stackoverflow
Solution 3 - VimromainlView Answer on Stackoverflow
Solution 4 - VimkenorbView Answer on Stackoverflow
Solution 5 - VimvvvView Answer on Stackoverflow
Solution 6 - VimouterSpaceView Answer on Stackoverflow
Solution 7 - VimIngo KarkatView Answer on Stackoverflow
Solution 8 - VimfoobariciuosView Answer on Stackoverflow
Solution 9 - VimSunny PunView Answer on Stackoverflow
Solution 10 - VimTimothy L.J. StewartView Answer on Stackoverflow
Solution 11 - VimJeremy WigginsView Answer on Stackoverflow