Add old article
60ddc0759f4ad946a025d94465ef066bd38ab35c
1 parent
dece8d45
log/articles/2010-04-24-staying-the-hell-out-of-insert-mode.txt
added
+88 -0
| 1 | + | Title: Staying the hell out of insert mode |
|
| 2 | + | Date: April 24th 2010 |
|
| 3 | + | ||
| 4 | + | Back in the day, the first thing I learnt about vi was how to get into insert |
|
| 5 | + | mode. It was really quite essential, because without knowing how, you couldn't |
|
| 6 | + | actually type anything. |
|
| 7 | + | ||
| 8 | + | The secret was in the "i" command. The `i` command was what made vi useable, |
|
| 9 | + | it was the alpha and the omega, vi's be-all end-all--and whenever the editor |
|
| 10 | + | would magically exit insert mode, panic would ensue and I'd frantically press |
|
| 11 | + | `i` to go back to insert-land. |
|
| 12 | + | ||
| 13 | + | I mean, what else would you want to do in a *text* editor, besides enter text? |
|
| 14 | + | It puzzled me for a long time. |
|
| 15 | + | ||
| 16 | + | The `i` key turned this rather archaic and obtuse program into a text-editor |
|
| 17 | + | which would respond predictably to keystrokes. I remember being asked from time |
|
| 18 | + | to time: "So what's the deal with vi?"; I'd answer something in the lines of: |
|
| 19 | + | "Vi? you have to press `i` to type. Also, try not to press `Esc`". |
|
| 20 | + | ||
| 21 | + | But that was then, and this is now. Today, vim is my primary editor. What |
|
| 22 | + | I'd like to show you, is how to stay the hell out of insert mode. |
|
| 23 | + | ||
| 24 | + | ## Why stay out? |
|
| 25 | + | ||
| 26 | + | Insert mode is vi's weakest mode. In this mode, it's no better than *any other |
|
| 27 | + | editor*, and you may as well be using *any other editor*. Vi's true power lies |
|
| 28 | + | in its ‘Normal mode'. Yes, inserting text is *not* normal in vi-land. The |
|
| 29 | + | more time you spend in Normal mode, the more super-powers you will have. |
|
| 30 | + | Trust me, it doesn't get any *less* normal than Normal mode. |
|
| 31 | + | ||
| 32 | + | > Someone once argued that insert mode was actually vi's most powerful mode, |
|
| 33 | + | because it was the only mode in which you could insert text. He obviously |
|
| 34 | + | wasn't familiar with `:r`. |
|
| 35 | + | ||
| 36 | + | ## How do you get the frack out? |
|
| 37 | + | ||
| 38 | + | Common knowledge states that pressing the `Esc` key will get you out of insert |
|
| 39 | + | mode. This is correct. This is also not very useful. If you're going to move |
|
| 40 | + | in and out of insert mode all the time, you're going to want it to be as |
|
| 41 | + | seamless as possible. There are two other ways to get out of insert mode: |
|
| 42 | + | ||
| 43 | + | Ctrl-[ |
|
| 44 | + | ||
| 45 | + | and |
|
| 46 | + | ||
| 47 | + | Ctrl-C |
|
| 48 | + | ||
| 49 | + | The other alternative, which I have chosen, is to map a key sequence to `Esc`. |
|
| 50 | + | Vi lets you map arbitrary sequences of keys to anything you like. For instance, |
|
| 51 | + | you could map `jj` to `Esc`. `j` is on the home row, so you don't need to move |
|
| 52 | + | your fingers to exit insert mode. I tried this out for a while, as well as a |
|
| 53 | + | couple of other alternatives. In the end, I settled for `kj`-- as it was the |
|
| 54 | + | fastest to type. To create this mapping, add this to your `.vimrc` file: |
|
| 55 | + | ||
| 56 | + | inoremap kj <Esc> |
|
| 57 | + | ||
| 58 | + | Another really useful trick is switching to normal mode for a single command. |
|
| 59 | + | You can do this with `Ctrl-O`. For instance, say you're typing away and you |
|
| 60 | + | want to quickly save your work, you can type `Ctrl-O :w`, which will write the |
|
| 61 | + | file and put you back in insert mode. |
|
| 62 | + | ||
| 63 | + | ## Learning, the hard way |
|
| 64 | + | ||
| 65 | + | How do you stop yourself from spending too much time in insert mode? |
|
| 66 | + | Add these key mappings to your *.vimrc*: |
|
| 67 | + | ||
| 68 | + | inoremap <Left> <NOP> |
|
| 69 | + | inoremap <Right> <NOP> |
|
| 70 | + | inoremap <Up> <NOP> |
|
| 71 | + | inoremap <Down> <NOP> |
|
| 72 | + | ||
| 73 | + | This will make sure you don't get your little fingers on the arrow keys, and |
|
| 74 | + | start navigating (*gasp*) in insert mode. |
|
| 75 | + | ||
| 76 | + | ## During your stay |
|
| 77 | + | ||
| 78 | + | Even though staying out of insert mode is safest, knowing what you *can* do in |
|
| 79 | + | this mode can be useful for the duration of your stay. I know I bashed it a |
|
| 80 | + | little, but it deserves some recognition for these useful commands: |
|
| 81 | + | ||
| 82 | + | - `Ctrl-Y`: insert the character right above the cursor—you can see how this can be useful... |
|
| 83 | + | - `Ctrl-U`: delete the current line from the cursor position. |
|
| 84 | + | - `Ctrl-A`: re-insert the text inserted in the previous insert session. |
|
| 85 | + | ||
| 86 | + | I hope I didn't put you off of insert mode too much. After all, as someone once |
|
| 87 | + | said: "it is the only mode in which you can insert". |
|
| 88 | + |