One key shortcutsThis post is about one key shortcuts in vim.
Vim has macros to record complex sequences of commands.
We'll cover macros in a later post.
The most useful single key shortcut to repeat a command is the period.
n is a useful single key shortcut to repeat searches.
Let's look at how they work.
Repeating last change
Type 10i to go into insert mode.
Type I love Lucy! and escape to normal mode with jj or the Esc key.
You should have ten lines that say I love Lucy!
Go to the top of the document with 1G.
Type dd to erase one line.
Type . (<- a period!) to erase another line.
The period repeats the last change.
You can quickly erase all the lines holding down the period key.
That was an artificial example, but here's a more practical one.
Type i to go into insert mode.
Type: This sentence would be all right, except that it goes on and on and on and on and on and on and on and on. That's kind of a problem with my sentences, that they are circular and never get to the point, ever.
Looking over the sentence, we want to get rid of the bold words:
This sentence would be all right, except that it goes on and on and on and on and on and on and on and on. That's kind of a problem with my sentences, that they are circular and never get to the point, ever.
Type /on and followed by Enter to get to the first on.
Type www to get to first word you want to delete.
Type dw to delete the word. Now type . to delete each word that follows.
Navigate to the other bold words and delete them in a similar manner.
Remember, we could also do something like this with delete followed by a search command.
Repeating search commands
Use n to repeat search commands in the same direction.
To change direction, type the command for the other direction alone.
For example, /gazelle searches forward for gazelle.
n searches forward for the next instance of gazelle.
? reverses direction to search for gazelle backwards.
If we type n after changing direction, it repeats the backwards search for gazelle.
The dd key combination is new.
A repeated command means to apply that command to all of the current line.
dd means delete the current line.