Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does VIM paste 1 line below where expected?

Tags:

vim

vi

A simple recipe to demonstrate the behavior of pasting in Vim/Vi...

  1. Run vim
  2. Enter insert mode
  3. Add 3 different lines of garbage to your file
  4. Exit insert mode (press escape)
  5. Move the caret to the second line
  6. Enter the command dd (delete line)
  7. Enter the command p (paste)

Notice the pasted line is inserted below where you might expect it to be placed.

Why is Vim/Vi programmed with this behavior, and is there any way to change it? (ie: Have the line pasted one line above the default position.)

like image 517
FreelanceConsultant Avatar asked Feb 21 '15 19:02

FreelanceConsultant


People also ask

How do I copy a line and paste it below in Vim?

So if you need to copy lines in vim, you yank them and if you want to paste, you put them on a specific location. The copy paste operation in vim is achieved using: yy or Y : copies/yanks the current line, including the newline character at the end of the line.


2 Answers

Use P. It will paste above the cursor instead of below.

like image 128
Steve McKay Avatar answered Oct 19 '22 20:10

Steve McKay


p will paste below the current line, P will paste above the line. If you want you can swap the behaviors of P and p add the following to your vimrc.

nnoremap p P
nnoremap P p
like image 27
FDinoff Avatar answered Oct 19 '22 22:10

FDinoff