aehlke
aehlke

Reputation: 15831

Paste in Vim without moving the cursor

Often I need to paste something into several adjacent lines, at the same or similar positions. It's a pain to have to move the cursor back to the beginning of the pasted contents every time, when moving on to the next line. How can I paste (as in, the command 'p') without moving the cursor? Or, how can I quickly get the cursor back to where it was before pasting?

Upvotes: 34

Views: 7837

Answers (6)

Raven
Raven

Reputation: 246

I found this page while looking for a Vim built-in way to paste without moving the cursor.

What I have been doing up until now is these three steps -- which don't require me to remember any new shortcuts:

  1. Paste
  2. Undo
  3. Redo

This leaves your cursor wherever it was before pasting, but is rather annoying needing to hit u Ctrl+R, and it changes the status line to talking about the changes made.

After reading through this page and a couple others, I have updated my ~/.vimrc to include the following:

" Use Shift+P to paste text and leave cursor at the starting position
map P Pg;

This rewrites the Shift+P binding to leave the cursor on the starting position, while keeping p as a shortcut to paste after the cursor while leaving the cursor to the end of the pasted text.

This doesn't require any new learning of commands, and seems to "just work".

Issue with this method: Trying this method out, the only side-effect I've noticed is that using . to repeat the Shift+P command doesn't use the remapping, so it still moves the cursor to the end of the paste. It's not a big issue, if I want to go down a line pasting items into the same column in each line, I can just turn on Caps Lock and press p without Shift for each line, then turn off Caps Lock again.

Any proper way to work around this issue?

Upvotes: 1

Cycl0n
Cycl0n

Reputation: 71

You can do it without leaving the row in the right keyboard layout (aside from p ofc):

pg;

g; goes to the previous position listed on the change list

For more info:

:help changelist
:help changes

Upvotes: 7

carl
carl

Reputation: 50534

The safest way without destroying a register is to do the following:

p`[

If you want to create a shortcut, just use any of vim's map functions that is suitable for you, eg:

noremap p p`[

Upvotes: 32

sillyMunky
sillyMunky

Reputation: 1278

You can quickly get back to where you were before pasting by pressing CTRL-o. This in general moves back to the previous cursor position.

I am pasting lines from all over a large document to one of 3 marks (moving around lines to come under headings). The fastest way I found is:

'ap

followed by CTRL-o

Upvotes: 3

Steve K
Steve K

Reputation: 2192

Whenever I have a sequence of steps to repeat several times I record a macro, which is trivially easy in Vim. The general method is

  1. Position the cursor where you want to make the first change.
  2. Type qx to start recording keystrokes.
  3. Make the first edit.
  4. Move the cursor to the position where the second edit should begin.
  5. Hit q again to quit recording.
  6. Type @x to replay the macro and make the next edit. The @ command takes a count so you can repeat the edit as many times as you want with one command.

So in your case, the entire sequence of keystrokes to record the macro might be

qxp`[jq

and 5@x to replay it five times for a total of 6 changes.

Note that the character after the first q is a register to record the macro into and it can be any letter, not just x. Just be careful your macros doesn't yank text into the register presently being recording into, it makes a real mess of things!

Macros can be arbitrarily long and complex. They can contain Ex mode commands and even call other macros.

Upvotes: 13

les2
les2

Reputation: 14469

'k' ? (as in the up arrow)

If you use 'p' to paste text below the current line, the cursor will be on the first line of the pasted content. Typing 'k' in command mode takes you to the line above the start of the pasted content.

Upvotes: -7

Related Questions