Thursday, April 14

snippets in vim

There are a variety of plugins that let you get little “snippets” in Vim, which is to say little blobs of text with placeholders so that you can press a key and get a little template to fill in. The idea is that this is useful for things you wind up typing repeatedly.

DigitalOcean’s custom Markdown dialect contains a couple of things I wind up typing a lot. One of them is this:

```command
some shell commands go here
```

I’ve gotten kind of tired of this and my wrists hurt a lot lately, so I went looking for a snippet plugin. After asking around, I settled on trying UltiSnips.

I added the following to my .vimrc, which is using Vundle to manage plugins:

" snippets
Plugin 'SirVer/ultisnips'
Plugin 'honza/vim-snippets'

" Trigger configuration. Do not use <tab> if you use https://github.com/Valloric/YouCompleteMe.
let g:UltiSnipsExpandTrigger="<tab>"
let g:UltiSnipsJumpForwardTrigger="<c-b>"
let g:UltiSnipsJumpBackwardTrigger="<c-z>"

" If you want :UltiSnipsEdit to split your window.
let g:UltiSnipsEditSplit="vertical"

And then ran:

$ vim +BundleInstall

After that I messed around in help files for a while and came up with the following, in ~/.vim/UltiSnips/markdown.snippets:

snippet ` "```command ... ```"
\`\`\`command
${0:commands}
\`\`\`
endsnippet

So now I can be in insert mode, and type `[Tab] and get:

```command
commands
```

…where commands is a placeholder and my cursor is waiting for input.

Actually pretty slick, and I felt for a minute like maybe I’d keep using it, but then the underlying Python started spewing errors and I realized that the big library of snippets takes a bunch of random mnemonics for everything that I was probably never going to learn.