Move my cursor

Cursor movement in most editors (ed users can stop now) is a fundamental operation. In Vim (and old vi) cursor movement commands can be used in combination with other commands.

Most users know that dd deletes a line, but many do not know that it is actually a simplified form of the d command that deletes whatever you define with a cursor movement operation. I’ll explain with examples:

  • l (and the right-arrow key) moves one character right, so dl deletes the character under the cursor (since the cursor position is actually the left hand edge of the visible cursor), but x also deletes to same character, so use that instead;
  • w moves to the start of the next word, so dw deletes to the start of the next word;
  • ^ moves to the start of the line, so d^ deletes to the start of the line;
  • $ moves to the end of the line, so d$ deletes to the end of the line, but D also deletes to the end of the line, so use that instead;
  • G moves to the end of the file, so dG deletes to the end of the file;
  • { and } move back and forward paragraphs, so d{ deletes a paragraph backwards and d} deletes a paragraph forwards;
  • t moves forward (within a line) until it finds a character you specify, so tp will move forward until it finds a p (it won’t move if there is no p), so dtp will delete up to the p;
  • ' moves to the start of the line containing the mark you specify (you do know how to make marks, don’t you?) and ` moves to the actual mark position, so d'a will delete to the start of the line you marked with a and d`o will delete to the o mark;

There are too many other cursor movement commands for me to show, and there are many other commands that can use them. Of these, my next 2 favourites are y and =. y is the copy equivalent to the cut of d, and I’m assuming you know that p is paste. = is the smart indent command.

Leave a Reply

Your email address will not be published.