How to paste in the line where the cursor is?
Asked Answered
K

12

65

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?

Krakau answered 21/2, 2012 at 19:17 Comment(1)
Related: How to paste before the cursor after selecting a vertical block?Mishnah
R
53

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.

Reductase answered 21/2, 2012 at 19:38 Comment(5)
Copying a word is fine. But I'm talking about line only. That means a line cannot be copied and pasted at the end of another line in Vim. Ain't it?Krakau
@Krakau -- Read more about linewise and characterwise copying. You can copy a line the way you want if you yank it in a characterwise manner, e.g., 0y$ rather than the linewise yy. p or P will paste into current line or different line depending on whether yank was characterwise (pastes into current line) or linewise (pastes into line above or below).Weisman
If you find yourself commonly wanting to "cast" p/P, you might want to check out UnconditionalPaste by Ingo Karkat. vim.org/scripts/script.php?script_id=3355Myriapod
I often want to copy a line (dd or yy) and treat it as char orientated. Perhaps removing the "\n" would work...not sure how to do that while it's in the buffer. Perhaps a script would help.Copeck
vp is the answer... https://mcmap.net/q/299254/-how-to-paste-in-the-line-where-the-cursor-isKenna
S
26

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
Sell answered 21/2, 2012 at 19:36 Comment(0)
O
11

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.

Omura answered 21/2, 2012 at 20:55 Comment(1)
"* is not the unnamed register. "" is the unnamed register. See :h quotequote. I imagine you have changed 'clipboard' to include unnamed.Myriapod
M
7

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

Mishnah answered 23/12, 2015 at 14:33 Comment(0)
K
4

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

Kenna answered 16/3, 2022 at 0:53 Comment(0)
E
3

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.

Earing answered 2/9, 2013 at 8:54 Comment(0)
L
3

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.

Lynx answered 14/5, 2014 at 6:23 Comment(0)
A
3

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.

Astro answered 24/4, 2018 at 23:56 Comment(0)
C
1

(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.

Chungchungking answered 10/11, 2017 at 2:13 Comment(1)
I think this is the most effective answer. Thanks. It's now in my .vimrc. I added a space at the end of the command so it makes more sense for me. Surprised this answer has not appeared many times elsewhere. nnoremap <leader>p :let @"=substitute(@", '\n\+$', '', '')<cr>pa <esc>Erhard
U
0

If you want to shift existing contents and you are pasting a full line or more from the buffer...

  • P pastes on the current line and stays on the current line.
  • p pastes on the next line and stays on that next line (aka the first line of pasted content).
  • gP pastes starting on the current line and moves to the line after the paste.
  • gp pastes starting on the next line and moves to the line after the paste.

To overwrite the current line, refer to other answers (usually something like R, Ctrlr, ")

Upbear answered 25/4, 2024 at 10:1 Comment(0)
M
-1

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

Methedrine answered 21/2, 2012 at 19:26 Comment(1)
You are right. I thought there's something that I don't know.Krakau
H
-1
  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 (Delete newline in Vim)

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

Hebetude answered 24/1, 2018 at 6:50 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.