tessl install tessl/npm-coc-nvim@0.0.0LSP based intellisense engine for neovim & vim8.
Complexity: Basic | Category: Intelligence
Common Tasks: Trigger completion | Navigate menu | Expand snippets | Confirm selection
Intelligent code completion powered by LSP with custom popup menu, snippet integration, and source management.
inoremap <silent><expr> <c-space> coc#start()Trigger completion at cursor position.
inoremap <silent><expr> <TAB> coc#pum#visible() ? coc#pum#next(1) : "\<TAB>"
inoremap <silent><expr> <S-TAB> coc#pum#visible() ? coc#pum#prev(1) : "\<S-TAB>"Navigate through completion items with Tab/Shift-Tab.
inoremap <silent><expr> <CR> coc#pum#visible() ? coc#pum#confirm() : "\<CR>"Accept the selected completion item.
inoremap <silent><expr> <TAB>
\ coc#expandableOrJumpable() ? "\<C-r>=coc#snippet#next()\<CR>" : "\<TAB>"Expand snippet or jump to next placeholder.
coc#start([{options}])
" Returns: stringTrigger completion manually with optional configuration.
Parameters:
options: Optional dictionary with completion options:
source: Specific completion source to triggerReturns: Key sequence string for insert mode mapping.
Example:
" Manual completion trigger
inoremap <silent><expr> <c-space> coc#start()
" Trigger specific source
inoremap <silent><expr> <c-f> coc#start({'source': 'file'})coc#refresh()
" Returns: stringStart or refresh completion at cursor position.
Returns: Key sequence string for insert mode mapping.
Example:
inoremap <silent><expr> <c-n> coc#refresh()coc#expandable()
" Returns: booleanCheck if a snippet can be expanded at the cursor position.
Returns: 1 if expandable, 0 otherwise.
Example:
inoremap <silent><expr> <TAB>
\ coc#expandable() ? "\<C-r>=coc#rpc#request('doKeymap', ['snippets-expand'])\<CR>" :
\ "\<TAB>"coc#jumpable()
" Returns: booleanCheck if can jump to the next snippet placeholder.
Returns: 1 if jumpable, 0 otherwise.
Example:
let g:coc_snippet_next = '<tab>'coc#expandableOrJumpable()
" Returns: booleanCheck if can expand a snippet or jump to placeholder.
Returns: 1 if expandable or jumpable, 0 otherwise.
Example:
inoremap <silent><expr> <TAB>
\ coc#expandableOrJumpable() ? "\<C-r>=coc#snippet#next()\<CR>" :
\ coc#pum#visible() ? coc#pum#next(1) :
\ "\<TAB>"coc#_select_confirm()
" Returns: stringSelect the first completion item (if none selected) and confirm.
Returns: Key sequence for insert mode.
Example:
inoremap <silent><expr> <CR> coc#pum#visible() ? coc#_select_confirm() : "\<CR>"coc#on_enter()
" Returns: stringNotify coc.nvim that Enter was pressed. Used for format-on-enter and other Enter key handling.
Returns: Key sequence string.
Example:
inoremap <silent><expr> <CR> coc#pum#visible() ? coc#pum#confirm() :
\ "\<C-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"coc#complete_indent()Trigger auto-indent after completion. Used internally.
coc#pum#visible()
" Returns: booleanCheck if the completion popup menu is currently visible.
Returns: 1 if visible, 0 otherwise.
Example:
if coc#pum#visible()
echo "Completion menu is open"
endifcoc#pum#next(insert)
" Returns: stringSelect the next item in the popup menu.
Parameters:
insert: If 1, insert mode behavior; if 0, selection onlyReturns: Key sequence string.
Example:
inoremap <silent><expr> <TAB> coc#pum#visible() ? coc#pum#next(1) : "\<TAB>"coc#pum#prev(insert)
" Returns: stringSelect the previous item in the popup menu.
Parameters:
insert: If 1, insert mode behavior; if 0, selection onlyReturns: Key sequence string.
Example:
inoremap <silent><expr> <S-TAB> coc#pum#visible() ? coc#pum#prev(1) : "\<S-TAB>"coc#pum#select(index, insert, confirm)
" Returns: stringSelect a specific item by index in the popup menu.
Parameters:
index: Zero-based index of item to selectinsert: If 1, insert mode behaviorconfirm: If 1, confirm selection immediatelyReturns: Key sequence string.
Example:
" Select first item
inoremap <silent><expr> <c-1> coc#pum#select(0, 1, 1)coc#pum#confirm()
" Returns: stringConfirm the selected completion item.
Returns: Key sequence string.
Example:
inoremap <silent><expr> <CR> coc#pum#visible() ? coc#pum#confirm() : "\<CR>"coc#pum#cancel()
" Returns: stringCancel completion and restore original text.
Returns: Key sequence string.
Example:
inoremap <silent><expr> <Esc> coc#pum#visible() ? coc#pum#cancel() : "\<Esc>"coc#pum#stop()
" Returns: stringClose the popup menu without restoring original text.
Returns: Key sequence string.
Example:
inoremap <silent><expr> <c-e> coc#pum#stop()coc#pum#insert()Insert the word of the selected item without confirming.
Example:
inoremap <silent><expr> <c-y> coc#pum#visible() ? coc#pum#insert() : "\<c-y>"coc#pum#scroll(forward)
" Returns: stringScroll the popup menu or detail window.
Parameters:
forward: If 1, scroll down; if 0, scroll upReturns: Key sequence string.
Example:
inoremap <silent><expr> <C-d> coc#pum#visible() ? coc#pum#scroll(1) : "\<C-d>"
inoremap <silent><expr> <C-u> coc#pum#visible() ? coc#pum#scroll(0) : "\<C-u>"coc#pum#info()
" Returns: dictionaryGet information about the current popup menu state.
Returns: Dictionary with keys:
index: Selected item index (-1 if none)scrollbar: Whether scrollbar is shownrow: Row positioncol: Column positionwidth: Menu widthheight: Menu heightsize: Total number of itemsinserted: Whether text was insertedExample:
let info = coc#pum#info()
echo 'Selected: ' . info.index . '/' . info.sizecoc#pum#select_confirm()Select first item if none selected, then confirm.
Example:
inoremap <silent><expr> <CR> coc#pum#visible() ? coc#pum#select_confirm() : "\<CR>"coc#pum#close_detail()Close the completion detail window.
coc#pum#winid()
" Returns: numberGet the window ID of the popup menu.
Returns: Window ID or 0 if not visible.
coc#pum#close([...])Close the popup menu.
coc#pum#create(lines, opt, config)
" Returns: dictionaryCreate a custom popup menu. Internal API for advanced use.
Parameters:
lines: List of menu itemsopt: Options dictionaryconfig: Configuration dictionaryCocAction('startCompletion', options)Start completion with specific options.
Parameters:
options: Dictionary with completion optionsExample:
call CocAction('startCompletion', {'source': 'word'})CocAction('refreshSource', [source])Refresh a specific completion source or all sources.
Parameters:
source: Optional source name to refreshExample:
" Refresh all sources
call CocAction('refreshSource')
" Refresh specific source
call CocAction('refreshSource', 'file')CocAction('sourceStat')
" Returns: listGet statistics for all completion sources.
Returns: List of source stat objects with name, priority, filepath, etc.
Example:
let sources = CocAction('sourceStat')
for source in sources
echo source.name . ' - ' . source.priority
endforCocAction('toggleSource', source)Toggle a completion source on or off.
Parameters:
source: Source name to toggleExample:
call CocAction('toggleSource', 'emoji')let g:coc_snippet_next = '<C-j>'Key for jumping to next snippet placeholder.
Type: String
Default: '<C-j>'
let g:coc_snippet_prev = '<C-k>'Key for jumping to previous snippet placeholder.
Type: String
Default: '<C-k>'
let g:coc_selectmode_mapping = 1Enable select mode mappings for snippet placeholders.
Type: Number
Default: 1
let b:coc_suggest_disable = 1Disable completion for the current buffer.
Type: Number
Default: 0
Example:
" Disable completion for certain filetypes
autocmd FileType markdown let b:coc_suggest_disable = 1let b:coc_disabled_sources = ['buffer', 'around']List of disabled completion sources for the current buffer.
Type: List
Default: []
Example:
" Disable word sources in large files
autocmd BufEnter * if line('$') > 10000 | let b:coc_disabled_sources = ['buffer', 'around'] | endiflet b:coc_suggest_blacklist = ['define', 'import']Words that won't trigger completion.
Type: List
Default: []
Example:
let b:coc_suggest_blacklist = ['TODO', 'FIXME', 'NOTE']let b:coc_additional_keywords = ['-', '@']Additional characters treated as part of keywords for completion.
Type: List
Default: []
Example:
autocmd FileType css let b:coc_additional_keywords = ['-']coc#util#do_complete(name, opt, cb)Trigger completion for testing or custom sources.
Parameters:
name: Source nameopt: Options dictionarycb: Callback functioncoc#util#suggest_variables(bufnr)
" Returns: dictionaryGet suggest-related variables for a buffer.
Parameters:
bufnr: Buffer numberReturns: Dictionary of suggest variables.
Common completion settings in coc-settings.json:
{
"suggest.autoTrigger": "always",
"suggest.noselect": true,
"suggest.enablePreselect": false,
"suggest.enablePreview": true,
"suggest.maxCompleteItemCount": 20,
"suggest.timeout": 5000,
"suggest.snippetIndicator": " ➜",
"suggest.minTriggerInputLength": 1,
"suggest.triggerAfterInsertEnter": false,
"suggest.acceptSuggestionOnCommitCharacter": true,
"suggest.labelMaxLength": 60
}" Tab for trigger completion and navigate
inoremap <silent><expr> <TAB>
\ coc#pum#visible() ? coc#pum#next(1) :
\ CheckBackspace() ? "\<Tab>" :
\ coc#refresh()
inoremap <expr><S-TAB> coc#pum#visible() ? coc#pum#prev(1) : "\<C-h>"
function! CheckBackspace() abort
let col = col('.') - 1
return !col || getline('.')[col - 1] =~# '\s'
endfunction
" Enter to confirm
inoremap <silent><expr> <CR> coc#pum#visible() ? coc#pum#confirm()
\ : "\<C-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"
" Ctrl-Space to trigger completion
inoremap <silent><expr> <c-space> coc#refresh()
" Scroll in popup
inoremap <silent><expr> <C-f> coc#pum#visible() ? coc#pum#scroll(1) : "\<Right>"
inoremap <silent><expr> <C-b> coc#pum#visible() ? coc#pum#scroll(0) : "\<Left>"" Use <Tab> for expand and jump
inoremap <silent><expr> <TAB>
\ coc#expandableOrJumpable() ? "\<C-r>=coc#snippet#next()\<CR>" :
\ coc#pum#visible() ? coc#pum#next(1) :
\ CheckBackspace() ? "\<TAB>" :
\ coc#refresh()
inoremap <silent><expr> <S-TAB>
\ coc#pum#visible() ? coc#pum#prev(1) :
\ "\<C-h>"
" Check if expandable or jumpable
function! s:check_snippet() abort
if coc#expandable()
return "expandable"
elseif coc#jumpable()
return "jumpable"
else
return "none"
endif
endfunction" File path completion
inoremap <silent><expr> <c-f> coc#start({'source': 'file'})
" Word completion
inoremap <silent><expr> <c-w> coc#start({'source': 'word'})
" Emoji completion
inoremap <silent><expr> <c-e> coc#start({'source': 'emoji'})" Disable in large files
autocmd BufEnter * call s:DisableInLargeFile()
function! s:DisableInLargeFile() abort
let size = line('$')
if size > 10000
let b:coc_suggest_disable = 1
echo "Completion disabled in large file"
endif
endfunction
" Disable in certain filetypes
autocmd FileType markdown,text let b:coc_suggest_disable = 1" Show available sources
function! ShowCompletionSources() abort
let sources = CocAction('sourceStat')
echo "Completion Sources:\n"
for source in sources
let status = source.disabled ? '✗' : '✓'
echo status . ' ' . source.name . ' (priority: ' . source.priority . ')'
endfor
endfunction
command! CocSources call ShowCompletionSources()
" Toggle specific source
nnoremap <silent> <leader>ts :call CocAction('toggleSource', 'buffer')<CR>function! ShowCompletionInfo() abort
if !coc#pum#visible()
echo "No completion menu"
return
endif
let info = coc#pum#info()
echo "Completion Menu Info:"
echo "Selected: " . (info.index + 1) . "/" . info.size
echo "Position: row=" . info.row . " col=" . info.col
echo "Size: " . info.width . "x" . info.height
echo "Scrollbar: " . (info.scrollbar ? "yes" : "no")
endfunction
nnoremap <silent> <leader>ci :call ShowCompletionInfo()<CR>Problem: Completion menu doesn't appear when typing.
Solutions:
" Check if completion is disabled
if get(b:, 'coc_suggest_disable', 0)
echo "Completion is disabled for this buffer"
endif
" Check if source is available
let sources = CocAction('sourceStat')
if empty(sources)
echo "No completion sources available"
endif
" Verify LSP is running
:CocCommand workspace.showOutputProblem: Specific completion source fails or is unavailable.
Solutions:
" Refresh problematic source
call CocAction('refreshSource', 'buffer')
" Disable failing source
let b:coc_disabled_sources = ['problematic-source']
" Check source status
let sources = CocAction('sourceStat')
for source in sources
if source.disabled
echo source.name . ' is disabled: ' . source.reason
endif
endforProblem: Completion takes too long to appear or causes lag.
Solutions:
"suggest.timeout": 10000let b:coc_suggest_disable = 1"suggest.maxCompleteItemCount": 10"suggest.enablePreview": falseProblem: Completion triggers when not wanted or doesn't trigger when needed.
Solutions:
"suggest.autoTrigger": "trigger" (instead of "always")"suggest.minTriggerInputLength": 2"suggest.triggerAfterInsertEnter": falselet b:coc_suggest_blacklist = ['TODO', 'FIXME']Problem: Tab key doesn't work as expected with snippets.
Solutions:
" Proper Tab/snippet integration
inoremap <silent><expr> <TAB>
\ coc#expandableOrJumpable() ? "\<C-r>=coc#snippet#next()\<CR>" :
\ coc#pum#visible() ? coc#pum#next(1) :
\ CheckBackspace() ? "\<TAB>" :
\ coc#refresh()
" Check backspace helper
function! CheckBackspace() abort
let col = col('.') - 1
return !col || getline('.')[col - 1] =~# '\s'
endfunctionProblem: Expected completion sources don't show results.
Solutions:
:CocCommand then look for source:CocInfo:set filetype?call CocAction('refreshSource')