Follow this tutorial to learn how to copy paste lines in vim. VIM, an acronym for Vi iMproved, is a highly configurable text editor built to make creating and changing any kind of text very efficient. It is included as “vi” with most UNIX systems and with Apple OS X.
Vim is featureful with the following being among its features:
- persistent, multi-level undo tree
- extensive plugin system
- support for hundreds of programming languages and file formats
- powerful search and replace
- integrates with many tools
Copy and paste lines in vim
Install VIM in Linux
Most Linux distributions do not usually ship with VIM pre-installed. If you have not already installed, then you can install it by running the commands below based on your system distro.
Install VIM on Ubuntu/Debian systems
VIM is available on the default Ubuntu/Debian repositories and you simply install by running the command;
apt install vim
Install VIM on CentOS/RHEL based systems
Similarly, you can install vim on CentOS/RHEL by running the command;
yum install vim
Install VIM on FreeBSD
Run the command below to install vim on FreeBSD systems;
pkg install vim
For any other distro, consult the respective package manager to learn how to install vim.
Copy and paste lines in vim
So, if you are editing some files using vim and within the editor, you need to copy and paste some lines to modify them instead of having to re-write the whole lines then vim offers various ways in which you can copy paste the lines. Let us see some of the examples on how to copy past lines in vim Linux.
The terms copy
and paste
are equivalently referred to as yank
and put
respectively. 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.p or P
: pastes/puts a line/lines under or above the cursor respectively.
The above operations should be done in normal
vim operation mode (this is the initial mode when you first open the file). You can enter into normal mode by pressing ESC
key.
Copy Current Line in VIM
To copy the current line (line in which the cursor is placed), simply press double lower y
(yy
) or single upper case y
(Y).
yy
or
Y
Copy Specific number of lines under the cursor in VIM
To copy specific number of lines under the cursor (beginning from the current location of the cursor ) in vim, simply specify the number of lines followed by the yank operation (Nyy). Where N is the number of lines.
For example, to copy two lines under the cursor (beginning from the current location of the cursor ), enter into normal mode by pressing ESC key, and press;
2yy
You can also use the operation to copy two lines.
:.,+yank
Where the dot (.) means current line and plus (+) means plus one line below the cursor.
To copy 3 lines, use 3yy or :.,+2yank
.
Copy Specific number of lines above the cursor in VIM
It is also possible to copy specific number of lines above the cursor in VIM, of course beginning from the current location of the cursor using the operation yN-1k
, where N-1
is the number of lines you want to copy.
For example, to copy two lines from the current position of the cursor, press (in normal mode, or escape mode), y2-1k
which is equivalent to y1k
.
y1k
To copy 5 lines above cursor (from the current cursor position);
y4k
You can also use other operations. for example, to copy the fifth line just right above the cursor;
:-4yank
Copy Specific Line Number in VIM
To copy specific line number in vim, use the :Nyank
operation where N is the specific line number you want to copy.
You can show number lines in vim using, :set number
.
For example, say you are at the end of the file being edited and you want to copy the second line of the vim, simply press ESC key and enter;
:2yank
Copy Specific Line Number and Paste right above or below the cursor
You can can copy specific line number and paste right above the cursor directly using the operation :Nt-
, where N is the line number you want to copy.
You can can copy specific line number and paste right below the cursor directly using the operation :Nt
., where N is the line number you want to copy.
For example, to copy line two and paste just above the current cursor position;
:2t-
To copy line two and paste just below the current cursor position;
:2t.
Copy a range of Lines in VIM
You can copy a range of lines in vim using the operation, :M,Nyank. Where M,N is the line number range like 10 to 15 as in 10,15.
So to copy line 10-15, use:
:10,15yank
To copy five lines just right above the cursor;
:-4,.yank
To copy five lines just below the cursor;
:.,+4yank
Copy All Lines in VIM
To copy all the lines in the entire file;
:%yank
Well, there is more.
Pasting Copied Lines in VIM
You can always paste the copied content using the p or P operations depending on the current position of the cursor.
To paste the lines below the cursor, simply enter p.
To paste the lines just above the cursor, simply enter, P.
Obtain VIM Help
You can obtain vim help by entering :help, on VIM;
:help