-
-
Notifications
You must be signed in to change notification settings - Fork 959
Using snippets
Coc has snippets support in different ways:
- Snippet completion items from different vim snippet plugins, by use extension like: coc-ultisnips and coc-neosnippet.
- Snippet kind of completion item from language servers, which are snipmate format.
- Snippet completion items from coc extensions that contribute VSCode snippets.
Complete item of snippet kind would be shown with ~
appended in label by default:
Note: when snippet format of complete item is set on completion resolve, you won't see ~
, since it's impossible for vim to update label of complete item during completion.
The snippet is designed to expand only when the completionDone
is triggered by using <C-y>
for confirm, so that user could decide expand the snippet or not.
To make <cr>
for confirm completion, add
inoremap <silent><expr> <cr> pumvisible() ? coc#_select_confirm() : "\<C-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"
to your init.vim
.
-
coc#_select_confirm()
helps select first completion item when necessary and send<C-y>
to vim for confirm completion. -
\<C-g>u
used for break undo chain at current position. -
coc#on_enter()
notify coc that you have pressed<enter>
, so it can format your code on<enter>
.
A snippet session would cancel under the following conditions:
-
InsertEnter
triggered outside snippet. - Content change at final placeholder.
- Content added after snippet.
- Content changed in a snippet but not happens to a placeholder.
You can nest snippets in an active snippet session, just like VSCode.
To navigate forward/backward of a snippet placeholder, use <C-j>
and <C-k>
.
Vim global variable g:coc_snippet_next
and g:coc_snippet_prev
can be used to change the key-mapping.
If you don't like ~
as snippet indicator of complete item in completion menu, you can change that by using coc.preferences.snippetIndicator
in your coc-settings.json
.
To make snippet completion work just like VSCode, you need to install coc-snippets then configure your <tab>
in vim like:
inoremap <silent><expr> <TAB>
\ pumvisible() ? coc#_select_confirm() :
\ coc#expandableOrJumpable() ? coc#rpc#request('doKeymap', ['snippets-expand-jump','']) :
\ <SID>check_back_space() ? "\<TAB>" :
\ coc#refresh()
function! s:check_back_space() abort
let col = col('.') - 1
return !col || getline('.')[col - 1] =~# '\s'
endfunction
let g:coc_snippet_next = '<Tab>'
let g:coc_snippet_prev = '<S-Tab>'
And add:
// make vim select first item on completion
"suggest.noselect": false
to your coc-settings.json
.
To load VSCode snippets, you need install coc-snippets extension.
Then install a VSCode snippet extension from GitHub with a command like:
:CocInstall https://github.com/andys8/vscode-jest-snippets.git#master
Open a file with a snippet related filetype, like foo.js
as javascript
and type part of the prefix characters.
Tip VSCode snippets extension can also installed by use vim-plug