{"id":8758,"date":"2021-04-25T22:39:11","date_gmt":"2021-04-25T19:39:11","guid":{"rendered":"https:\/\/kifarunix.com\/?p=8758"},"modified":"2024-03-18T22:53:17","modified_gmt":"2024-03-18T19:53:17","slug":"how-to-copy-paste-lines-in-vim","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/how-to-copy-paste-lines-in-vim\/","title":{"rendered":"How to copy paste lines in vim"},"content":{"rendered":"\n
Follow this tutorial to learn how to copy paste lines in vim. VIM<\/a>, 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<\/em>.<\/p>\n\n\n\n Vim is featureful with the following being among its features:<\/p>\n\n\n\n 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.<\/p>\n\n\n\n VIM is available on the default Ubuntu\/Debian repositories and you simply install by running the command;<\/p>\n\n\n\n Similarly, you can install vim on CentOS\/RHEL by running the command;<\/p>\n\n\n\n Run the command below to install vim on FreeBSD systems;<\/p>\n\n\n\n For any other distro, consult the respective package manager to learn how to install vim.<\/p>\n\n\n\n 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.<\/p>\n\n\n\n The terms The copy paste operation in vim is achieved using:<\/p>\n\n\n\n The above operations should be done in To copy the current line (line in which the cursor is placed), simply press or <\/p>\n\n\n\n To copy specific number of lines under the cursor (beginning from the current location of the cursor <\/em>) in vim, simply specify the number of lines followed by the yank operation (N<\/em>yy<\/strong>). Where N<\/strong> is the number of lines.<\/p>\n\n\n\n For example, to copy two lines under the cursor (beginning from the current location of the cursor <\/em>), enter into normal mode by pressing ESC<\/strong> key, and press;<\/p>\n\n\n\n You can also use the operation to copy two lines.<\/p>\n\n\n\n Where the dot (.) means current line and plus<\/strong> (+) means plus one line below the cursor.<\/p>\n\n\n\n To copy 3 lines, use 3yy<\/strong> or 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 For example, to copy two lines from the current position of the cursor, press (in normal mode, or escape mode), To copy 5 lines above cursor (from the current cursor position);<\/p>\n\n\n\n You can also use other operations. for example, to copy the fifth line just right above the cursor;<\/p>\n\n\n\n To copy specific line number in vim, use the 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<\/strong> key and enter;<\/p>\n\n\n\n You can can copy specific line number and paste right above<\/strong> the cursor directly using the operation You can can copy specific line number and paste right below<\/strong> the cursor directly using the operation For example, to copy line two and paste just above the current cursor position;<\/p>\n\n\n\n To copy line two and paste just below the current cursor position;<\/p>\n\n\n\n You can copy a range of lines in vim using the operation, :M,Nyank<\/strong>. Where M,N<\/strong> is the line number range like 10 to 15 as in 10,15<\/strong>.<\/p>\n\n\n\n So to copy line 10-15, use:<\/p>\n\n\n\n To copy five lines just right above the cursor;<\/p>\n\n\n\n To copy five lines just below the cursor;<\/p>\n\n\n\n To copy all the lines in the entire file;<\/p>\n\n\n\n Well, there is more.<\/p>\n\n\n\n You can always paste the copied content using the p<\/strong> or P<\/strong> operations depending on the current position of the cursor.<\/p>\n\n\n\n To paste the lines below the cursor, simply enter p<\/strong>.<\/p>\n\n\n\n To paste the lines just above the cursor, simply enter, P<\/strong>.<\/p>\n\n\n\n\n
Copy and paste lines in vim<\/a><\/h2>\n\n\n\n
Install VIM in Linux<\/a><\/h3>\n\n\n\n
Install VIM on Ubuntu\/Debian systems<\/a><\/h4>\n\n\n\n
apt install vim<\/code><\/pre>\n\n\n\n
Install VIM on CentOS\/RHEL based systems<\/a><\/h4>\n\n\n\n
yum install vim<\/code><\/pre>\n\n\n\n
Install VIM on FreeBSD<\/a><\/h4>\n\n\n\n
pkg install vim<\/code><\/pre>\n\n\n\n
Copy and paste lines in vim<\/h3>\n\n\n\n
copy<\/code><\/strong> and
paste<\/code><\/strong> are equivalently referred to as
yank<\/strong><\/code> and
put<\/strong><\/code> 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.<\/p>\n\n\n\n
\n
yy or Y<\/code><\/strong>: copies\/yanks the current line, including the newline character at the end of the line.<\/li>\n\n\n\n
p or P<\/code><\/strong>: pastes\/puts a line\/lines under or above the cursor respectively.<\/li>\n<\/ul>\n\n\n\n
normal<\/code><\/strong> vim operation mode (this is the initial mode when you first open the file). You can enter into normal mode by pressing
ESC<\/code><\/strong> key.<\/p>\n\n\n\n
Copy Current Line in VIM<\/h4>\n\n\n\n
double lower y<\/code><\/strong> (
yy<\/code><\/strong>) or
single upper case y<\/code><\/strong> (Y).<\/p>\n\n\n\n
yy<\/strong><\/code><\/pre>\n\n\n\n
Y<\/strong><\/code><\/pre>\n\n\n\n
Copy Specific number of lines under the cursor in VIM<\/h4>\n\n\n\n
2yy<\/strong><\/code><\/pre>\n\n\n\n
:.,+yank<\/strong><\/code><\/pre>\n\n\n\n
:.,+2yank<\/code><\/strong>.<\/p>\n\n\n\n
Copy Specific number of lines above the cursor in VIM<\/a><\/h4>\n\n\n\n
yN-1k<\/code><\/strong>, where
N-1<\/code><\/strong> is the number of lines you want to copy.<\/p>\n\n\n\n
y2-1k<\/code><\/strong> which is equivalent to
y1k<\/code><\/strong>.<\/p>\n\n\n\n
y1k<\/strong><\/code><\/pre>\n\n\n\n
y4k<\/strong><\/code><\/pre>\n\n\n\n
:-4yank<\/strong><\/code><\/pre>\n\n\n\n
Copy Specific Line Number in VIM<\/a><\/h4>\n\n\n\n
:Nyank<\/code><\/strong> operation where N<\/strong> is the specific line number you want to copy.<\/p>\n\n\n\n
:set number<\/code><\/strong>.<\/p>\n\n\n\n
:2yank<\/strong><\/code><\/pre>\n\n\n\n
Copy Specific Line Number and Paste right above or below the cursor<\/h4>\n\n\n\n
:Nt-<\/code><\/strong>, where N<\/strong> is the line number you want to copy.<\/p>\n\n\n\n
:Nt<\/code><\/strong>., where N<\/strong> is the line number you want to copy.<\/p>\n\n\n\n
:2t-<\/strong><\/code><\/pre>\n\n\n\n
:2t<\/strong>.<\/code><\/pre>\n\n\n\n
Copy a range of Lines in VIM<\/a><\/h4>\n\n\n\n
:10,15yank<\/strong><\/code><\/pre>\n\n\n\n
:-4,.yank<\/strong><\/code><\/pre>\n\n\n\n
:.,+4yank<\/strong><\/strong><\/code><\/pre>\n\n\n\n
Copy All Lines in VIM<\/h4>\n\n\n\n
:%yank<\/strong><\/code><\/pre>\n\n\n\n
Pasting Copied Lines in VIM<\/h3>\n\n\n\n
Obtain VIM Help<\/h3>\n\n\n\n