DISQUS

Tech-Recipes: Copy and paste text with vi or vim | UNIX | Tech-Recipes

  • john_doe · 1 year ago
    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?
  • fuzzy · 1 year ago
    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/Free...
  • Kesavan · 1 year ago
    Good one.,
  • ddreggors · 1 year ago
    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)
    <down arrow> (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.
  • ddreggors · 1 year ago
    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)
    <down arrow> (select the lines you wish to yank/cut)
    or
    <right/left arrow> (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!
  • saravanan · 1 year ago
    How to copy the portion of a file to another file in the same folder in unix?
  • Chrissy · 6 months ago
    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").
  • Maria · 10 months ago
    Perfect :)
  • Ronni · 8 months ago
    Nice explanation
  • aFree4U · 8 months ago
    thanks for posting this