Snippets solution for coc.nvim
It's capable of:
- Load UltiSnips snippets.
- Load textmate format snippets from coc extensions.
- Load snipmate snippets.
- Provide snippets as completion items.
- Provide trigger key for trigger snippet.
- Provide snippets list for edit snippet.
- Provide
snippet.editSnippets
command for edit snippets of current filetype.
- Use same keys for jump placeholder.
- Nested snippet support.
- Always async, never slows you down.
- Improved match for complete items with TextEdit support.
- Edit snippet by
:CocList snippets
, sorted by mru.
In your vim/neovim, run command:
:CocInstall coc-snippets
" Use <C-l> to trigger snippet expand.
imap <C-l> <Plug>(coc-snippets-expand)
" Use <C-j> to select text for visual text of snippet.
vmap <C-j> <Plug>(coc-snippets-select)
" Use <C-j> to jump to forward placeholder, which is default
let g:coc_snippet_next = '<c-j>'
" Use <C-k> to jump to backward placeholder, which is default
let g:coc_snippet_prev = '<c-k>'
Note: when using same key for expand snippet and jump forward, jump forward would have higher priority, to make expand have higher priority, you should use:
imap <C-j> <Plug>(coc-snippets-expand-jump)
To open snippet files, use command:
:CocList snippets
Some ultisnips features are not supported:
- Position check of trigger option, including
b
,w
andi
. - Execute vim, python and shell code in snippet.
-
extends
,priority
andclearsnippets
command in snippet file. - Visual placeholder.
- Placeholder and variable transform.
- Expression snippet.
- Automatic trigger snippet.
- Context snippets.
- Support loading snipmate snippets.
- Execute shell code with custom shabang (will not support).
- Automatic reformat snippet after change of placeholder (can't support).
- Format related snippet options, including
t
,s
andm
(can't support). - Snippet actions (can't support).
Note coc-snippets convert UltiSnips snippets to textmate snippets and send it to coc's snippets manager, format snippets after snippet insert will not be supported except for placeholder transform which also supported by textmate snippet.
-
snippets.priority
: priority of snippets source, default90
. -
snippets.extends
: extends filetype's snippets with other filetypes, example:{ "cpp": ["c"], "javascriptreact": ["javascript"], "typescript": ["javascript"] }
-
snippets.shortcut
, shortcut in completion menu, defaultS
. -
snippets.autoTrigger
: enable auto trigger for auto trigger snippets, defaulttrue
. -
snippets.triggerCharacters
: trigger characters for completion, default[]
. -
snippets.loadFromExtensions
: specify whether to load snippets from -
snippets.textmateSnippetsRoots
: absolute directories that contains textmate/VSCode snippets to load. extensions, default:true
-
snippets.expandFallbackWithPum
: fallback action when expand failed and pumvisible, default:refresh
. Possible values:refresh
: trigger completion for new complete items.confirm
: confirm completion with current selected complete item.next
: select next complete item.none
: do nothing.
-
snippets.ultisnips.enable
: enable load UltiSnips snippets, defaulttrue
. -
snippets.ultisnips.usePythonx
: usepythonx
for eval python code, defaulttrue
. -
snippets.ultisnips.pythonVersion
: python version to use for run python code, default to3
, will always usepyx
commands on vim. -
snippets.ultisnips.directories
: directories that searched for snippet files, could be subfolder in every $runtimepath or absolute paths, default:["UltiSnips"]
-
snippets.snipmate.enable
: enable load snipmate snippets, defaulttrue
. -
snippets.snippets.author
: author name used forg:snips_author
Python regular expression of UltiSnips would be converted to javascript regex, however some
patterns are not supported, including (?s)
, \Z
, (?(id/name)yes-pattern|no-pattern)
.
The failed snippets would not be loaded, you can checkout the errors by open output channel:
:CocCommand workspace.showOutput snippets
Q: Can i use this without install ultisnips?
A: Yes, this extension could work with or without UltiSnips installed, it works independently, it doesn't use code or read configuration from UltiSnips.
Q: How to use same key for select next completion item and expand snippet?
A: Use condition keymap like:
inoremap <silent><expr> <TAB>
\ pumvisible() ? "\<C-n>" :
\ <SID>check_back_space() ? "\<TAB>" :
\ coc#rpc#request('doKeymap', ['snippets-expand', "\<TAB>"])
function! s:check_back_space() abort
let col = col('.') - 1
return !col || getline('.')[col - 1] =~# '\s'
endfunction
MIT