VimPlugin: vim-snipmate: Difference between revisions

From wikinotes
Line 62: Line 62:
<syntaxhighlight lang="vim">
<syntaxhighlight lang="vim">
" function to execute in place of snipmate's completer
" function to execute in place of snipmate's completer
"  - when completer visible, repeating key selects next snippet
"  - when only one completion remains, expand snippet
"  - otherwise, choose from available completions
func! SnipmateChooseOrComplete()
func! SnipmateChooseOrComplete()
     if pumvisible()
     if pumvisible()
        " when completer visible, repeating key selects next snippet
         call feedkeys("\<Down>")
         call feedkeys("\<Down>")
     else
     else
         if len(l:completions) == 1
         if len(l:completions) == 1
            " when only one completion remains, expand snippet
             call feedkeys("\<Plug>snipMateTrigger")
             call feedkeys("\<Plug>snipMateTrigger")
         else
         else
            " choose from available completions
             call feedkeys("\<Plug>snipMateShow")
             call feedkeys("\<Plug>snipMateShow")
         endif
         endif

Revision as of 15:27, 21 July 2022

vim snippets in envine written in viml.

Documentation

github https://github.com/garbas/vim-snipmate
snippet syntax https://github.com/garbas/vim-snipmate/blob/master/doc/SnipMate.txt#L309
sample snippets https://github.com/honza/vim-snippets/tree/master/snippets

Locations

~/.vim/snippets/${filetype}.snippets snippet files

Install

Plug 'https://github.com/marcweber/vim-addon-mw-utils'
Plug 'https://github.com/tomtom/tlib_vim'
Plug 'https://github.com/garbas/vim-snipmate'

Configuration

" version of snippets-fileformat to use
let g:snipMate { 'snippet_version': 1 }

Usage

Basics:

  • By default, triggering completion is bound to tab key.
  • If there are multiple matches, use ctrl n/p to select one, then hit enter.
  • Tab to move to each field in the expanded snippet.
<tab>       " cycle available, or choose from completion
<c-r><tab>  " (from insert) list avail completions

Tips n Tricks

Snipmate may not play nice with your tab keybindings.
Since it's keybindings must be executed within insert mode, it may be useful to write your own entrypoint.

" function to execute in place of snipmate's completer
"   - when completer visible, repeating key selects next snippet
"   - when only one completion remains, expand snippet
"   - otherwise, choose from available completions
func! SnipmateChooseOrComplete()
    if pumvisible()
        call feedkeys("\<Down>")
    else
        if len(l:completions) == 1
            call feedkeys("\<Plug>snipMateTrigger")
        else
            call feedkeys("\<Plug>snipMateShow")
        endif
    endif
endfunc

" bind a '<Plug>' command, (since we can execute it from insert mode)
inoremap <silent> <Plug>SnipmateChooseOrComplete <C-R>=<SID>SnipmateChooseOrComplete()<CR>

" create your keybinding
imap <c-j> <Plug>SnipmateChooseOrComplete