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?
5 Answers
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`[
-
2Note when you repeat using . the mapping is ignored. Is that a vim bug, or is there a way to honor the mapping? This is especially useful when P rather than p is used, as Shift-P is awkward to repeat Commented Nov 10, 2009 at 22:58
-
@pixelbeat I think there's a repeat.vim which makes . more powerful/intelligent. Might help you.– aehlkeCommented May 13, 2010 at 1:08
-
1My twist on this is
nnoremap p P`[j
: This pastes before, keepiong the cursor at the same spot, then moves down a line. You can comment code, prepend/append stuff on consecutive lines as long as stuff is aligned, etc, all by mashing the p key. Very flexible. Commented Apr 3, 2013 at 17:49 -
1Is there any way to make
.
respect this remap when repeating a previousP
? Because currently when I runnnoremap P P`[
I get exactly the behavior I want withP
, but then if I hit.
after instead of anotherP
, it just reruns the rawP
. :(– mtraceurCommented Feb 18, 2020 at 19:27 -
Is it possible to make this mapping work for
p
key after<ctrl+o>
in insert mode? With this mapping, I get extra'[]'
in insert modectrl+o
andp
.– TunCommented May 13, 2020 at 14:06
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
- Position the cursor where you want to make the first change.
- Type qx to start recording keystrokes.
- Make the first edit.
- Move the cursor to the position where the second edit should begin.
- Hit q again to quit recording.
- 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.
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
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
'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.
-
p pastes after cursor. it doesn't have to be in next line. it depends on how you selected block to copy (v vs. shift-v). -1– user80168Commented Oct 19, 2009 at 8:12