or run

tessl search
Log in

Version

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/coc.nvim@0.0.x

docs

index.md
tile.json

tessl/npm-coc-nvim

tessl install tessl/npm-coc-nvim@0.0.0

LSP based intellisense engine for neovim & vim8.

completion.mddocs/intelligence/

Code Completion [Basic]

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.

Table of Contents

  • Common Tasks
  • Core Functions
  • Popup Menu Functions
  • CocAction API
  • Global Variables
  • Buffer Variables
  • Utility Functions
  • Configuration
  • Usage Examples
  • Error Handling
  • Troubleshooting
  • See Also

Common Tasks

Trigger Completion Manually

inoremap <silent><expr> <c-space> coc#start()

Trigger completion at cursor position.

Navigate Completion Menu

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.

Confirm Selection

inoremap <silent><expr> <CR> coc#pum#visible() ? coc#pum#confirm() : "\<CR>"

Accept the selected completion item.

Expand Snippet

inoremap <silent><expr> <TAB>
  \ coc#expandableOrJumpable() ? "\<C-r>=coc#snippet#next()\<CR>" : "\<TAB>"

Expand snippet or jump to next placeholder.

Core Functions

coc#start()

coc#start([{options}])
" Returns: string

Trigger completion manually with optional configuration.

Parameters:

  • options: Optional dictionary with completion options:
    • source: Specific completion source to trigger
    • Any other LSP CompletionOptions

Returns: 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()

coc#refresh()
" Returns: string

Start or refresh completion at cursor position.

Returns: Key sequence string for insert mode mapping.

Example:

inoremap <silent><expr> <c-n> coc#refresh()

coc#expandable()

coc#expandable()
" Returns: boolean

Check 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()

coc#jumpable()
" Returns: boolean

Check if can jump to the next snippet placeholder.

Returns: 1 if jumpable, 0 otherwise.

Example:

let g:coc_snippet_next = '<tab>'

coc#expandableOrJumpable()

coc#expandableOrJumpable()
" Returns: boolean

Check 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()

coc#_select_confirm()
" Returns: string

Select 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()

coc#on_enter()
" Returns: string

Notify 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()

coc#complete_indent()

Trigger auto-indent after completion. Used internally.

Popup Menu Functions

coc#pum#visible()

coc#pum#visible()
" Returns: boolean

Check if the completion popup menu is currently visible.

Returns: 1 if visible, 0 otherwise.

Example:

if coc#pum#visible()
  echo "Completion menu is open"
endif

coc#pum#next()

coc#pum#next(insert)
" Returns: string

Select the next item in the popup menu.

Parameters:

  • insert: If 1, insert mode behavior; if 0, selection only

Returns: Key sequence string.

Example:

inoremap <silent><expr> <TAB> coc#pum#visible() ? coc#pum#next(1) : "\<TAB>"

coc#pum#prev()

coc#pum#prev(insert)
" Returns: string

Select the previous item in the popup menu.

Parameters:

  • insert: If 1, insert mode behavior; if 0, selection only

Returns: Key sequence string.

Example:

inoremap <silent><expr> <S-TAB> coc#pum#visible() ? coc#pum#prev(1) : "\<S-TAB>"

coc#pum#select()

coc#pum#select(index, insert, confirm)
" Returns: string

Select a specific item by index in the popup menu.

Parameters:

  • index: Zero-based index of item to select
  • insert: If 1, insert mode behavior
  • confirm: If 1, confirm selection immediately

Returns: Key sequence string.

Example:

" Select first item
inoremap <silent><expr> <c-1> coc#pum#select(0, 1, 1)

coc#pum#confirm()

coc#pum#confirm()
" Returns: string

Confirm the selected completion item.

Returns: Key sequence string.

Example:

inoremap <silent><expr> <CR> coc#pum#visible() ? coc#pum#confirm() : "\<CR>"

coc#pum#cancel()

coc#pum#cancel()
" Returns: string

Cancel completion and restore original text.

Returns: Key sequence string.

Example:

inoremap <silent><expr> <Esc> coc#pum#visible() ? coc#pum#cancel() : "\<Esc>"

coc#pum#stop()

coc#pum#stop()
" Returns: string

Close the popup menu without restoring original text.

Returns: Key sequence string.

Example:

inoremap <silent><expr> <c-e> coc#pum#stop()

coc#pum#insert()

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()

coc#pum#scroll(forward)
" Returns: string

Scroll the popup menu or detail window.

Parameters:

  • forward: If 1, scroll down; if 0, scroll up

Returns: 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()

coc#pum#info()
" Returns: dictionary

Get information about the current popup menu state.

Returns: Dictionary with keys:

  • index: Selected item index (-1 if none)
  • scrollbar: Whether scrollbar is shown
  • row: Row position
  • col: Column position
  • width: Menu width
  • height: Menu height
  • size: Total number of items
  • inserted: Whether text was inserted

Example:

let info = coc#pum#info()
echo 'Selected: ' . info.index . '/' . info.size

coc#pum#select_confirm()

coc#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()

coc#pum#close_detail()

Close the completion detail window.

coc#pum#winid()

coc#pum#winid()
" Returns: number

Get the window ID of the popup menu.

Returns: Window ID or 0 if not visible.

coc#pum#close()

coc#pum#close([...])

Close the popup menu.

coc#pum#create()

coc#pum#create(lines, opt, config)
" Returns: dictionary

Create a custom popup menu. Internal API for advanced use.

Parameters:

  • lines: List of menu items
  • opt: Options dictionary
  • config: Configuration dictionary

CocAction API

startCompletion

CocAction('startCompletion', options)

Start completion with specific options.

Parameters:

  • options: Dictionary with completion options

Example:

call CocAction('startCompletion', {'source': 'word'})

refreshSource

CocAction('refreshSource', [source])

Refresh a specific completion source or all sources.

Parameters:

  • source: Optional source name to refresh

Example:

" Refresh all sources
call CocAction('refreshSource')

" Refresh specific source
call CocAction('refreshSource', 'file')

sourceStat

CocAction('sourceStat')
" Returns: list

Get 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
endfor

toggleSource

CocAction('toggleSource', source)

Toggle a completion source on or off.

Parameters:

  • source: Source name to toggle

Example:

call CocAction('toggleSource', 'emoji')

Global Variables

g:coc_snippet_next

let g:coc_snippet_next = '<C-j>'

Key for jumping to next snippet placeholder.

Type: String Default: '<C-j>'

g:coc_snippet_prev

let g:coc_snippet_prev = '<C-k>'

Key for jumping to previous snippet placeholder.

Type: String Default: '<C-k>'

g:coc_selectmode_mapping

let g:coc_selectmode_mapping = 1

Enable select mode mappings for snippet placeholders.

Type: Number Default: 1

Buffer Variables

b:coc_suggest_disable

let b:coc_suggest_disable = 1

Disable completion for the current buffer.

Type: Number Default: 0

Example:

" Disable completion for certain filetypes
autocmd FileType markdown let b:coc_suggest_disable = 1

b:coc_disabled_sources

let 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'] | endif

b:coc_suggest_blacklist

let b:coc_suggest_blacklist = ['define', 'import']

Words that won't trigger completion.

Type: List Default: []

Example:

let b:coc_suggest_blacklist = ['TODO', 'FIXME', 'NOTE']

b:coc_additional_keywords

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 = ['-']

Utility Functions

coc#util#do_complete()

coc#util#do_complete(name, opt, cb)

Trigger completion for testing or custom sources.

Parameters:

  • name: Source name
  • opt: Options dictionary
  • cb: Callback function

coc#util#suggest_variables()

coc#util#suggest_variables(bufnr)
" Returns: dictionary

Get suggest-related variables for a buffer.

Parameters:

  • bufnr: Buffer number

Returns: Dictionary of suggest variables.

Configuration

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
}

Usage Examples

Complete Completion Setup

" 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>"

Snippet Integration

" 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

Custom Source Trigger

" 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 Completion Conditionally

" 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

Source Management

" 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>

Advanced Completion Info

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>

Error Handling

No Completion Suggestions

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.showOutput

Completion Source Errors

Problem: 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
endfor

Troubleshooting

Completion is too slow

Problem: Completion takes too long to appear or causes lag.

Solutions:

  1. Increase timeout: "suggest.timeout": 10000
  2. Disable in large files: let b:coc_suggest_disable = 1
  3. Reduce max items: "suggest.maxCompleteItemCount": 10
  4. Disable preview: "suggest.enablePreview": false

Completion menu appears at wrong time

Problem: Completion triggers when not wanted or doesn't trigger when needed.

Solutions:

  1. Adjust trigger mode: "suggest.autoTrigger": "trigger" (instead of "always")
  2. Set minimum length: "suggest.minTriggerInputLength": 2
  3. Disable after insert: "suggest.triggerAfterInsertEnter": false
  4. Use blacklist: let b:coc_suggest_blacklist = ['TODO', 'FIXME']

Snippet expansion conflicts with Tab

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'
endfunction

Sources not providing completions

Problem: Expected completion sources don't show results.

Solutions:

  1. Check source is enabled: :CocCommand then look for source
  2. Verify language server is running: :CocInfo
  3. Check file type is supported: :set filetype?
  4. Refresh source: call CocAction('refreshSource')

See Also

  • Diagnostics - Error and warning management
  • Code Actions - Quick fixes and refactoring
  • Hover and Signatures - Documentation display
  • Snippets - Snippet expansion and management
  • Configuration - Settings and options