tags:

views:

2794

answers:

6

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}.

A: 

Maybe macros would be better suited for short snippets?

mk
+4  A: 

SnippetsEmu is a useful snippets plugin.

MDCore
+2  A: 

As noted by MDCore, SnippetsEmu is a popular Vim script that does just that and more. If you need only expending (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.
Sébastien RoccaSerra
+3  A: 

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

Jeremy Cantrell
+14  A: 

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}

Freaking awesome!!!! Great script voyeg3r. Exactly what I was looking for.
Daniel