Search code examples
vimsnipmate

vim and snipmate, how to undo expansion


I am using vim with snipmate plugin. How can I undo the expansion?

# 1) before expansion
def[tab here]

# 2) expanded
def method_name
end

# How to get to 1) step?

Solution

  • I have implemented such by explicitly creating an undo point when triggering the snippet. The challenge is that the same key (<Tab> by default) is used for snippet expansion and to jump to the next tab stop.

    These parts in ~/.vim/after/plugin/snipMate.vim should do it. Note that this doesn't deal with the literal tab fallback, as I have remapped the trigger key to something else (see my fork), and don't care about that part, but you could extract the relevant parts from TriggerSnippet() and add those to the conditional.

    function! s:UndoPointBeforeSnippet()
            return (exists('g:snipPos') ? '' : "\<C-g>u")
    endfunction
    inoremap <expr> <SID>(UndoPointBeforeSnippet) <SID>UndoPointBeforeSnippet()
    
    ino <silent> <script> <tab> <SID>(UndoPointBeforeSnippet) <c-r>=TriggerSnippet()<cr>