Drill 8: Getting Into Insert

Screencast

Review of Last Week

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.

This Week's Drill

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.

Commands

i and a - Enter insert mode, either before or after the cursor, but otherwise no action.

o and 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.

I and A - Go to beginning or end of line, then insert. gI will go to the true beginning of line (column 0) rather than the first character on the line.

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 insert mode)

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 S, it 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.

Mapping

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.

Practice

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.

Comments

comments powered by Disqus