# HG changeset patch # User Steve Losh # Date 1352586965 18000 # Node ID 495eedad2b7721ce680a9d9d9d1dcf98e6ee3920 # Parent 7ac975d8e717c946635d50748edb3a1bc9be830c Proof 12-16 diff -r 7ac975d8e717 -r 495eedad2b77 chapters/12.markdown --- a/chapters/12.markdown Sat Nov 10 17:09:56 2012 -0500 +++ b/chapters/12.markdown Sat Nov 10 17:36:05 2012 -0500 @@ -7,10 +7,9 @@ Autocommands are a way to tell Vim to run certain commands whenever certain events happen. Let's dive right into an example. -First, open a new file with `:edit foo` and close it right away with `:quit`. -Look on your hard drive and you'll notice that the file is not there. This is -because Vim doesn't actually *create* the file until you save it for the first -time. +Open a new file with `:edit foo` and close it right away with `:quit`. Look on +your hard drive and you'll notice that the file is not there. This is because +Vim doesn't actually *create* the file until you save it for the first time. Let's change it so that Vim creates files as soon as you edit them. Run the following command: @@ -45,13 +44,13 @@ * Starting to edit a file that doesn't already exist. * Reading a file, whether it exists or not. -* Switching a buffer's `filetype`. +* Switching a buffer's `filetype` setting. * Not pressing a key on your keyboard for a certain amount of time. * Entering insert mode. * Exiting insert mode. -This is just a tiny sample of the available events -- there are many more you -can use to do lots of interesting things. +This is just a tiny sample of the available events. There are many more you can +use to do lots of interesting things. The next part of the command is a "pattern" that lets you be more specific about when you want the command to fire. Start up a new Vim instance and run the @@ -80,7 +79,7 @@ following command: :::vim - :autocmd BufWrite *.html :normal gg=G + :autocmd BufWritePre *.html :normal gg=G We're getting a bit ahead of ourselves here because we're going to talk about `normal` later in the book, but for now you'll need to bear with me because it's @@ -103,7 +102,8 @@ reindent the current file. Don't worry about how that works just yet. What we *do* want to pay attention to is the autocommand. The event type is -`BufWrite`, which means the event will be checked whenever you write *any* file. +`BufWritePre`, which means the event will be checked just before you write *any* +file. We used a pattern of `*.html` to ensure that this command will only fire when we're working on files that end in `.html`. This lets us target our @@ -117,7 +117,7 @@ events with a comma. Run this command: :::vim - :autocmd BufWrite,BufRead *.html :normal gg=G + :autocmd BufWritePre,BufRead *.html :normal gg=G This is almost like our last command, except it will also reindent the code whenever we *read* an HTML file as well as when we write it. This could be @@ -167,12 +167,12 @@ --------- Skim `:help autocmd-events` to see a list of all the events you can bind -autocommands to. You don't need to memorize each one right now, but just try -to get a feel for the kinds of things you can do. +autocommands to. You don't need to memorize each one right now; just try to get +a feel for the kinds of things you can do. Create a few `FileType` autocommands that use `setlocal` to set options for your favorite filetypes just the way you like them. Some options you might like to -change on a per-filetype basis are `wrap`, `list`, and `number`. +change on a per-filetype basis are `wrap`, `list`, `spell`, and `number`. Create a few more "comment this line" autocommands for filetypes you work with often. diff -r 7ac975d8e717 -r 495eedad2b77 chapters/13.markdown --- a/chapters/13.markdown Sat Nov 10 17:09:56 2012 -0500 +++ b/chapters/13.markdown Sat Nov 10 17:36:05 2012 -0500 @@ -29,8 +29,8 @@ Run the following commands: :::vim - :autocmd FileType javascript :iabbrev iff if ( ) {} :autocmd FileType python :iabbrev iff if: + :autocmd FileType javascript :iabbrev iff if () Open a Javascript file and try out the `iff` abbreviation. Then open a Python file and try it there too. Vim will perform the appropriate abbreviation diff -r 7ac975d8e717 -r 495eedad2b77 chapters/14.markdown --- a/chapters/14.markdown Sat Nov 10 17:09:56 2012 -0500 +++ b/chapters/14.markdown Sat Nov 10 17:36:05 2012 -0500 @@ -6,10 +6,10 @@ :::vim :autocmd BufWrite * :echom "Writing buffer!" -Now write your current buffer with `:write` and run `:messages` to view the +Now write the current buffer with `:write` and run `:messages` to view the message log. You should see the "Writing buffer!" message in the list. -Now write your current buffer again and run `:messages` to view the message log. +Now write the current buffer again and run `:messages` to view the message log. You should see the "Writing buffer!" message in the list twice. Now run the exact same autocommand again: @@ -17,7 +17,7 @@ :::vim :autocmd BufWrite * :echom "Writing buffer!" -Write your current buffer one more time and run `:messages`. You will see the +Write the current buffer one more time and run `:messages`. You will see the "Writing buffer!" message in the list *four* times. What happened? When you create an autocommand like this Vim has no way of knowing if you want @@ -40,8 +40,8 @@ :::vim :autocmd BufWrite * :sleep 200m -Now write the file. You may or may not notice a slight sluggishness in Vim's -writing time. Now run the command three more times: +Now write the current buffer. You may or may not notice a slight sluggishness +in Vim's writing time. Now run the command three more times: :::vim :autocmd BufWrite * :sleep 200m @@ -91,7 +91,7 @@ What happened when you wrote the file? Was it what you expected? If you thought Vim would "replace" the group, you can see that you guessed -wrong. Don't worry, most people think the same thing at first. +wrong. Don't worry, most people think the same thing at first (I know I did). When you use `augroup` multiple times Vim will *combine* the groups each time. diff -r 7ac975d8e717 -r 495eedad2b77 chapters/15.markdown --- a/chapters/15.markdown Sat Nov 10 17:09:56 2012 -0500 +++ b/chapters/15.markdown Sat Nov 10 17:36:05 2012 -0500 @@ -12,13 +12,14 @@ Some examples of operators are `d`, `y`, and `c`. For example: :::text - Operator - vvvvvv - dw " Delete to next word - ci( " Change inside parens - yt, " Yank until comma - ^^^^^^^^^^^ - Movement + Keys Operator Movement + ---- -------- ------------- + dw Delete to next word + ci( Change inside parens + yt, Yank until comma + +Movement Mappings +----------------- Vim lets you create new movements that work with all existing commands. Run the following command: @@ -28,7 +29,7 @@ Now type the following text into a buffer: - :::python + :::text return person.get_pets(type="cat", fluffy_only=True) Put your cursor on the word "cat" and type `dp`. What happened? Vim deleted @@ -43,7 +44,7 @@ We can use this new mapping immediately with all operators. Type the same text as before into the buffer (or simply undo the change): - :::python + :::text return person.get_pets(type="cat", fluffy_only=True) Put your cursor on the word "cat" and type `cp`. What happened? Vim deleted diff -r 7ac975d8e717 -r 495eedad2b77 chapters/16.markdown --- a/chapters/16.markdown Sat Nov 10 17:09:56 2012 -0500 +++ b/chapters/16.markdown Sat Nov 10 17:36:05 2012 -0500 @@ -23,7 +23,7 @@ This is some text about topic two. It has only one paragraph. -The lines "underlined" with `=` characters are treated as heading by Markdown. +The lines "underlined" with `=` characters are treated as headings by Markdown. Let's create some mappings that let us target headings with movements. Run the following command: @@ -68,7 +68,8 @@ Execute ------- -The `execute` takes a string and performs it as a command. Run this: +The `execute` takes a Vimscript string (which we'll cover in more detail later) +and performs it as a command. Run this: :::vim :execute "write" @@ -148,7 +149,7 @@ ------- That was a lot of work, but now we've looked at each part of the mapping. To -recap it in a nutshell: +recap: * We created a operator-pending mapping for "inside this section's heading". * We used `execute` and `normal!` to run the normal commands we needed to select @@ -176,9 +177,9 @@ The only difference from the previous mapping is the very end, where we select the text to operate on: - :::vim - kvg_ - g_vk0 + :::text + inside heading: kvg_ + around heading: g_vk0 The rest of the mapping is the same, so we still start on the first character of the line of equal signs. From there: