b63f0efbd25a

Abbreviations
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Fri, 07 Oct 2011 21:47:47 -0400
parents 5b8c0b46ef78
children f719623ed1c7
branches/tags (none)
files chapters/07.markdown chapters/08.markdown introduction.markdown outline.org

Changes

--- a/chapters/07.markdown	Fri Oct 07 20:27:46 2011 -0400
+++ b/chapters/07.markdown	Fri Oct 07 21:47:47 2011 -0400
@@ -22,16 +22,16 @@
 Lets add a mapping that will open our `~/.vimrc` file in a split so we can edit
 it and get back to coding.  Run this command:
 
-    :nnoremap <leader>ev <c-w>v:edit $MYVIMRC<cr>
+    :nnoremap <leader>ev :vsplit $MYVIMRC<cr>
 
 I like to think of this command as "**e**dit my **v**imrc file".
 
 `$MYVIMRC` is a special Vim variable that points to your `~/.vimrc` file.  Don't
 worry about that for right now, just trust me that that variable works.
 
-Pressing `<c-w>v` in Vim opens a new vertical split.  If you'd prefer
-a horizontal split replace the `v` with an `s`, but in this age of widescreen
-monitors I don't know why you would.
+`:vsplit` opens a new vertical split.  If you'd prefer a horizontal split you
+can replace it with `:split`, but in this age of widescreen monitors I don't
+know why you would.
 
 Take a minute and think through that command in your mind.  The goal is: "open
 my `~/.vimrc` file in a new split".  Why does it work?  Why is every single
--- a/chapters/08.markdown	Fri Oct 07 20:27:46 2011 -0400
+++ b/chapters/08.markdown	Fri Oct 07 21:47:47 2011 -0400
@@ -0,0 +1,120 @@
+Abbreviations
+=============
+
+Vim has a feature called "abbreviations" that feel similar to mappings but are
+meant for use in insert, replace, and command modes.  They're extremely flexible
+and powerful, but we're just going to cover the most common uses here.
+
+We're only going to worry about insert mode abbreviations in this book. Run the
+following command:
+
+    :iabbrev adn and
+
+Now enter insert mode and type:
+
+    One adn two.
+
+As soon as you hit space after typing the "adn" Vim will replace it with "and".
+
+Correcting typos like this is a great use for abbreviations.  Run these
+commands:
+
+    :iabbrev waht what
+    :iabbrev tehn then
+
+Now enter insert mode again and type:
+
+    Well, I don't know waht we should do tehn.
+
+Notice how *both* abbreviations were substituted, even though you didn't type
+a space after the second one.
+
+Keyword Characters
+------------------
+
+Vim will substitute an abbreviation when you type any "non-keyword character"
+after an abbreviation.  "Non-keyword character" means any character not in the
+`iskeyword` option.  Run this command:
+
+    :set iskeyword?
+
+You should see something like `iskeyword=@,48-57,_,192-255`.  This format is
+very complicated, but in essence it means that all of the following are
+considered "keyword characters":
+
+* The underscore character (`_`).
+* All alphabetic ASCII characters, both upper and lower case, and their accented
+  versions.
+* Any characters with an ASCII value between 48 and 57 (the digits zero through
+  nine).
+* Any characters with an ASCII value between 192 and 255 (some special ASCII
+  characters).
+
+If you want to read the *full* description of this option's format you can check
+out `:help isfname`, but I'll warn you that you'd better have a beer at the
+ready if you don't want to start sobbing while reading.
+
+For our purposes you can simply remember that abbreviations will be expanded
+when you type anything that's not a letter, number, or underscore.
+
+More Abbreviations
+------------------
+
+Abbreviations are useful for more than just correcting typos.  Let's add a few
+more that can help in day-to-day text editing.  Run the following commands:
+
+    :iabbrev @@    steve@stevelosh.com
+    :iabbrev ccopy Copyright 2011 Steve Losh, all rights reserved.
+
+Feel free to replace my name and email address with your own, then enter insert
+mode and try them out.
+
+These abbreviations take large chunks of text that you type often and compress
+them down to a few characters.  Over time, this can save you a lot of time, as
+well as wear and tear on your fingers.
+
+Why Not Use Mappings?
+---------------------
+
+If you're thinking that abbreviations seem similar to mappings, you're right.
+However, they're intended to be used for different things.  Let's look at an
+example.
+
+Run this command:
+
+    :inoremap ssig --<cr>Steve Losh<cr>steve@stevelosh.com
+
+This is a *mapping* intended to let you insert your signature quickly.  Try it
+out by entering insert mode and typing `ssig`.
+
+It seems to work great, but there's a problem.  Try entering insert mode and
+typing this text:
+
+    Larry Lessig wrote the book "Remix".
+
+You'll notice that Vim has expanded the `ssig` in Larry's name!  Mappings don't
+take into account what characters come before or after the map -- they only look
+at the specific sequence that you map.
+
+Remove the mapping and replace it with an abbreviation by running the following
+commands:
+
+    :iunmap ssig
+    :iabbrev ssig --<cr>Steve Losh<cr>steve@stevelosh.com
+
+Now try out the abbreviation again.
+
+This time Vim will pay attention to the characters before and after `ssig` and
+only expand it when we want it to.
+
+Exercises
+---------
+
+Add abbreviations for some common typos you know you personally make to your
+`~/.vimrc` file.  Be sure to use the mappings you created in the last chapter to
+open and source the file!
+
+Add abbreviations for your own email address, website, and signature as well.
+
+Think of some pieces of text you type very often and add abbreviations for them
+too.
--- a/introduction.markdown	Fri Oct 07 20:27:46 2011 -0400
+++ b/introduction.markdown	Fri Oct 07 21:47:47 2011 -0400
@@ -17,9 +17,10 @@
 can redistribute it as long as you don't make any changes and don't charge for
 it.
 
-The source code to the book is available [on BitBucket][hg] and [on GitHub][git]
-If you find any mistakes or feel you could improve it feel free to send a pull
-request.
+The source code to the book is available [on BitBucket][hg] and [on
+GitHub][git].  If you find any mistakes or feel you could improve it feel free
+to send a pull request, but I'm retaining the copyright on the book for now, so
+if you're not comfortable with that I understand.
 
 [book]: /
 [hg]: http://bitbucket.org/sjl/learnvimscriptthehardway/
--- a/outline.org	Fri Oct 07 20:27:46 2011 -0400
+++ b/outline.org	Fri Oct 07 21:47:47 2011 -0400
@@ -6,18 +6,26 @@
 ** strict mapping
 ** leaders
 ** editing .vimrc faster
+** abbreviations
+** eating characters
+** more about mappings
 ** autocommands
 ** augroups
-** status lines
-** abbreviations
-** buffer-specific mapping
-** other mapping options
-*** <silent>
-*** <script>
-*** <unique>
-*** <expr>
+** buffer-specific abbreviations
+** buffer-specific mappings
+** wrapping up mapping
+*** Mapping options
+**** <silent>
+**** <script>
+**** <unique>
+**** <expr>
+*** checking mappings
+**** map
+**** verbose map
 ** operator-pending maps
 ** training with <nop>
+** status lines?
+** a word on shortened command names
 * part 2 - programming in vimscript
 ** variables
 ** variable scopes