-
-
Notifications
You must be signed in to change notification settings - Fork 958
Completion with sources
-
Full LSP completion support, especially snippet and
additionalTextEdit
feature. You'll understand why it's awesome when you experience it with a coc extension likecoc-tsserver
. - Completion resolving on completion item change, it does async completion resolve on completion item change and the detail and documentation will be shown in a float window when possible.
- Asynchronous and parallel completion request. Unless using vim sources, your vim will never be blocked.
- Incomplete request and cancel request support, only incomplete completion requests will be triggered on filtering completion items and cancellation requests are sent to servers only when necessary.
- Start completion without timer. The completion will start after you type the first letter of a word by default and is filtered with new input after the completion has finished. Other completion engines use a timer to trigger completion, so you always have to wait after the typed character.
- Realtime buffer keywords. Coc will generate buffer keywords on buffer change in the background (with debounce), while some completion engines use a cache which isn't always correct. Plus, Locality bonus feature from VSCode is enabled by default.
- Filter completion items when possible. When you do a fuzzy filter with completion items, some completion engines will trigger a new completion, but coc.nvim will filter the items when possible, which makes it much faster.
There are 3 different trigger modes:
-
always
, the default mode, which triggers completion on a letter inserted ortriggerCharacters
(or trigger pattern match) defined by the current activated sources. -
trigger
, only trigger completion when you typetriggerCharacters
(or trigger pattern match) defined by the completion sources. -
none
, disable auto trigger completion, you will have to trigger the completion manually.
Lots of completion behavior can be changed by using the configuration file, check out :h coc-config-suggest
for details.
By default, coc.nvim use its own completeopt
option during completion to provide the best auto-completion experience.
There's no function of coc.nvim that can be used as omnifunc
because it's not possible to support all LSP completion features when using omnifunc
.
For features like textEdit
and additionalTextEdits
(mostly used by automatic import feature) of LSP to work, you have to confirm completion, which is <C-y>
by default in vim. Read the next section for example key-mappings.
You have to remap <cr>
to make sure it confirms completion when popup menu is visible, since default behavior of <CR>
could be different in regard to current completion state and completeopt
option (:h popupmenu-keys
).
inoremap <expr> <cr> pumvisible() ? "\<C-y>" : "\<C-g>u\<CR>"
Note: \<C-g>u
is used to break undo level.
To make <cr>
select the first completion item and confirm the completion when no item has been selected:
inoremap <silent><expr> <cr> pumvisible() ? coc#_select_confirm() : "\<C-g>u\<CR>"
to make coc.nvim format your code on <cr>
:
inoremap <silent><expr> <cr> pumvisible() ? coc#_select_confirm() : "\<C-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"
Use complete_info()
if you need to confirm completion, only when there's selected complete item:
if exists('*complete_info')
inoremap <silent><expr> <cr> complete_info(['selected'])['selected'] != -1 ? "\<C-y>" : "\<C-g>u\<CR>"
endif
You can make use of coc#refresh()
to trigger completion like this:
" use <tab> for trigger completion and navigate to the next complete item
function! s:check_back_space() abort
let col = col('.') - 1
return !col || getline('.')[col - 1] =~ '\s'
endfunction
inoremap <silent><expr> <Tab>
\ pumvisible() ? "\<C-n>" :
\ <SID>check_back_space() ? "\<Tab>" :
\ coc#refresh()
Note: the <tab>
could be remapped by another plugin, use :verbose imap <tab>
to check if it's mapped as expected.
" use <c-space>for trigger completion
inoremap <silent><expr> <c-space> coc#refresh()
Some terminals may send <NUL> when you press <c-space>, so you could instead:
" use <c-space>for trigger completion
inoremap <silent><expr> <NUL> coc#refresh()
inoremap <expr> <Tab> pumvisible() ? "\<C-n>" : "\<Tab>"
inoremap <expr> <S-Tab> pumvisible() ? "\<C-p>" : "\<S-Tab>"
Use command :CocList sources
to get current completion source list.
Name | Shortcut | Description |
---|---|---|
around |
[A] | Words of the current buffer. |
buffer |
[B] | Words of other open buffers. |
file |
[F] | Filename completion, auto-detected. |
You can configure completion sources by using coc-settings.json
-
"coc.source.{name}.enable"
: controls if the source is enabled. -
"coc.source.{name}.shortcut"
: shortcut used in completion menu. -
"coc.source.{name}.priority"
: priority of the source, lower priority sources will be sorted after high priority sources when they have same score. -
"coc.source.{name}.disableSyntaxes"
: syntax names used to disable source from completion, eg:["comment", "string"]
-
"coc.source.around.firstMatch"
: Filter complete items by first letter strict match, defaulttrue
. -
"coc.source.buffer.firstMatch"
: Filter complete items by first letter strict match, defaulttrue
. -
"coc.source.buffer.ignoreGitignore"
: Ignore git ignored files for buffer words, defaulttrue
. -
"coc.source.file.trimSameExts"
: Filename extensions to trim extension names on file completion, default[".ts", ".js"]
-
"coc.source.file.ignoreHidden"
: Ignore completion for hidden files, defaulttrue
. -
coc.source.file.ignorePatterns
: Ignore patterns of matcher module, default[]
.
- coc-sources: includes some common completion source extensions.
- coc-neco: viml completion support.
- coc-snippets: snippets solution for coc.nvim
- coc-vimtex: vimtex integration.
- coc-neoinclude: neoinclude integration.
- coc-lbdbq: email address completion.
- coc-browser: web browser words completion.
- coc-github-users: GitHub username completion.
- coc-db: Database completion.
- sphinx.nvim: Source for Sphinx's cross-referencing roles.