Question

I have recently started using Vim as my text editor and am currently working on my own customizations.

I suppose keyboard mappings can do pretty much anything, but for the time being I'm using them as a sort of snippets facility almost exclusively.

So, for example, if I type def{TAB} (:imap def{TAB} def ():<ESC>3ha), it expands to:

def |(): # '|' represents the caret

This works as expected, but I find it annoying when Vim waits for a full command while I'm typing a word containing "def" and am not interested in expanding it.

  • Is there a way to avoid this or use this function more effectively to this end?
  • Is any other Vim feature better suited for this?

After taking a quick look at SnippetsEmu, it looks like it's the best option and much easier to customize than I first thought.

To continue with the previous example:

:Snippet def <{}>():

Once defined, you can expand your snippet by typing def{TAB}.

Was it helpful?

Solution

SnippetsEmu is a useful snippets plugin.

OTHER TIPS

Snipmate - like texmate :) http://www.vim.org/scripts/script.php?script_id=2540

video: http://vimeo.com/3535418

snippet def 
     """ ${1:docstring} """
     def ${2:name}:
         return ${3:value}

As another suggestion (although slightly different) using vim's built in functionality:

:iabbrev def def(): #<LEFT><LEFT><LEFT><LEFT><LEFT>

Now whenever you type def followed by a space or other non-word character, it will expand to the same as what you've given as the output of SnippetsEmu (the space comes from the space you entered to trigger the completion).

This approach doesn't suffer the "lag" issue you encountered using :inoremap, and is built-into vim. For more information on this feature, look at :help abbrev.

You may be concerned that being triggered by space not tab it will trigger unnecessarily, but in general vim is pretty smart about when to trigger it. The issue can be additionally mitigated by enabling the abbreviation only for certain file-types (eg, python):

au filetype python :iabbrev ... etc

Snip[ets] (Manager|Emu|Mate|.vim) is of course also a perfect solution, but it's nice to be aware of the alternatives (especially when they are built in).

If SnippetsEmu is too heavy or ambitious for what you need (it was for me), I wrote a plugin that manages snippets based on filetype. It even has tab completion when picking the snippet! :)

Get it here: snippets.vim

I just installed UltiSnips. There’s a good article that explains why you might choose UltiSnips: Why UltiSnips?

I haven’t used any of the other snippet plugins; I decided to take the plunge with one that seemed full-featured and would be able to accommodate me as I gain more Vim skills and want to do more sophisticated things.

As noted by MDCore, SnippetsEmu is a popular Vim script that does just that and more. If you need only expanding (without moving back the caret), you can use the standard :ab[breviate] command.

:ab[breviate] [<expr>] {lhs} {rhs}
        add abbreviation for {lhs} to {rhs}.  If {lhs} already
        existed it is replaced with the new {rhs}.  {rhs} may
        contain spaces.
        See |:map-<expr>| for the optional <expr> argument.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top