Staying the hell out of insert mode
April 24th 2010Back in the day, the first thing I learnt about vi was how to get into insert mode. It was really quite essential, because without knowing how, you couldn’t actually type anything.
The secret was in the ‘i’ command.
The i command was what made vi useable, it was the alpha and the omega, vi’s be-all
end-all—and whenever the editor would magically exit insert mode, panic would ensue
and I’d frantically press i to go back to insert-land.
I mean, what else would you want to do in a text editor, besides enter text? It puzzled me for a long time.
The i key turned this rather archaic and obtuse program into a text-editor which would
respond predictably to keystrokes. I remember being asked from time to time: “So what’s
the deal with vi?” I’d answer something in the lines of: “Vi? you have to press i to
type. Also, try not to press Esc”.
But that was then, and this is now. Today, vim is my primary editor. What I’d like to show you, is how to stay the hell out of insert mode.
Why stay out?
Insert mode is vi’s weakest mode. In this mode, it’s no better than any other editor, and you may as well be using any other editor. Vi’s true power lies in its ‘Normal mode’. Yes, inserting text is not normal in vi-land. The more time you spend in Normal mode, the more super-powers you will have. Trust me, it doesn’t get any less normal than Normal mode.
Someone once argued that insert mode was actually vi’s most powerful mode, because it was the only mode in which you could insert text. He obviously wasn’t familiar with
:r.
How do you get the frack out?
Common knowledge states that pressing the Esc key will get you out of insert mode.
This is correct. This is also not very useful. If you’re going to move in and out of
insert mode all the time, you’re going to want it to be as seamless as possible. There
are two other ways to get out of insert mode:
Ctrl-[
and
Ctrl-C
This is especially useful if you have your Caps Lock key binded to Ctrl (which, of
course, you all do, right?).
The other alternative, which I have chosen, is to map a key sequence to Esc. Vi lets you
map arbitrary sequences of keys to anything you like. For instance, you could map
jj to Esc. j is on the home row, so you don’t need to move your fingers to exit
insert mode. I tried this out for a while, as well as a couple of other alternatives. In
the end, I settled for kj—as it was the fastest to type. To create this mapping, add
this to your .vimrc file:
inoremap kj <Esc>
Another really useful trick is switching to normal mode for a single command. You can do
this with Ctrl-O. For instance, say you’re typing away and you want to quickly save
your work, you can type Ctrl-O :w, which will write the file and put you back in
insert mode.
Learning, the hard way
How do you stop yourself from spending too much time in insert mode? Add these key mappings to your .vimrc:
inoremap <Left> <NOP>
inoremap <Right> <NOP>
inoremap <Up> <NOP>
inoremap <Down> <NOP>
This will make sure you don’t get your little fingers on the arrow keys, and start navigating (gasp) in insert mode.
During your stay
Even though staying out of insert mode is safest, knowing what you can do in this mode can be useful for the duration of your stay. I know I bashed it a little, but it deserves some recognition for these useful commands:
Ctrl-Y: insert the character right above the cursor—you can see how this can be useful…Ctrl-U: delete the current line from the cursor position.Ctrl-A: re-insert the text inserted in the previous insert session.
I hope I didn’t put you off of insert mode too much. After all, as someone once said: “it is the only mode in which you can insert”.
Enjoyed the read? follow me on twitter, or subscribe to my feed.