Text Expansion In Vim
These slides accompanied the 2013-09-17
NoVA Vim meetup and have since been
edited. They’re still a ...
Rationale
One of the most frequently cited reasons for
using vim is that it saves you time/keystrokes,
yet many people do ...
imap
● instantly replaces characters typed
● works even in the middle of a word
● if you start typing characters that happ...
iabbrev
● triggered when you finish typing a word and
hit space
● great for common strings like email
addresses and urls
●...
Snippets
Many snippet plugins, but the two I’m most
familar with are:
* Snipmate: port of textmate’s snippet behavior
* Ul...
My Ultisnips Configuration
let g:UltiSnipsExpandTrigger="<tab>"
let g:UltiSnipsJumpForwardTrigger="<tab>"
let g:UltiSnipsJ...
Quickly edit snippets
I used to have a snippet plugin installed, but never used it
because editing/listing them was too ha...
Basic usage
snippet l "console.log"
console.log(“${1}”);
endsnippet
I type: l<tab>
Result: console.log(“|”)
snippet for "for (...) {...}"
for (var ${2:i}=0; $2 < ${1:Things}.length; $2++) {
${3:$1[$2]}
};
endsnippet
I type “for<ta...
Visual replacement.
snippet if "if"
if (${1:condition}) {
${VISUAL}$0
}
endsnippet
I want to wrap code in an if block. I s...
Interpolation
snippet date "date"
the date is `date`
endsnippet
runs the bash date command and inserts its
result
snippet ...
Zen Coding
Plugins exist for many editors
* zencoding (now emmet) is the initial popular
vim port
* sparkup adds some feat...
sparkup examples
body>div>em
becomes:
<body>
<div>
<em></em>
</div>
</body>
More Examples
#id
.classname
ul.classname
li*3>em
head>title<body
visit https://github.com/rstacruz/sparkup#examples for a...
Sparkup Pros/Cons
● extended syntax
● jump between
insert locations
● not easily
configurable
● Main repo not
pathogen-fri...
How can I productively use input
expansion?
Pick a small set of uses that come up a lot.
Practice using the expansions unt...
Upcoming SlideShare
Loading in …5
×

Text expansion in vim

750 views
596 views

Published on

Published in: Technology, Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
750
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
2
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Text expansion in vim

  1. 1. Text Expansion In Vim These slides accompanied the 2013-09-17 NoVA Vim meetup and have since been edited. They’re still a bit more of an outline, but have a few gems of content. @mattboehm github.com/mattboehm
  2. 2. Rationale One of the most frequently cited reasons for using vim is that it saves you time/keystrokes, yet many people do not use any kind of input expansion. * type faster * save keystrokes (prevent RSI) * avoid common typos
  3. 3. imap ● instantly replaces characters typed ● works even in the middle of a word ● if you start typing characters that happen to be part of a map, the text doesn’t display on the screen til you finish the map or type a differing character ● no easy way to undo? ● (you probably don’t want this for most cases)
  4. 4. iabbrev ● triggered when you finish typing a word and hit space ● great for common strings like email addresses and urls ● no easy way to un-expand? =(
  5. 5. Snippets Many snippet plugins, but the two I’m most familar with are: * Snipmate: port of textmate’s snippet behavior * Ultisnips: inspired by textmate/snipmate. Added functionality, but requires vim compiled with a modern python.
  6. 6. My Ultisnips Configuration let g:UltiSnipsExpandTrigger="<tab>" let g:UltiSnipsJumpForwardTrigger="<tab>" let g:UltiSnipsJumpBackwardTrigger="<s-tab>" let g:UltiSnipsSnippetDirectories=["mysnippets"] I recommend copying the stock snippets into a separate directory and changing them to meet your needs.
  7. 7. Quickly edit snippets I used to have a snippet plugin installed, but never used it because editing/listing them was too hard. Make editing snippets effortless. nnoremap <leader>vs :call g:EditMySnippets() <CR> function! g:EditMySnippets() let ft = &ft tabe ~/.vim/bundle/mbsnippets/mysnippets/ call search(ft) endfunction When i type ,vs this opens my snippet dir in NERDTree and jumps to the first snippet containing the filetype of the original file
  8. 8. Basic usage snippet l "console.log" console.log(“${1}”); endsnippet I type: l<tab> Result: console.log(“|”)
  9. 9. snippet for "for (...) {...}" for (var ${2:i}=0; $2 < ${1:Things}.length; $2++) { ${3:$1[$2]} }; endsnippet I type “for<tab>” and get: for (var i=0; i < Things.length; i++) { Things[i] }; “Things” is highlighted. As I type, it changes to what I’m updating and all positions update in real time.
  10. 10. Visual replacement. snippet if "if" if (${1:condition}) { ${VISUAL}$0 } endsnippet I want to wrap code in an if block. I select it and type <tab>if<tab>
  11. 11. Interpolation snippet date "date" the date is `date` endsnippet runs the bash date command and inserts its result snippet ver "pythonversion" python version: `!p import sys;snip.rv=sys.version` endsnippet check out SirVer’s screencast for a better example:http://ww.sirver.net/blog/2012/03/31/fourth-episode-of-ultisnips-screencast/
  12. 12. Zen Coding Plugins exist for many editors * zencoding (now emmet) is the initial popular vim port * sparkup adds some features and is what I currently use
  13. 13. sparkup examples body>div>em becomes: <body> <div> <em></em> </div> </body>
  14. 14. More Examples #id .classname ul.classname li*3>em head>title<body visit https://github.com/rstacruz/sparkup#examples for a better presentation of examples
  15. 15. Sparkup Pros/Cons ● extended syntax ● jump between insert locations ● not easily configurable ● Main repo not pathogen-friendly; I use tristen’s branch
  16. 16. How can I productively use input expansion? Pick a small set of uses that come up a lot. Practice using the expansions until they’re ground into your brain then add more as desired. Do not look at the screen for feedback! You need to be able to type your shortcuts with your eyes closed while barely thinking about it.

×