Search code examples
vi

How do you yank a part of a line in vi?


I've spent quite a bit of time searching for an answer, but I can only find answers for vim, which suggest using visual mode or registers. Neither of these seem like options for vi (version SVR4.0, Solaris 2.5.0). I know about D, yy, dd, which yank or delete the entire line or up to the end up the line.

I'm now wondering if it's even possible to yank/delete a part of a line: say, the part within the brackets in: I want to co[py this par]t of the line.


Solution

  • All of the movement commands work with yank, like h, j, k, l for left, down, up, right. So if the cursor is on the first p in

    I want to co[py this par]t of the line.
    

    y11l (yank eleven ell) gives

    py this par
    

    y3w gives you the next 3 words, meaning if the closing ] is not there it'll include the final t.

    If your cursor is on the [ (assuming that's actually part of the string), y% (move to matching bracket) gives:

    [py this par]