# HG changeset patch # User Steve Losh # Date 1352577042 18000 # Node ID 41e61b3dce7adf46f8113057412d5033ca03f8d9 # Parent 78f061a9fff817e7349d23e0e193b4ff4b98cad3 Proof 3-6 diff -r 78f061a9fff8 -r 41e61b3dce7a chapters/03.markdown --- a/chapters/03.markdown Sat Nov 10 14:21:55 2012 -0500 +++ b/chapters/03.markdown Sat Nov 10 14:50:42 2012 -0500 @@ -1,9 +1,11 @@ Basic Mapping ============= -If there's one feature of Vimscript that will let you bend Vim to your will it's -the ability to map keys. Mapping keys lets you tell Vim: "when I press this -key, I want you to do this stuff instead of whatever you would normally do". +If there's one feature of Vimscript that will let you bend Vim to your will more +than any other, it's the ability to map keys. Mapping keys lets you tell Vim: + +> When I press this key, I want you to do this stuff instead of whatever you +> would normally do. We're going to start off by mapping keys in normal mode. We'll talk about how to map keys in insert and other modes in the next chapter. @@ -16,7 +18,7 @@ Put your cursor somewhere in the text and press `-`. Notice how Vim deleted the character under the cursor, just like if you had pressed `x`. -We already have a key for "delete that character under the cursor", so let's +We already have a key for "delete the character under the cursor", so let's change that mapping to something slightly more useful. Run this command: :::vim @@ -28,8 +30,8 @@ Special Characters ------------------ -You can use `` to tell Vim about special keys that are hard to type. -Try running this command: +You can use `` to tell Vim about special keys. Try running this +command: :::vim :map viw @@ -51,19 +53,20 @@ one of the places where Vim comments don't work. Try running this command: :::vim - :map viw " Use space to select a word + :map viw " Select word If you try pressing `` now, something horrible will almost certainly happen. Why? When you press the space bar now, Vim thinks you want it to do what -`viw"Usespacetoselectaword` -would do. Obviously, this isn't what we want. +`viw"Selectword` would do. Obviously this isn't what we +want. -This mapping is even more interesting though, because if you look closely at its -effect you might notice something strange. Take a few minutes to try to figure -out what it is before you move on. Don't worry if you don't get it now, because -we'll talk about it more later. +If you look closely at the effect of this mapping you might notice something +strange. Take a few minutes to try to figure out exactly what happens when you +use it, and *why* that happens. + +Don't worry if you don't get it right away -- we'll talk about it more soon. Exercises --------- diff -r 78f061a9fff8 -r 41e61b3dce7a chapters/04.markdown --- a/chapters/04.markdown Sat Nov 10 14:21:55 2012 -0500 +++ b/chapters/04.markdown Sat Nov 10 14:50:42 2012 -0500 @@ -2,11 +2,11 @@ ============= In the last chapter we talked about how to map keys in Vim. We used the `map` -command which made our keys work in normal mode. If you played around a bit -before moving on to this chapter, you may have noticed that our mappings also +command which made the keys work in normal mode. If you played around a bit +before moving on to this chapter, you may have noticed that the mappings also took effect in visual mode. -We can be more specific about when we want our mappings to work by using `nmap`, +You can be more specific about when you want mappings to apply by using `nmap`, `vmap`, and `imap`. These tell Vim to only use the mapping in normal, visual, or insert mode respectively. @@ -39,10 +39,10 @@ At first the idea of mapping the same key to do different things depending on which mode you're in may sound like a terrible idea. Why would you want to have to stop and think which mode you're in before pressing the key? Wouldn't -that negate any time you saved from the mapping itself? +that negate any time you save from the mapping itself? In practice it turns out that this isn't really a problem. Once you start using -Vim a lot you won't be thinking about the individual keys you're typing any +Vim often you won't be thinking about the individual keys you're typing any more. You'll think: "delete a line" and not "press `dd`". Your fingers and brain will learn your mappings and the keys themselves will become subconscious. @@ -56,15 +56,14 @@ :imap dd You might think that this would let you press `Ctrl+d` whenever you're in insert -mode to delete the current line. This would be nice, because now you don't need -to go back into normal mode to cut out lines! +mode to delete the current line. This would be handy because you wouldn't need +to go back into normal mode to cut out lines. Go ahead and try it. It won't work -- instead it will just put two `d`s in your file! That's pretty useless. The problem is that Vim is doing exactly what we told it to. We said: "when I press `` I want you to do what pressing `d` and `d` would normally do". - Well, normally when you're in insert mode and press the `d` key twice, you get two `d`s in a row! @@ -77,7 +76,7 @@ The `` is our way of telling Vim to press the Escape key, which will take us out of insert mode. -Now try the mapping. It works! But notice how you're now back in normal mode. +Now try the mapping. It works, but notice how you're now back in normal mode. This makes sense, because we told Vim that `` should exit insert mode and delete a line, but we never told it to go back into insert mode. @@ -86,8 +85,7 @@ :::vim :imap ddi -The `i` at the end reenters insert mode for us, and our mapping is finally -complete. +The `i` at the end enters insert mode, and our mapping is finally complete. Exercises --------- diff -r 78f061a9fff8 -r 41e61b3dce7a chapters/05.markdown --- a/chapters/05.markdown Sat Nov 10 14:21:55 2012 -0500 +++ b/chapters/05.markdown Sat Nov 10 14:50:42 2012 -0500 @@ -1,12 +1,11 @@ Strict Mapping ============== -Hold on, things are about to get a little wild. +Get ready, because things are about to get a little wild. So far we've used `map`, `nmap`, `vmap`, and `imap` to create key mappings that -will save time. These work, but they have a downside. - -Run the following commands: +will save time. These work, but they have a downside. Run the following +commands: :::vim :nmap - dd @@ -19,9 +18,9 @@ now I need to run `dd`", and so it deletes the current line. When you map keys with these commands Vim will take *other* mappings into -account. This may sound like a good thing at first but in reality it's pure, -unadulterated evil. Let's talk about why, but first remove those mappings by -running the following commands: +account. This may sound like a good thing at first but in reality it's pure +evil. Let's talk about why, but first remove those mappings by running the +following commands: :::vim :nunmap - @@ -35,7 +34,7 @@ :::vim :nmap dd Ojddk -You might think that this would change `dd` to: +At first glance it might look like this would map `dd` to: * Open a new line above this one. * Exit insert mode. @@ -43,7 +42,7 @@ * Delete the current line. * Move up to the blank line just created. -In effect, this should "clear the current line". Try it. +Effectively this should "clear the current line". Try it. Vim will seem to freeze when you press `dd`. If you press `` you'll get Vim back, but there will be a ton of empty lines in your file! What happened? @@ -58,11 +57,10 @@ * Open a line. * Exit insert mode. * Move down a line. - * `dd` is mapped, so perform the mapping. - * ... + * `dd` is mapped, so perform the mapping, and so on. -This mapping can never finish running! Go ahead and remove it with the -following command: +This mapping can never finish running! Go ahead and remove this terrible thing +with the following command: :::vim :nunmap dd @@ -70,14 +68,13 @@ Side Effects ------------ -Not only do the `*map` commands we've learned so far run the danger of -recursing, but because they take other mappings into account they can change -when we install various plugins. +One downside of the `*map` commands is the danger of recursing. Another is that +their behavior can change if you install a plugin that maps keys they depend on. When you install a new Vim plugin there's a good chance that you won't use and -memorize every mapping it uses. Even if you *do*, you'd have to go back and -look through your `~/.vimrc` file to make sure none of your custom mappings use -something that the plugin has mapped, and vice versa. +memorize every mapping it uses. Even if you do, you'd have to go back and look +through your `~/.vimrc` file to make sure none of your custom mappings use a key +that the plugin has mapped. This would make installing plugins tedious and error-prone. There must be a better way. @@ -101,15 +98,12 @@ Each of the `*map` commands has a `*noremap` counterpart that ignores other mappings: `noremap`, `nnoremap`, `vnoremap`, and `inoremap`. -When to Use ------------ - When should you use these nonrecursive variants instead of their normal -counterparts? The answer is: always. +counterparts? **Always.** -**No, seriously, ALWAYS.** +**No, seriously, *always*.** Using a bare `*map` is just *asking* for pain down the road when you install a plugin or add a new custom mapping. Save yourself the trouble and type the diff -r 78f061a9fff8 -r 41e61b3dce7a chapters/06.markdown --- a/chapters/06.markdown Sat Nov 10 14:21:55 2012 -0500 +++ b/chapters/06.markdown Sat Nov 10 14:50:42 2012 -0500 @@ -29,9 +29,9 @@ creates a custom mapping to delete a line, while the second "clears" a line and puts you into insert mode. -This means we can pick a key that we don't care about (like `-`) as a "prefix" -key and create mappings on top of it. It means we have to type an extra key to -activate our mappings, but one extra keystroke can easily be absorbed into +This means you can pick a key that you don't care about (like `-`) as a "prefix" +key and create mappings on top of it. It means you'll have to type an extra key +to activate the mappings, but one extra keystroke can easily be absorbed into muscle memory. If you think this might be a good idea, you're right, and it turns out that Vim @@ -59,17 +59,18 @@ current line. Why bother with setting `` at all, though? Why not just include your -"prefix" key directly in your mapping commands? +"prefix" key directly in your mapping commands? There are three good reasons. -First, you may decide you need the normal function of your leader later on down -the road. Defining it in one place makes it easy to change later. +First of all, you may decide you need the normal function of your leader later +on down the road. Defining it in one place makes it easy to change later. Second, when someone else is looking at your `~/.vimrc` file they'll immediately know what you mean when you say ``, and they can simply copy your -mapping into their own `~/.vimrc` if they like it. +mapping into their own `~/.vimrc` if they like it even if they use a different +leader. -Finally, many Vim plugins map things on top of your ``, so if you've -already got it set up they will just work. +Finally, many Vim plugins create mappings that start with ``. If you've +already got it set up they'll work properly and will feel familiar. Local Leader ------------ @@ -90,6 +91,8 @@ Now you can use `` in mappings and it will work just like `` does, except for resolving to a different key. +Feel free to change this key to something else if you don't like backslash. + Exercises ---------