_includes/
_layouts/
_pages/
_posts/
2010-04-24-staying-the-hell-out-of-insert-mode.md
4.2 KiB
2020-07-19-popol.md
9.1 KiB
2021-01-22-nakamoto.md
8.2 KiB
2024-01-31-segfault-the-mother-of-invention.md
6.2 KiB
assets/
css/
fonts/
images/
js/
.gitignore
62 B
Gemfile
68 B
Gemfile.lock
1.7 KiB
Makefile
198 B
README
192 B
_config.yml
278 B
avatar.png
44.3 KiB
cloudhead.gpg
892 B
favicon.png
179 B
index.md
79 B
publish
1.6 KiB
robots.txt
33 B
_posts/2010-04-24-staying-the-hell-out-of-insert-mode.md
raw
| 1 | --- |
| 2 | title: "Staying the hell out of insert mode" |
| 3 | date: 24.04.2010 |
| 4 | type: post |
| 5 | layout: default |
| 6 | --- |
| 7 | |
| 8 | Back in the day, the first thing I learnt about [vi][1] was how to get into |
| 9 | insert mode. It was really quite essential, because without knowing how, you |
| 10 | couldn't actually type anything. |
| 11 | |
| 12 | [1]: http://en.wikipedia.org/wiki/Vi |
| 13 | |
| 14 | The secret was in the `i` command. The `i` command was what made vi useable, |
| 15 | it was the alpha and the omega, vi's be-all end-all - and whenever the editor |
| 16 | would magically exit insert mode, panic would ensue and I'd frantically press |
| 17 | `i` to go back to insert-land. |
| 18 | |
| 19 | I mean, what else would you want to do in a *text editor*, besides entering text? |
| 20 | It puzzled me for a long time. |
| 21 | |
| 22 | The `i` key turned this rather archaic and obtuse program into a text-editor |
| 23 | which would respond predictably to keystrokes. I remember being asked from time |
| 24 | to time: "So what's the deal with vi?"; I'd answer in the lines of: "Vi? you |
| 25 | have to press `i` to start typing. Also, try not to press `Esc`". |
| 26 | |
| 27 | But that was then, and this is now. Today, vi ([vim][2] actually) is my primary |
| 28 | editor. What I'd like to show you, is how to stay the hell out of insert mode. |
| 29 | |
| 30 | [2]: http://en.wikipedia.org/wiki/Vim_(text_editor) |
| 31 | |
| 32 | ## Why stay out? |
| 33 | |
| 34 | Insert mode is vi's weakest mode. In this mode, it's no better than *any other |
| 35 | editor*, and you may as well be using *any other editor*. Vi's true power lies |
| 36 | in its *Normal mode*. Yes, inserting text is *not* normal in vi-land. The |
| 37 | more time you spend in Normal mode, the more super-powers you will have. |
| 38 | Trust me, it doesn't get any *less* normal than Normal mode. |
| 39 | |
| 40 | > Someone once argued that insert mode was actually vi's most powerful mode, |
| 41 | > because it was the only mode in which you could insert text. He obviously |
| 42 | > wasn't familiar with `:r`. |
| 43 | |
| 44 | ## How do you get the hell out? |
| 45 | |
| 46 | Common knowledge states that pressing the `Esc` key will get you out of insert |
| 47 | mode. This is correct. This is also not very useful. If you're going to move |
| 48 | in and out of insert mode all the time, you're going to want it to be as |
| 49 | seamless as possible. There are two other ways to get out of insert mode: |
| 50 | |
| 51 | Ctrl-[ |
| 52 | |
| 53 | and |
| 54 | |
| 55 | Ctrl-C |
| 56 | |
| 57 | The other alternative, which I have chosen, is to map a key sequence to enter |
| 58 | normal mode. Vi lets you map arbitrary sequences of keys to anything you like. |
| 59 | For instance, you could map `jj` to normal mode. `j` is on the home row, so you |
| 60 | don't need to move your fingers to exit insert mode. I tried this out for a |
| 61 | while, as well as a couple of other alternatives. In the end, I settled on |
| 62 | `kj`, as it was the fastest to type. To create this mapping, add this to your |
| 63 | `.vimrc` file: |
| 64 | |
| 65 | inoremap kj <Esc> |
| 66 | |
| 67 | So what if I actually wanted to write "jj" or "kj"? For example if I wanted to |
| 68 | write a blog post such as this one? Well, I'd just have to wait for the |
| 69 | first letter in the sequence to be inserted. The length of time vi waits for you |
| 70 | to complete such a sequence of characters is controlled by the `timeoutlen` setting, |
| 71 | which defaults to `1000ms`. You can change this as such: |
| 72 | |
| 73 | set timeoutlen=200 |
| 74 | |
| 75 | Another really useful trick is switching to normal mode for a single command. |
| 76 | You can do this with `Ctrl-O`. For instance, say you're typing away in insert |
| 77 | mode and you want to quickly save your work, you can type `Ctrl-O` and `:w`, |
| 78 | which will write the file and put you back in insert mode. |
| 79 | |
| 80 | ## Learning, the hard way |
| 81 | |
| 82 | How do you stop yourself from spending too much time in insert mode? Well, you |
| 83 | could add these key mappings to your *.vimrc*: |
| 84 | |
| 85 | inoremap <Left> <NOP> |
| 86 | inoremap <Right> <NOP> |
| 87 | inoremap <Up> <NOP> |
| 88 | inoremap <Down> <NOP> |
| 89 | |
| 90 | This would make sure you don't get your little fingers on the arrow keys, and |
| 91 | start navigating (*gasp*) in insert mode. |
| 92 | |
| 93 | ## During your stay |
| 94 | |
| 95 | Even though staying out of insert mode is safest, knowing what you *can* do in |
| 96 | this mode can be useful for the duration of your stay. |
| 97 | |
| 98 | - `Ctrl-Y`: insert the character right above the cursor—you can see how this can be useful... |
| 99 | - `Ctrl-U`: delete the current line from the cursor position. |
| 100 | - `Ctrl-A`: re-insert the text inserted in the previous insert session. |
| 101 | |
| 102 | I hope I didn't put you off of insert mode too much. After all, as someone once |
| 103 | said: *it is the only mode in which you can insert*. |