Home

Awesome

coc-snippets

Snippets solution for coc.nvim

2019-03-23 00_09_39

Snippet preview requires neovim 0.4 or latest vim8

It's capable of:

Note: some features of ultisnips and snipmate format snippets not supported, checkout Ultisnips features.

Why?

Python support

Ultisnips provider needs pythonx support on (neo)vim, to check the feature exists, try:

:echo has('pythonx')

On neovim, run command:

:checkhealth

If it is not installed, run:

pip install pynvim

and make sure you have Python 3 provider for neovim installed.

On vim8, run command:

:pyx print(1)

in your vim, if it throws, it means your vim is not compiled with python support or the python dynamic lib required by vim is missing(or broken).

Note: some python code may require python feature that not supported by the python interpreter used by vim, error will throw on that case.

Error will be shown when pythonx with (neo)vim can't work, fix pythonx support or disable ultisnips support by add "snippets.ultisnips.enable": false in your configuration file.

Install

In your vim/neovim, run command:

:CocInstall coc-snippets

Examples

" Use <C-l> for trigger snippet expand.
imap <C-l> <Plug>(coc-snippets-expand)

" Use <C-j> for select text for visual placeholder of snippet.
vmap <C-j> <Plug>(coc-snippets-select)

" Use <C-j> for jump to next placeholder, it's default of coc.nvim
let g:coc_snippet_next = '<c-j>'

" Use <C-k> for jump to previous placeholder, it's default of coc.nvim
let g:coc_snippet_prev = '<c-k>'

" Use <C-j> for both expand and jump (make expand higher priority.)
imap <C-j> <Plug>(coc-snippets-expand-jump)

" Use <leader>x for convert visual selected code to snippet
xmap <leader>x  <Plug>(coc-convert-snippet)

Make <tab> used for trigger completion, completion confirm, snippet expand and jump like VSCode.

Note from coc.nvim 0.0.82, functions starts with coc#pum should be used for custom completion of coc.nvim.

inoremap <silent><expr> <TAB>
      \ coc#pum#visible() ? coc#_select_confirm() :
      \ coc#expandableOrJumpable() ? "\<C-r>=coc#rpc#request('doKeymap', ['snippets-expand-jump',''])\<CR>" :
      \ CheckBackspace() ? "\<TAB>" :
      \ coc#refresh()

function! CheckBackspace() abort
  let col = col('.') - 1
  return !col || getline('.')[col - 1]  =~# '\s'
endfunction

let g:coc_snippet_next = '<tab>'

Ultisnips features

Some ultisnips features are not supported:

Note: python regex in snippet are converted to javascript regex, however, some regex patterns can't be supported by javascript, including (?x) (?s) \Z (?(id/name)yes-pattern|no-pattern).

Functions

Key-mappings

Note you can't use noremap with <Plug> key-mappings.

Commands

Options

F.A.Q

Q: How to check if a snippet successfully loaded?

A: Use command :CocCommand workspace.showOutput snippets

Q: Some ultisnips snippet not works as expected.

A: Reformat after change of placeholder feature can't be supported for now, and some regex pattern can't be converted to javascript regex pattern, so the snippet can be failed to load.

Q: Where to get snippets?

A: One solution is install honza/vim-snippets which is widely used. To create snippets yourself:

Q: Get error message ModuleNotFoundError: No module named 'vimsnippets'

A: Make sure honza/vim-snippets in your vim's &runtimepath.

Q: Do I need to install Ultisnips.

A: No! This extension is designed to work with or without Ultisnips, you can still install Ultisnips, but this extension would not run any code or read configuration from it.

Q: How to check jumpable or expandable at current position.

A: Use functions provided by coc.nvim: coc#expandable() coc#jumpable() and coc#expandableOrJumpable().

Q: It doesn't load snippets from vim-go.

A: It uses g:UltiSnipsSnippetDirectories which is not supported, you can add settings:

snippets.ultisnips.directories: [
  "UltiSnips",
  "gosnippets/UltiSnips"
],

to load it.

Q: How could I add custom UltiSnips snippets.

A: You can create snippet files in folder: $VIMCONFIG/coc/ultisnips, use command :CocCommand snippets.editSnippets to open user snippet of current filetype.

Supporting

If you like this extension, consider supporting me on Patreon or PayPal:

<a href="https://www.patreon.com/chemzqm"><img src="https://c5.patreon.com/external/logo/become_a_patron_button.png" alt="Patreon donate button" /> </a> <a href="https://www.paypal.com/paypalme/chezqm"><img src="https://werwolv.net/assets/paypal_banner.png" alt="PayPal donate button" /> </a>

License

MIT