# HG changeset patch # User Steve Losh # Date 1318049474 14400 # Node ID 48c5cc292f9932847d71539c411d96d7172cf697 # Parent 74a20f78f27f23c453b30a6af23ed9d6292e54c2 Local abbreviations. diff -r 74a20f78f27f -r 48c5cc292f99 chapters/13.markdown --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chapters/13.markdown Sat Oct 08 00:51:14 2011 -0400 @@ -0,0 +1,50 @@ +Buffer-Local Abbreviations +========================== + +That last chapter was a monster, so let's tackle something easier. We've seen +how to define buffer-local mappings and options, so let's apply the same idea to +abbreviations. + +Open your `foo` and `bar` files again, switch to `foo`, and run the following +command: + + :iabbrev --- — + +While still in `foo` enter insert mode and type the following text: + + Hello --- world. + +Vim will replace the `---` for you. Now switch to `bar` and try it. It should +be no surprise that it's not replaced, because we defined the abbreviation to be +local to the `foo` buffer. + +Autocommands and Abbreviations +------------------------------ + +Let's pair up these buffer-local abbreviations with autocommands to set them to +make ourselves a little "snippet" system. + +Run the following commands: + + :autocmd FileType javascript :iabbrev iff if ( ) {} + :autocmd FileType python :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 +depending on the type of the current file. + +Exercises +--------- + +Create a few more "snippet" abbreviations for some of the things you type often +in specific kinds of files. Some good candidates are `return` for most +languages, `function` for javascript, and thinks like `“` and `”` +for HTML files. + +Add these snippets to your `~/.vimrc` file. + +Remember: the best way to learn to use these new snippets is to *disable* the +old way of doing things. Running `:iabbrev return NOPENOPENOPE` will +*force* you to use your abbreviation instead. Add these "training" snippets to +match all the ones you created to save time. + diff -r 74a20f78f27f -r 48c5cc292f99 outline.org --- a/outline.org Sat Oct 08 00:29:38 2011 -0400 +++ b/outline.org Sat Oct 08 00:51:14 2011 -0400 @@ -9,11 +9,9 @@ ** abbreviations ** more about mappings ** training with +** buffer-local mappings and options ** autocommands -** buffer-specific mappings -** buffer-specific abbreviations -** buffer-specific settings -** filetype autocommands +** buffer-local abbreviations ** augroups ** wrapping up mapping *** Mapping options