Vim Tip #4: Basic yank, delete, paste, and move


tags: Vim

Vim Tips

Once you know the basics of moving around (I repeat: spend a LOT of time practicing using hjkl), it's time to learn slightly more complex ways to move, yank, delete, and paste. These commands (like most) apply in Normal mode.

yy yanks (copies) the current line. A lot of Vim commands accept multipliers, so 3yy yanks this line and the 2 lines after it - so three lines in total. To paste the yanked lines, press p - this will paste after the cursor (or in this case, after the current line). P (capital P) will paste before/above the current line. (As I've mentioned: case is important, and changing a letter's case means it does something else.)

If you want to move the lines, use dd to delete the current line or 5dd to delete five lines. This also goes into the copy buffer so p or P will work on it too.

There are faster ways to move around: Ctrl-f moves forward a page, and Ctrl-b moves back a page. Also useful is the ability to get to the beginning or end of the document: gg goes to the beginning of the document, and G goes to the end of the document.

To get around a particular line, use 0 to go to the beginning of the line, and $ to get to the end.

All Vim commands seem arcane, and there are an immense number of them. You won't remember all of them, but practise each one as you learn them: you'll end up remembering the ones that are most useful to you.