Commits

Steve Losh committed 495eeda

Proof 12-16

  • Participants
  • Parent commits 7ac975d

Comments (0)

Files changed (5)

File chapters/12.markdown

 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:
 
 * 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
 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
 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
 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
 ---------
 
 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.

File chapters/13.markdown

 Run the following commands:
 
     :::vim
-    :autocmd FileType javascript :iabbrev <buffer> iff if ( ) {}<left><left><left><left><left>
     :autocmd FileType python     :iabbrev <buffer> iff if:<left>
+    :autocmd FileType javascript :iabbrev <buffer> iff if ()<left>
 
 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

File chapters/14.markdown

     :::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:
     :::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
     :::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
 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.
 

File chapters/15.markdown

 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:
 
 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
 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

File chapters/16.markdown

 
     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:
 
 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"
 -------
 
 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
 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: