0

The default pasting behavior in vim is to put the line directly below the cursor. Pasting "Line 2" on "Line 1" results like this:

Line 1

Line 2
Line 1
Line 2

Is it possible to paste lines in vim with this result occurring instead?

Line 2
Line 1
1
  • 3
    The answer to this is so easy to find with a simple ::help p.
    – romainl
    Commented Jun 28, 2023 at 5:14

2 Answers 2

2

I would use the paste before: P (capital P) to paste before the cursor.

If you want the cursor to stay on the original line you could also do: gP

In opposition to paste after: p (small p) to paste after the cursor.

More information with: :help P, :help gP or :help put.

4

TLDR:

Use P (capital P) instead of p (lowercase p)


Long Answer:

Looking at the vim help section we can learn a cool few things about the "put" command:

  • keymap can be capitalized to put text before the cursor (most keybindings uses this technique, look at o and O)
  • you can provide a count (handy to copy multiple times a template)
  • you can specify the register it gets its value from (e.g. I use the void buffer "_dP when I need to paste but not yank the value I overwrite)
                        p put E353 E1240
["x]p           Put the text [from register x] after the cursor
            [count] times.

                            P
["x]P           Put the text [from register x] before the cursor
            [count] times.
2
  • Welcome to Vi and Vim! High-quality answers typically include explanations and references to documentation that help querents understand and further explore the answer. Feel free to edit to include additional information!
    – D. Ben Knoble
    Commented Jun 28, 2023 at 12:48
  • Done. Sorry, still new to this :)
    – agoodshort
    Commented Jul 5, 2023 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.