Terning Tricks

Tern for Vim

Next week I'll be speaking about Tern at Vim London. Tern is a server that allows text editors to intelligently autocomplete JavaScript, rename variables, and display documentation. It includes support for Node, jQuery, and standard JavaScript/ECMAScript.

During the preparation of my slides I realised there are a few things about Tern for Vim that aren't entirely obvious, mainly to do with configuration options. For one thing, the Vim plugin has excellent keyboard shortcuts that aren't enabled by default. To enable them, add this to your .vimrc:

let g:tern_map_keys=1

As far as I know the keyboard shortcuts are not documented, so here's a reference: tern_keyboard.pdf.

Shortcut Command Description
<Leader> td :TernDoc Documentation under cursor
<Leader> tb :TernDocBrowse Browse documentation
<Leader> tt :TernType Type hints
<Leader> td :TernDef Jump to definition (yes, 'td' is duplicated)
<Leader> tpd :TernDefPreview Jump to definition inside preview
<Leader> tsd :TernDefSplit Definition in new split
<Leader> ttd :TernDefTab Definition in new tab
<Leader> tr :TernRefs All references under cursor
<Leader> tR :TernRename Rename variable

Another useful configuration option is let g:tern_show_argument_hints='on_hold', which will display argument type hints when the cursor is left over a function. It can do a certain amount of introspection to figure out types, and also parses JSDoc-style comments.

blog comments powered by Disqus