# HG changeset patch # User Steve Losh # Date 1318042104 14400 # Node ID f719623ed1c7352dbe865c2f3862efc02c4164a9 # Parent b63f0efbd25a573768fe6ea0e1f390767f2dfb57 MOAR diff -r b63f0efbd25a -r f719623ed1c7 chapters/09.markdown --- a/chapters/09.markdown Fri Oct 07 21:47:47 2011 -0400 +++ b/chapters/09.markdown Fri Oct 07 22:48:24 2011 -0400 @@ -0,0 +1,91 @@ +More Mappings +============= + +I know we've talked a lot about mappings so far, but we're going to practice +them again now. Mappings are one of the easiest, fastest ways to make your Vim +editing more productive, so it's good to focus on them quite a bit. + +One concept that has showed up in several examples but that we haven't +explicitly talked about is mapping a sequence of multiple keys. + +Run the following command: + + :nnoremap jk dd + +Now make sure you're in normal mode and press `j` followed quickly by `k`. Vim +will delete the current line. + +Now try pressing only `j` and waiting for a bit. If you don't press `k` quickly +after the `j`, Vim decides that you don't want to active the mapping and instead +runs the normal `j` functionality (moving down a line). + +This mapping will make it painful to move around our file, so lets remove it. +Run the following command: + + :nunmap jk + +Now typing `jk` in normal mode will move down and then up a line as usual. + +A More Complicated Mapping +-------------------------- + +You've seen a bunch of simple mappings so far, so it's time to look at something +with a bit more meat to it. Run the following command: + + :nnoremap " viwa"hviwoi"lel + +Now *that's* an interesting mapping! First, go ahead and try it out. Enter +normal mode, put your cursor over a word in your text and type `"`. Vim +will surround the word in double quotes! + +How does this work? Let's split it apart into pieces and think of what each one +does: + + viwa"hviwoi"lel + +* `viw`: visually select the current word +* ``: exit visual mode, which leaves the cursor on the last character of + the word +* `a`: enter insert mode *after* the current character +* `"`: insert a `"` into the text, because we're in insert mode +* ``: return to normal mode +* `h`: move left one character +* `viw`: visually select the word once again +* `o`: move the cursor to the opposite side of the visual selection +* ``: exit visual mode, which leaves the cursor on the *first* character of + the word this time +* `i`: enter insert mode *before* the current character +* `"`: insert a `"` into the text again +* ``: return to normal mode +* `l`: move right, which puts our cursor on the first character of the word +* `e`: move to the end of the word +* `l`: move right, which puts our cursor over the ending quote + +Remember: because we used `nnoremap` instead of `nmap` it doesn't matter if +we've mapped any of the keys in this sequence to something else. Vim will use +the default functionality for everything in the sequence. + +Hopefully you can see how much potential Vim's mappings have, as well as how +unreadable they can become. + +Exercises +--------- + +Create a mapping similar to the one we just looked at, but for single quotes +instead of double quotes. + +Try using `vnoremap` to add a mapping that will wrap whatever text you have +*visually selected* in quotes. You'll probably need the `gv` command for this, +so read up on it with `:help gv`. + +Map `H` in normal mode to go to the beginning of the current line. Since `h` +moves left you can think of `H` as a "stronger" `h`. + +Map `L` in normal mode to go to the end of the current line. Since `l` +moves right you can think of `L` as a "stronger" `l`. + +Find out what commands you just overwrote by reading `:help H` and `:help L`. +Decide whether you care about them (you probably won't). + +Add all of these mappings to your `~/.vimrc` file, making sure to use your "edit +`~/.vimrc`" and "source `~/.vimrc`" mappings to do so. diff -r b63f0efbd25a -r f719623ed1c7 chapters/10.markdown --- a/chapters/10.markdown Fri Oct 07 21:47:47 2011 -0400 +++ b/chapters/10.markdown Fri Oct 07 22:48:24 2011 -0400 @@ -0,0 +1,78 @@ +Training Your Fingers +===================== + +In this chapter we're going to talk about how to learn Vim more effectively, but +we need to do a bit of preparation first. + +Let's set up one more mapping that will save more wear on your left hand than +any other mapping you ever create. Run the following command: + + :inoremap jk + +Now enter insert mode and type `jk`. Vim will act as if you pressed the escape +key and return you to normal mode. + +There are a number of ways to exit insert mode in Vim: + +* The escape key +* `` +* `` + +Each of those requires you to stretch your fingers uncomfortably. Using `jk` is +great, because the keys are right under two of your strongest fingers and you +don't have to perform a chord. + +Some people prefer using `jj` instead of `jk`, but I prefer `jk` for two +reasons: + +* It's typed with two separate keys, so you can "roll" your fingers instead of + using the same one twice. +* Pressing `jk` in normal mode out of habit will move down and then up, leaving + you exactly where you started. Using `jj` in normal mode will move you to + a different place in your file. + +Learning the Map +---------------- + +Now that you've got a great new mapping, how can you learn to use it? Chances +are you've already got the escape key in your muscle memory, so in the heat of +coding you'll hit it without even thinking. + +The trick to relearning a mapping is to *force* yourself to use it by +*disabling* the old key(s). Run the following command: + + :inoremap + +This effectively disables the escape key in insert mode by telling Vim to +perform `` (no operation) instead. + +Now you *have* to use your `jk` mapping to exit insert mode. At first you'll +forget, type escape and start trying to do something in normal mode and you'll +wind up with stray characters in your text. + +It will be frustrating, but if you stick with it you'll be surprised at how fast +your mind and fingers absorb the new mapping. Within an hour or two you won't +be accidentally hitting escape any more. + +This idea applies to any new mapping you create to replace an old one, and even +to life in general. When you want to change a habit, make it harder or +impossible to do! + +If you want to start cooking meals instead of microwaving TV dinners, don't buy +any TV dinners when you go shopping. You'll cook some real food when you get +hungry enough. + +If you want to quit smoking, always leave your cigarettes in your car's trunk. +When you get the urge to have a casual cigarette you'll think of what a pain in +the ass it will be to walk out to the car and are less likely to bother doing +it. + +Exercises +--------- + +If you still find yourself using the arrow keys to navigate around Vim in normal +mode, map them to `` to make yourself stop. + +If you still use the arrow keys in insert mode, map them to `` there too. +The right way to use Vim is to get out of insert mode as soon as you can and use +normal mode to move around. diff -r b63f0efbd25a -r f719623ed1c7 outline.org --- a/outline.org Fri Oct 07 21:47:47 2011 -0400 +++ b/outline.org Fri Oct 07 22:48:24 2011 -0400 @@ -7,8 +7,8 @@ ** leaders ** editing .vimrc faster ** abbreviations -** eating characters ** more about mappings +** training with ** autocommands ** augroups ** buffer-specific abbreviations @@ -23,7 +23,6 @@ **** map **** verbose map ** operator-pending maps -** training with ** status lines? ** a word on shortened command names * part 2 - programming in vimscript