Copy and paste text with vi or vim

Posted by Rex in UNIX

The ability to duplicate text in an editor can be handy. vi and vim have several useful copy and paste commands.


The command ‘Y’ or ‘yy’ copies (yanks) one or more lines. To copy one line, two lines, 10 lines, and all lines to the end of the file, respectively:

Y
2Y
10Y
yG

To paste the text contained in the buffer above (uppercase P) or below the current cursor position (lowercase p), respectively:

P
p

It is also possible to yank text within a line. The following commands yank text from the current cursor position to the end of the word and the end of the line, respectively:

yw
y$

The same commands paste the text within a line. Lower case p pastes after the cursor position and upper case P pastes before.

Paste will also work with deleted text, either lines or parts of lines. Be careful not to execute any other commands prior to pasting as this will empty the buffer.

The Conversation

Follow the reactions below and share your own thoughts.

36 Responses to “Copy and paste text with vi or vim”

  1. November 11, 2008 at 12:11 pm, john_doe said:

    I need to yank x amount of lines from the middle of the file. Am I now suppose to count the amount of lines or what?

    Reply

    • November 19, 2008 at 7:27 pm, fuzzy said:

      Nope – just use m to mark the start, with an arbitrary buffer name (so you might type mx). Move your cursor down to where you want to stop copying, and type y’x (or d’x if you’re cutting and pasting). Then move the cursor to the point where you want to paste, and type p. Magic.

      You might find something like this helpful as a reference: http://www.onlamp.com/pub/a/bsd/2001/10/25/FreeBSD_Basics.html?page=1

      Reply

  2. November 18, 2008 at 6:54 am, Kesavan said:

    Good one.,

    Reply

    • November 26, 2008 at 10:26 am, ddreggors said:

      Another nice trick is to use Visual Mode…

      go to the line you wish to start yanking/cutting
      CTRL-V (enable visual mode)
      SHIFT-$ (select to end of line)
      (select the lines you wish to yank/cut)
      d (cut lines) or y (yank/copy)

      now just use ‘p’ or ‘P’ as needed to paste below or above cursor!

      This is the same as fuzzy’s except it allows you to “SEE” what you are yanking.

      Reply

      • November 26, 2008 at 10:36 am, ddreggors said:

        Sorry one change…

        CTRL-V is Visual Block Mode
        simply use the lowercase “v” to enter visual mode and no need to do the “SHIFT-$” (should be SHIFT-4 or $) as mentioned above. So that process now beomes:

        go to the line you wish to start yanking/cutting
        v (enable visual mode)
        (select the lines you wish to yank/cut)
        or
        (select characters/words in a line)
        d (cut lines) or y (yank/copy)

        now just use ‘p’ or ‘P’ as needed to paste below or above cursor!

        Reply

  3. December 11, 2008 at 4:06 pm, saravanan said:

    How to copy the portion of a file to another file in the same folder in unix?

    Reply

    • June 03, 2009 at 12:18 am, Chrissy said:

      I realize your post is from 5 months ago from when I’m posting this, saravanan, but for anyone else who may read this:

      You have several options that I know of–and I’m not even a vim master. Just for clarity’s sake, fileA refers to the file you want to copy *from*, and fileB refers to the file you want to copy *to*.
      1. You could open fileB, execute the command “:r fileA” (which would copy all of fileA into the open file), and then remove the portions you don’t want.
      2. You could open fileA, execute the command “:split fileB” (which would open fileB to the side of fileA), select the lines you wish to copy from fileA with the combination Shift+V and up/down arrow or j/k, press “y” to yank the lines, switch over to fileB with Ctrl+W Ctrl+W, and then paste the lines with “p”.
      3. Open fileA, select the lines you wish to copy, yank the lines with “”by” (note that’s a double-quotation mark in front of the “b”, and that the “b” could be any letter of the alphabet), open fileB, and finally paste the lines with “”bp” (where “b” is the same letter you used to yank).

      Note that some of these methods may differ, depending on your version of vim. And by the by, the files don’t have to be in the same folder for these options to work; you simply specify the path to the files (i.e., “:split /path/to/fileB”).

      Reply

  4. February 18, 2009 at 1:21 pm, Maria said:

    Perfect :)

    Reply

  5. April 11, 2009 at 8:01 am, Ronni said:

    Nice explanation

    Reply

  6. March 26, 2010 at 12:50 pm, Anonymous said:

    this is my first time using the vi editor.Can anyone show me how to create user account in solaris 10.

    Reply

    • September 02, 2010 at 8:19 am, Me said:

      It’s raining. I will eat a horse for my breakfast..

      Reply

  7. November 05, 2010 at 1:26 pm, Website Design Montreal said:

    Thank you for all the comments. I found what I was look for at O’reilly ‘s onlamp website (Thanks to Fuzzy)

    For those of you who are still looking for me this worked
    :100,125w newfile

    To see the results, I can open up newfile like so:

    :e newfile

    Reply

  8. November 17, 2010 at 9:15 am, Ocean Wy said:

    if I yanked something wrong and I need to cancel it, what should I do?

    thanks

    Reply

    • September 19, 2012 at 1:58 pm, abhinav said:

      > esc u

      Reply

  9. November 26, 2010 at 6:40 am, Sutourdot said:

    hi …
    i eed information abou copying the text of pdf /msword file as following.

    there atre 5 lines . i want to copy 1st and 5 th line at a time with out continuety.
    please message me to sutourdot@yahoo.co.in

    Reply

  10. December 12, 2010 at 8:30 pm, David said:

    How do you copy part of a line and paste it into another line at the cursor position (not above or below the current line)?

    Reply

    • April 22, 2011 at 8:14 am, Guest said:

      Copy until the end of a line:
      y$
      Copy to the beginning of a line:
      y^
      Copy following 4 words:
      y4w

      Reply

  11. December 14, 2010 at 5:12 am, tg said:

    @chrissy – nice! thanks

    Reply

  12. December 29, 2010 at 5:14 am, Atrimodi said:

    copy and paste line :

    command is : yyp

    Reply

  13. December 30, 2010 at 8:33 pm, Patrick Cahill said:

    Here is how the FTP sequence transpires:

    Microsoft Windows XP [Version 5.1.2600]
    (C) Copyright 1985-2001 Microsoft Corp.
    C:>ftp 158.96.235.130
    Connected to 158.96.235.130.
    220-QTCP at DOMINONB.
    220 Connection will close if idle more than 30 minutes.
    User (158.96.235.130:(none)): xxxxxxx
    331 Enter password.
    Password:
    230 xxxxxxx logged on.
    ftp> bin
    200 Representation type is binary IMAGE.
    ftp> put “C:Documents and SettingsxxxxxxDesktopmysqlinst.savf”
    200 PORT subcommand request successful.
    150 Sending file to member SAVF in file MYSQLINST in library QGPL.
    > Netout :Software caused connection abort
    426-Cannot write to member SAVF in file MYSQLINST in library QGPL.
    426 Data transfer ended.
    ftp: 2293725 bytes sent in 52.08Seconds 44.04Kbytes/sec.
    ftp>

    I have done this file transfer to many other machines but this one is giving me trouble.

    Reply

  14. February 10, 2011 at 8:50 pm, Clint O Baxley said:

    Windows in a vim discussion. LOL my sides hurt.

    Reply

    • May 10, 2012 at 6:05 am, shawn said:

      lol that indeed was funny ^ there about windows in a vim discussion. my sides started hurting, too

      Reply

  15. June 12, 2011 at 11:29 am, Mahesh said:

    An useful post :)  

    Reply

  16. August 16, 2011 at 8:19 am, Trunghuynh Bk said:

    can’t anyone show me the way to type the linux path in VI editor.
    Do we have the functionality of TAB key in VI editor

    Reply

  17. January 09, 2012 at 3:37 am, shareef said:

    Hi,
    i am having text in a file liike
    abc
    xyz
    fgh
    jkl

    i need to print as
    abc abc
    xyz xyz
    fgh fgh
    jkl jkl
    in same file…can anyone tell me the way to do that (in vi/vim/unix..what ever)..thanks in advance…

    Thanks…
    Shareef Shiek

    Reply

  18. April 03, 2012 at 12:49 am, alok said:

    I wish to duplicate the lines present in my file.

    eg: Present file looks like
    abc
    xyz
    123
    the new file should look like:
    abc
    abc
    xyz
    xyz
    123
    123

    please help.

    Thanks
    Alok

    Reply

    • May 26, 2012 at 9:31 am, minime said:

      > I don’t know how to duplicate all the lines in a file in an efficient way, but one work-around is as follows:
      Go to the first line in the file. Denote by “a” an arbitrary letter. In normal mode type:
      qayypjqu
      [explanation: “qa” starts recording your following actions into register “a”. “yy” yanks the line, “p” pastes it below the line, “j” goes to the next line, “q” stops recording and “u” undoes what you just did, but keeps the register (in the first place, you just want to write the register)].

      Now type G and look at the lower right of the window to see how many lines your file has (I didn’t find a better solution, sorry for that). Assume it has 235 lines. All you now need to type is (again in normal mode): gg235@a
      [“gg” goes to the first line in the file, and “235@a” performs the actions in register “a” 235 times.]
      That should duplicate all the lines in the file.

      Reply

      • June 21, 2012 at 8:43 am, Koteswararao J said:

        > awk ‘{print $1,$1}’ filename > new_filename

        Reply

  19. January 19, 2013 at 5:24 pm, Harinder said:

    Hi

    Can anyone tell me,how to copy the line above of the selected line in Vii editor.

    for example:-

    i am a student
    i am doing b.tech
    i belongs to delhi

    my cursor position is on 3rd line and i want to copy of above 2 lines without move cursor on above lines….help me

    Reply

  20. February 11, 2013 at 4:30 am, sevak said:

    it helps.

    Reply

  21. March 14, 2013 at 6:30 am, aditya menon said:

    For some reason, I can’t get these commands to run in gVim on Windows :( Yank just doesn’t work, doesn’t do anything.

    Reply

  22. March 30, 2013 at 12:38 pm, Vlad said:

    Wtf.. even my terminal has copy paste and mouse selection and it even works with vi, why should i bother with those commands :)

    Reply

  23. June 30, 2013 at 11:33 pm, laike9m said:

    good article!

    Reply

  24. November 12, 2013 at 11:50 am, Rahul said:

    This is awesome :) thanks

    Reply

  25. November 17, 2013 at 4:15 pm, DDon Turner said:

    Any chance you could move this nasty “share” toolbar from the left of the screen? It impedes reading the text significantly! Many thanks.

    Reply

  26. February 19, 2014 at 4:50 am, Vyacheslav said:

    Thank you

    Reply

Leave a Reply