Vim: Text Manipulation

This is part 4 of a series of tutorial to Vim. You can read Part 3 here.

Copy and Paste

Moving around text is probably the most used operation used in any text editor. As you might have guessed, there are couple of ways to do these operations in Vim.

Lesson 5

In Vim's lingo copying is yanking.
You yank a line.

  • Hit y to yank; Yank takes in range-characters, words or lines. Hit y3w to copy 3 words. y10j (or y10<downarrow>) to yank 10 below lines. All the motion commands which we learned last time can be combined with y.
    • yy yanks the whole current line.
    • y<rightarrow> will yank just one character on the right.
  • d to delete stuff. Deleting not only removes text from buffer, it adds it to clipboard1. So d can be used to delete as well as to cut.
    • dd deletes current whole line.
    • x will delete one character below the cursor.
    • xp will exchange characters, try it to see what I mean.
  • p to paste stuff. You can use p to paste to right/below and P to paste above/before.
  • All of them, y, d, p, can be combined with any motion commands.

Till, In and Around

Let me tell you about 3 more motion commands which fit beautifully with yank and delete.

1: int foo(int a, int b, char *str) {
2:     a += b;
3:     str[0] = 'd';
4:     return a;
5: }
  1. t<char> (till): it will give you text till the first character is encountered
  2. i<bracket> (in): it will give you text in/inside the brackets
    • I've used this only with brackets, it may work with other characters which I'm unaware of.
  3. a<bracket> (a / around): gives back text inside bracket along with the
Initial Position Command Comment Resulting line Clipboard
line 1, column 0 dta delete till a a, int b, char *str) { int foo(int a
line 1, inside '(' yi( yanks arguments inside '(' not changed since we are yanking int a, int b, char *str
anywhere in lines 2 to 4 da{ deletes everything inside and including {…} (body of function) ~int foo(int a, int b, char *str) ~ { complete function body }

One more thing: Time Travel

From this part onwards you will be doing complex manipulation to text. It would be nice to have another important feature in our arsenal: Undo.

  • You can undo your last action by hitting u.
  • And, you can undo your undo (redo) by hitting <ctrl>+r

Vim stores all your changes along with timestamp. In fact, Vim stores undo branches, which can be very helpful in case you want to redo something you've over written. You can read more about undo branches here.

Summary

Command Comment
y yank, copy
d delete to delete as well as cut
p paste
P paste above/before
x delete character below cursor
t<char> do x till first instance of character 'char'
i<brac> do x to text inside enclosing bracket 'brac', works with {, [, (, <
a<brac> ditto as above, includes the bracket too in capture
u undo
<ctrl>+r redo
  • Yank, delete and paste can be combined with other motion commands
  • 'till', 'around', 'in' are your friends, remember them, specially when dealing with function arguments, HTML and languages which use '{' to enclose blocks.

    Click here for part 5

Footnotes:

1

not actually clipboard but registers, as we'll shall see.

Creative Commons License

Date: 2017-06-1

Author: Anurag Peshne

Emacs 25.2.2 (Org mode 9.1.14)

Validate