Edit vi article
c9fbfce76d777d5485e9898767ff154cff12f894
1 parent
7755bb37
log/articles/2010-04-24-staying-the-hell-out-of-insert-mode.txt
+28 -16
| 1 | 1 | Title: Staying the hell out of insert mode |
|
| 2 | 2 | Date: April 24th 2010 |
|
| 3 | 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. |
|
| 4 | + | Back in the day, the first thing I learnt about [vi][1] was how to get into |
|
| 5 | + | insert mode. It was really quite essential, because without knowing how, you |
|
| 6 | + | couldn't actually type anything. |
|
| 7 | 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 |
|
| 8 | + | [1]: http://en.wikipedia.org/wiki/Vi |
|
| 9 | + | ||
| 10 | + | The secret was in the "`i`" command. The `i` command was what made vi useable, |
|
| 11 | + | it was the alpha and the omega, vi's be-all end-all---and whenever the editor |
|
| 10 | 12 | would magically exit insert mode, panic would ensue and I'd frantically press |
|
| 11 | - | `i` to go back to insert-land. |
|
| 13 | + | '`i`' to go back to insert-land. |
|
| 12 | 14 | ||
| 13 | - | I mean, what else would you want to do in a *text* editor, besides enter text? |
|
| 15 | + | I mean, what else would you want to do in a *text editor*, besides entering text? |
|
| 14 | 16 | It puzzled me for a long time. |
|
| 15 | 17 | ||
| 16 | 18 | The `i` key turned this rather archaic and obtuse program into a text-editor |
|
| 17 | 19 | 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 | + | to time: "So what's the deal with vi?"; I'd answer in the lines of: "Vi? you |
|
| 21 | + | have to press `i` to start typing. Also, try not to press `Esc`". |
|
| 22 | + | ||
| 23 | + | But that was then, and this is now. Today, vi ([vim][2] actually) is my primary |
|
| 24 | + | editor. What I'd like to show you, is how to stay the hell out of insert mode. |
|
| 20 | 25 | ||
| 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. |
|
| 26 | + | [2]: http://en.wikipedia.org/wiki/Vim_(text_editor) |
|
| 23 | 27 | ||
| 24 | 28 | ## Why stay out? |
|
| 25 | 29 | ||
| 26 | 30 | Insert mode is vi's weakest mode. In this mode, it's no better than *any other |
|
| 27 | 31 | 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 |
|
| 32 | + | in its 'Normal mode'. Yes, inserting text is *not* normal in vi-land. The |
|
| 29 | 33 | more time you spend in Normal mode, the more super-powers you will have. |
|
| 30 | 34 | Trust me, it doesn't get any *less* normal than Normal mode. |
|
| 31 | 35 | ||
| 32 | 36 | > Someone once argued that insert mode was actually vi's most powerful mode, |
|
| 33 | 37 | because it was the only mode in which you could insert text. He obviously |
| 48 | 52 | ||
| 49 | 53 | The other alternative, which I have chosen, is to map a key sequence to `Esc`. |
|
| 50 | 54 | Vi lets you map arbitrary sequences of keys to anything you like. For instance, |
|
| 51 | 55 | you could map `jj` to `Esc`. `j` is on the home row, so you don't need to move |
|
| 52 | 56 | 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 |
|
| 57 | + | couple of other alternatives. In the end, I settled for `kj`, as it was the |
|
| 54 | 58 | fastest to type. To create this mapping, add this to your `.vimrc` file: |
|
| 55 | 59 | ||
| 56 | 60 | inoremap kj <Esc> |
|
| 57 | 61 | ||
| 62 | + | So what if I actually wanted to write "jj" or "kj"? For example if I wanted to |
|
| 63 | + | write a blog post such as this one? Well, I'd just have to wait for the |
|
| 64 | + | first letter in the sequence to be inserted. The length of time vi waits for you |
|
| 65 | + | to complete such a sequence of characters is controlled by the `timeoutlen` setting, |
|
| 66 | + | which defaults to 1000 ms. You can change this as such: |
|
| 67 | + | ||
| 68 | + | set timeoutlen=200 |
|
| 69 | + | ||
| 58 | 70 | Another really useful trick is switching to normal mode for a single command. |
|
| 59 | 71 | You can do this with `Ctrl-O`. For instance, say you're typing away and you |
|
| 60 | 72 | want to quickly save your work, you can type `Ctrl-O :w`, which will write the |
|
| 61 | 73 | file and put you back in insert mode. |
|
| 62 | 74 | ||
| 63 | 75 | ## Learning, the hard way |
|
| 64 | 76 | ||
| 65 | 77 | How do you stop yourself from spending too much time in insert mode? |
|
| 66 | - | Add these key mappings to your *.vimrc*: |
|
| 78 | + | Well, you could add these key mappings to your *.vimrc*: |
|
| 67 | 79 | ||
| 68 | 80 | inoremap <Left> <NOP> |
|
| 69 | 81 | inoremap <Right> <NOP> |
|
| 70 | 82 | inoremap <Up> <NOP> |
|
| 71 | 83 | inoremap <Down> <NOP> |
|
| 72 | 84 | ||
| 73 | - | This will make sure you don't get your little fingers on the arrow keys, and |
|
| 85 | + | This would make sure you don't get your little fingers on the arrow keys, and |
|
| 74 | 86 | start navigating (*gasp*) in insert mode. |
|
| 75 | 87 | ||
| 76 | 88 | ## During your stay |
|
| 77 | 89 | ||
| 78 | 90 | 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 |
|
| 91 | + | this mode can be useful for the duration of your stay. I know I criticized it a |
|
| 80 | 92 | little, but it deserves some recognition for these useful commands: |
|
| 81 | 93 | ||
| 82 | 94 | - `Ctrl-Y`: insert the character right above the cursor—you can see how this can be useful... |
|
| 83 | 95 | - `Ctrl-U`: delete the current line from the cursor position. |
|
| 84 | 96 | - `Ctrl-A`: re-insert the text inserted in the previous insert session. |