Kitcha
Kitcha

Reputation: 1731

How to paste in the line where the cursor is?

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?

Upvotes: 65

Views: 44960

Answers (12)

Brian Wright
Brian Wright

Reputation: 193

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

Upvotes: 0

Timothy L.J. Stewart
Timothy L.J. Stewart

Reputation: 1754

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

Upvotes: 4

outerSpace
outerSpace

Reputation: 137

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.

Upvotes: 3

kenorb
kenorb

Reputation: 166399

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

Upvotes: 7

foobariciuos
foobariciuos

Reputation: 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

Upvotes: -2

Sunny Pun
Sunny Pun

Reputation: 726

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

Upvotes: 1

Ingo Karkat
Ingo Karkat

Reputation: 172570

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.

Upvotes: 3

vvv
vvv

Reputation: 31

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.

Upvotes: 3

romainl
romainl

Reputation: 196546

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.

Upvotes: 12

Matthew Strawbridge
Matthew Strawbridge

Reputation: 20620

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

Upvotes: 26

Randy Morris
Randy Morris

Reputation: 40927

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.

Upvotes: 53

Jeremy Wiggins
Jeremy Wiggins

Reputation: 7299

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

Upvotes: -1

Related Questions