{"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<p>Follow this tutorial to learn how to copy paste lines in vim. <a href=\"https:\/\/www.vim.org\/\" rel=\"nofollow noopener\" class=\"rank-math-link\" target=\"_blank\">VIM<\/a>, an acronym for Vi iMproved, is <em>a highly configurable text editor built to make creating and changing any kind of text very efficient. It is included as &#8220;vi&#8221; with most UNIX systems and with Apple OS X<\/em>.<\/p>\n\n\n\n<p>Vim is featureful with the following being among its features:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><em>persistent, multi-level undo tree<\/em><\/li>\n\n\n\n<li><em>extensive plugin system<\/em><\/li>\n\n\n\n<li><em>support for hundreds of programming languages and file formats<\/em><\/li>\n\n\n\n<li><em>powerful search and replace<\/em><\/li>\n\n\n\n<li><em>integrates with many tools<\/em><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"copy-past-lines-in-vim-linux\"><a class=\"rank-math-link\" href=\"#copy-past-lines-in-vim-linux\">Copy and paste lines in vim<\/a><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"install-vim-linux\"><a href=\"#install-vim-linux\" class=\"rank-math-link\">Install VIM in Linux<\/a><\/h3>\n\n\n\n<p>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<h4 class=\"wp-block-heading\" id=\"install-vim-ubuntu-debian\"><a href=\"#install-vim-ubuntu-debian\" class=\"rank-math-link\">Install VIM on Ubuntu\/Debian systems<\/a><\/h4>\n\n\n\n<p>VIM is available on the default Ubuntu\/Debian repositories and you simply install by running the command;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>apt install vim<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"install-vim-centos-rhel\"><a href=\"#install-vim-centos-rhel\" class=\"rank-math-link\">Install VIM on CentOS\/RHEL based systems<\/a><\/h4>\n\n\n\n<p>Similarly, you can install vim on CentOS\/RHEL by running the command;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>yum install vim<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><a href=\"#install-vim-freebsd\" class=\"rank-math-link\">Install VIM on FreeBSD<\/a><\/h4>\n\n\n\n<p>Run the command below to install vim on FreeBSD systems;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>pkg install vim<\/code><\/pre>\n\n\n\n<p>For any other distro, consult the respective package manager to learn how to install vim.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Copy and paste lines in vim<\/h3>\n\n\n\n<p>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<p>The terms <strong><code>copy<\/code><\/strong> and <strong><code>paste<\/code><\/strong> are equivalently referred to as <code><strong>yank<\/strong><\/code> and <code><strong>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<p>The copy paste operation in vim is achieved using:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>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<li><strong><code>p or P<\/code><\/strong>: pastes\/puts a line\/lines under or above the cursor respectively.<\/li>\n<\/ul>\n\n\n\n<p>The above operations should be done in <strong><code>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 <strong><code>ESC<\/code><\/strong> key.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Copy Current Line in VIM<\/h4>\n\n\n\n<p>To copy the current line (line in which the cursor is placed), simply press <strong><code>double lower y<\/code><\/strong> (<strong><code>yy<\/code><\/strong>) or <strong><code>single upper case y<\/code><\/strong> (Y).<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code><strong>yy<\/strong><\/code><\/pre>\n\n\n\n<p>or <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code><strong>Y<\/strong><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Copy Specific number of lines under the cursor in VIM<\/h4>\n\n\n\n<p>To copy specific number of lines under the cursor (<em>beginning from the  current location of the cursor <\/em>) in vim, simply specify the number of lines followed by the yank operation (<em>N<\/em><strong>yy<\/strong>). Where <strong>N<\/strong> is the number of lines.<\/p>\n\n\n\n<p>For example, to copy two lines under the cursor (<em>beginning from the current location of the cursor <\/em>), enter into normal mode by pressing <strong>ESC<\/strong> key, and press;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code><strong>2yy<\/strong><\/code><\/pre>\n\n\n\n<p>You can also use the operation to copy two lines.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code><strong>:.,+yank<\/strong><\/code><\/pre>\n\n\n\n<p>Where the dot (.) means current line and <strong>plus<\/strong> (+) means plus one line below the cursor.<\/p>\n\n\n\n<p>To copy 3 lines, use <strong>3yy<\/strong> or <strong><code>:.,+2yank<\/code><\/strong>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"vim-copy-lines-above-cursor\"><a href=\"#vim-copy-lines-above-cursor\" class=\"rank-math-link\">Copy Specific number of lines above the cursor in VIM<\/a><\/h4>\n\n\n\n<p>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 <strong><code>yN-1k<\/code><\/strong>, where <strong><code>N-1<\/code><\/strong> is the number of lines you want to copy.<\/p>\n\n\n\n<p>For example, to copy two lines from the current position of the cursor, press (in normal mode, or escape mode), <strong><code>y2-1k<\/code><\/strong> which is equivalent to <strong><code>y1k<\/code><\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code><strong>y1k<\/strong><\/code><\/pre>\n\n\n\n<p>To copy 5 lines above cursor (from the current cursor position);<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code><strong>y4k<\/strong><\/code><\/pre>\n\n\n\n<p>You can also use other operations. for example, to copy the fifth line just right above the cursor;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code><strong>:-4yank<\/strong><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"copy-specific-line-number-vim\"><a href=\"#copy-specific-line-number-vim\" class=\"rank-math-link\">Copy Specific Line Number in VIM<\/a><\/h4>\n\n\n\n<p>To copy specific line number in vim, use the <strong><code>:Nyank<\/code><\/strong> operation where <strong>N<\/strong> is the specific line number you want to copy.<\/p>\n\n\n\n<p>You can show number lines in vim using, <strong><code>:set number<\/code><\/strong>.<\/p>\n\n\n\n<p>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 <strong>ESC<\/strong> key and enter;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code><strong>:2yank<\/strong><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Copy Specific Line Number and Paste right above or below the cursor<\/h4>\n\n\n\n<p>You can can copy specific line number and paste right <strong>above<\/strong> the cursor directly using the operation <strong><code>:Nt-<\/code><\/strong>, where <strong>N<\/strong> is the line number you want to copy.<\/p>\n\n\n\n<p>You can can copy specific line number and paste right <strong>below<\/strong> the cursor directly using the operation <strong><code>:Nt<\/code><\/strong>., where <strong>N<\/strong> is the line number you want to copy.<\/p>\n\n\n\n<p>For example, to copy line two and paste just above the current cursor position;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code><strong>:2t-<\/strong><\/code><\/pre>\n\n\n\n<p>To copy line two and paste just below the current cursor position;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code><strong>:2t<\/strong>.<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"copy-a-range-of-lines-vim\"><a href=\"#copy-a-range-of-lines-vim\" class=\"rank-math-link\">Copy a range of Lines in VIM<\/a><\/h4>\n\n\n\n<p>You can copy a range of lines in vim using the operation, <strong>:M,Nyank<\/strong>. Where <strong>M,N<\/strong> is the line number range like 10 to 15 as in <strong>10,15<\/strong>.<\/p>\n\n\n\n<p>So to copy line 10-15, use:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code><strong>:10,15yank<\/strong><\/code><\/pre>\n\n\n\n<p>To copy five lines just right above the cursor;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code><strong>:-4,.yank<\/strong><\/code><\/pre>\n\n\n\n<p>To copy five lines just below the cursor;<\/p>\n\n\n\n<pre id=\"block-26550fda-5779-4e33-a789-5a3ffa556843\" class=\"wp-block-preformatted\"><strong>:.,<strong>+4yank<\/strong><\/strong><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Copy All Lines in VIM<\/h4>\n\n\n\n<p>To copy all the lines in the entire file;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code><strong>:%yank<\/strong><\/code><\/pre>\n\n\n\n<p>Well, there is more.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Pasting Copied Lines in VIM<\/h3>\n\n\n\n<p>You can always paste the copied content using the <strong>p<\/strong> or <strong>P<\/strong> operations depending on the current position of the cursor.<\/p>\n\n\n\n<p>To paste the lines below the cursor, simply enter <strong>p<\/strong>.<\/p>\n\n\n\n<p>To paste the lines just above the cursor, simply enter, <strong>P<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Obtain VIM Help<\/h3>\n\n\n\n<p>You can obtain vim help by entering <strong>:help<\/strong>, on VIM;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code><strong>:help<\/strong><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Other VIM tutorials<\/h3>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/delete-lines-matching-specific-pattern-in-a-file-using-vim\/\" target=\"_blank\" aria-label=\" (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"rank-math-link\">Delete Lines Matching Specific Pattern in a File using VIM<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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<\/p>\n","protected":false},"author":1,"featured_media":8760,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[374,121,49],"tags":[3470,3471,3474,3473,3476,3479,375,3475,3478,3481,3472,3480,3477],"class_list":["post-8758","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-vim","category-howtos","category-command-cheatsheets","tag-copy-current-line-in-vim","tag-copy-lines-in-vim","tag-copy-paste-lines-in-vim","tag-copy-paste-vim","tag-p","tag-paste-lines-in-vim","tag-vim","tag-vim-copy","tag-vim-copy-paste","tag-vim-yank-to-copy","tag-yank","tag-yank-files-in-vim","tag-yy","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50","resize-featured-image"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/8758"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=8758"}],"version-history":[{"count":2,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/8758\/revisions"}],"predecessor-version":[{"id":21834,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/8758\/revisions\/21834"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media\/8760"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=8758"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=8758"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=8758"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}