Last updated on May 5th, 2021 at 12:05 am
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
How to copy 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.
How to copy 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.
paste are equivalently referred to as
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
Copy Current Line in VIM
To copy the current line (line in which the cursor is placed), simply press
double lower y (
single upper case y (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;
You can also use the operation to copy two lines.
Where the dot (.) means current line and plus (+) means plus one line below the cursor.
To copy 3 lines, use 3yy or
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
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
To copy 5 lines above cursor (from the current cursor position);
You can also use other operations. for example, to copy the fifth line just right above the cursor;
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,
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;
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;
To copy line two and paste just below the current cursor position;
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:
To copy five lines just right above the cursor;
To copy five lines just below the cursor;
Copy All Lines in VIM
To copy all the lines in the entire file;
Well, there is more. But up-to that end, we conclude this guide on how to copy paste lines in vim.
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;
Other VIM tutorials
Delete Lines Matching Specific Pattern in a File using VIM