Last week we looked at optimizing your workflow when opening and switching buffers. I hope that you were more concious of how you can mix the multitude of Vim's commands into your own workflow. By knowing exactly the commands you use all the time, you can train your fingers to type exactly those commands. A Vim master isn't somebody who knows every command, but somebody who knows the commands that matter to get work done.
For the next few weeks, we're going to step back from larger scale workflow optimization and dive into low level commands. Today we're going to look at the multitude of ways to get from normal mode, into insert mode.
The specific keystroke wins we're looking for are small, but because they're multiplied by so many transitions into insert mode throughout the day, it adds up to a significant part of the editing effort.
Vim has a ton of ways to get into insert mode. Most of them can be thought of as having two parts: an action, then leaving you in insert mode.
a - Enter insert mode, either before or after the cursor, but
otherwise no action.
O - Make a newline, then go insert in it. If you give it a count, it
makes a bunch of newlines for you, all with the same content you type.
A - Go to beginning or end of line, then insert.
go to the true beginning of line (column 0) rather than the first character on
c<motion> - Change and substitute.
ciw for instance deletes the word your
on (using the 'inner word' text object), and puts you into insert mode. See
VimDrills #3 for a good overview of how text objects work. This
can also be a more traditional motion, for example
c$ would delete until the
end of the line, and leave you in insert mode.
<count>s - Substitute the next
<count> characters. If we were to split this
into distinct commands, it would be
<count>xi (delete characters, then go into
S - the whole line form of the substitute command is particularly interesting.
It deletes whatever is on the line, and puts you into insert mode. What I love
most about it is that if you're on the beginning of a line, and you hit
jumps to the correct indent level, ready for you to type a line of code. No need
to spam the tab key to get to the indent level of the surrounding code. Watch
the screencast to see what I mean.
gi - Hop back to last location you were in insert mode, and go back into
insert mode. If we were to split this up, it would be using the mark jump
`. and then going into insert mode. For a quick refresher on
marks, check out VimDrills #4.
After a check of my vimrc, it doesn't seem like I have any mappings for getting into insert mode. I'd love to hear from you about what you may use or find handy. There's a comment form at the bottom of the page.
Where before you had to perform an action, then insert. Combine them into a single command. It's that simple, this is not a tricky, deep thought week. We're going back to the core goal of training your fingers.
Don't let yourself get lazy with your muscle memory. If you forget, and use a slower combination of keys, undo and go back and repeat it with the correct command.