VimPlugin: vim-snipmate: Difference between revisions

From wikinotes
 
(2 intermediate revisions by the same user not shown)
Line 18: Line 18:
|-
|-
| <code>~/.vim/snippets/${filetype}.snippets</code> || snippet files
| <code>~/.vim/snippets/${filetype}.snippets</code> || snippet files
|-
| <code>~/.vim/snippets/_.snippets</code> || global snippets
|-
|-
|}
|}
Line 57: Line 59:
= Tips n Tricks =
= Tips n Tricks =
<blockquote>
<blockquote>
== Custom Trigger ==
<blockquote>
{{ NOTE |
some completers have additional plugins to integrate snippets. ex coc-snippets }}
Snipmate may not play nice with your tab keybindings.<br>
Snipmate may not play nice with your tab keybindings.<br>
Since it's keybindings must be executed within insert mode, it may be useful to write your own entrypoint.
Since it's keybindings must be executed within insert mode, it may be useful to write your own entrypoint.
Line 62: Line 70:
<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
Line 83: Line 91:
imap <c-j> <Plug>SnipmateChooseOrComplete
imap <c-j> <Plug>SnipmateChooseOrComplete
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- Custom Trigger -->
</blockquote><!-- Tips n Tricks -->
</blockquote><!-- Tips n Tricks -->

Latest revision as of 03:06, 14 June 2023

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
~/.vim/snippets/_.snippets global snippets

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

Custom Trigger

NOTE:

some completers have additional plugins to integrate snippets. ex coc-snippets

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