Mapping Tab to Both Snippets and Code Completion in Vim

SnippetsEmu allows you to create TextMate-style snippets in Vim which, together with Vim’s native code-completion feature, makes Vim a top choice among lightweight text editors. When I installed Snippy (as its creator, ironically, likes to call it), I had one problem: SnippetsEmu maps its functionality to the key, which is what I map code completion to. Thus, my mapping was causing Snippy not to work. As I was not about to oppose years of bash-trained muscle memory by mapping completion to a different key, I decided to bite the bullet (that hurts, by the way) and try to edit the plugin’s vim script. It turned out to be a very easy edit. Apparently the saints were smiling on me – or, at least, the patron saint of Not Having to Learn Vim Scripting.

Note that if you are using the SuperTab script, support is built into SnippetsEmu. The following is for everyone not using that script who wants to map the tab key.

Open snippetsEmu.vim and find the function s:ReturnKey. In my version, it’s on line 627. This function gets called when no snippet is found; all it does is output the tab character (or whatever key the snippets are mapped to). We want this function to output Control-P instead, since that is Vim’s native code-completion trigger. Find the line in s:ReturnKey that looks like this:

exe "return \"".substitute(g:snippetsEmu_key, '^<', "\\\\<","")."\""

This line might have been changed in your version, so just find the line that seems to do the same thing – output g:snippetsEmu_key. Replace that line with this:

return "\<c-p>"

That’s it! If that doesn’t work, all I know to do is pray. I suggest you pray to the patron saint of Damnit I Have to Learn Vim Scripting.