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

<p>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.</p>

<p>So, for example, if I type <code>def{TAB}</code> (<code>:imap def{TAB} def ():&lt;ESC&gt;3ha</code>), it expands to:</p>

<pre><code>def |(): # '|' represents the caret
</code></pre>

<p>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.</p>

<ul>
<li>Is there a way to avoid this or use this function more effectively to this end?</li>
<li>Is any other Vim feature better suited for this?</li>
</ul>

<hr>

<p>After taking a quick look at <a href="http://www.vim.org/scripts/script.php?script_id=1318" rel="noreferrer">SnippetsEmu</a>, it looks like it's the best option and much easier to customize than I first thought.</p>

<p>To continue with the previous example:</p>

<pre><code>:Snippet def &lt;{}&gt;():
</code></pre>

<p>Once defined, you can expand your snippet by typing <code>def{TAB}</code>.</p>

[{'answer_id': 15886, 'author': 'MDCore', 'author_id': 1896, 'author_profile': 'https://Stackoverflow.com/users/1896', 'pm_score': 4, 'selected': True, 'text': '<p><a href="http://www.vim.org/scripts/script.php?script_id=1318" rel="nofollow noreferrer">SnippetsEmu</a> is a useful snippets plugin. </p>\n'}, {'answer_id': 32324, 'author': 'Sébastien RoccaSerra', 'author_id': 2797, 'author_profile': 'https://Stackoverflow.com/users/2797', 'pm_score': 2, 'selected': False, 'text': '<p>As noted by MDCore, <a href="http://www.vim.org/scripts/script.php?script_id=1318" rel="nofollow noreferrer">SnippetsEmu</a> 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 <code>:ab[breviate]</code> command.</p>\n\n<pre><code>:ab[breviate] [&lt;expr&gt;] {lhs} {rhs}\n        add abbreviation for {lhs} to {rhs}.  If {lhs} already\n        existed it is replaced with the new {rhs}.  {rhs} may\n        contain spaces.\n        See |:map-&lt;expr&gt;| for the optional &lt;expr&gt; argument.\n</code></pre>\n'}, {'answer_id': 255647, 'author': 'Jeremy Cantrell', 'author_id': 18866, 'author_profile': 'https://Stackoverflow.com/users/18866', 'pm_score': 3, 'selected': False, 'text': '<p>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! :)</p>\n\n<p>Get it here: <a href="http://www.vim.org/scripts/script.php?script_id=2152" rel="noreferrer">snippets.vim</a></p>\n'}, {'answer_id': 879590, 'author': 'SergioAraujo', 'author_id': 2571881, 'author_profile': 'https://Stackoverflow.com/users/2571881', 'pm_score': 5, 'selected': False, 'text': '<p>Snipmate - like texmate :)\n<a href="http://www.vim.org/scripts/script.php?script_id=2540" rel="noreferrer">http://www.vim.org/scripts/script.php?script_id=2540</a></p>\n\n<p>video:\n<a href="http://vimeo.com/3535418" rel="noreferrer">http://vimeo.com/3535418</a></p>\n\n<pre><code>snippet def \n     """ ${1:docstring} """\n     def ${2:name}:\n         return ${3:value}\n</code></pre>\n'}, {'answer_id': 9919910, 'author': 'alwillis', 'author_id': 395537, 'author_profile': 'https://Stackoverflow.com/users/395537', 'pm_score': 3, 'selected': False, 'text': '<p>I just installed <a href="https://github.com/vim-scripts/UltiSnips#readme" rel="noreferrer">UltiSnips</a>. There’s a good article that explains why you might choose UltiSnips: <a href="http://fueledbylemons.com/blog/2011/07/27/why-ultisnips/" rel="noreferrer">Why UltiSnips?</a></p>\n\n<p>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.</p>\n'}, {'answer_id': 13133097, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 4, 'selected': False, 'text': '<p>As another suggestion (although slightly different) using vim\'s built in functionality:</p>\n\n<pre><code>:iabbrev def def(): #&lt;LEFT&gt;&lt;LEFT&gt;&lt;LEFT&gt;&lt;LEFT&gt;&lt;LEFT&gt;\n</code></pre>\n\n<p>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). </p>\n\n<p>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 <code>:help abbrev</code>.</p>\n\n<p>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):</p>\n\n<pre><code>au filetype python :iabbrev ... etc\n</code></pre>\n\n<p>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). </p>\n'}]
['https://Stackoverflow.com/questions/15334', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1670/']