tessl install tessl/npm-coc-nvim@0.0.0LSP based intellisense engine for neovim & vim8.
Complexity: Basic | Category: Intelligence
Common Tasks: Apply quick fixes | Rename symbols | Organize imports | Refactor code
Apply code actions, quick fixes, refactorings, and symbol renaming provided by language servers.
nmap <leader>qf <Plug>(coc-fix-current)Apply first available quick fix for current line.
nmap <leader>ac <Plug>(coc-codeaction-cursor)Show available code actions for cursor position.
nmap <leader>rn <Plug>(coc-rename)Rename symbol at cursor across workspace.
:call CocAction('organizeImport')Organize and sort imports in current file.
<Plug>(coc-codeaction)Show code actions available for the current file or buffer.
Example:
nmap <leader>a <Plug>(coc-codeaction)<Plug>(coc-codeaction-line)Show code actions for the current line.
Example:
nmap <leader>al <Plug>(coc-codeaction-line)<Plug>(coc-codeaction-cursor)Show code actions for cursor position only.
Example:
nmap <leader>ac <Plug>(coc-codeaction-cursor)<Plug>(coc-codeaction-selected)Code action for selected range. Works as operator in normal mode or with visual selection.
Example:
nmap <leader>a <Plug>(coc-codeaction-selected)
xmap <leader>a <Plug>(coc-codeaction-selected)<Plug>(coc-fix-current)Apply first available quick fix for current line.
Example:
nmap <leader>qf <Plug>(coc-fix-current)<Plug>(coc-rename)Rename symbol at cursor position across workspace.
Example:
nmap <leader>rn <Plug>(coc-rename)<Plug>(coc-refactor)Open refactor window for advanced refactoring operations.
Example:
nmap <leader>rf <Plug>(coc-refactor)<Plug>(coc-codelens-action)Invoke code lens action at cursor position.
Example:
nmap <leader>cl <Plug>(coc-codelens-action)CocAction('codeAction', [mode], [only])Get and apply code actions.
Parameters:
mode: Scope mode - '' (buffer), 'line', 'cursor', or visual mode ('v', 'V', 'x')only: Optional array of CodeActionKind to filter (e.g., ['quickfix'], ['refactor'])Example:
" All buffer actions
call CocAction('codeAction', '')
" Line actions
call CocAction('codeAction', 'line')
" Cursor actions
call CocAction('codeAction', 'cursor')
" Only quickfix actions
call CocAction('codeAction', 'cursor', ['quickfix'])
" Only refactor actions
call CocAction('codeAction', 'line', ['refactor'])CocAction('codeActions', [mode], [only])
" Returns: listGet available code actions without applying.
Parameters:
mode: Scope modeonly: Optional filter by CodeActionKindReturns: List of code action objects.
Example:
let actions = CocAction('codeActions', 'cursor')
echo 'Available actions: ' . len(actions)
for action in actions
echo action.title
endforCocAction('codeActionRange', start, end, [only])Get code actions for specific line range.
Parameters:
start: Start line number (0-based)end: End line number (0-based)only: Optional CodeActionKind filterExample:
" Actions for lines 10-20
call CocAction('codeActionRange', 9, 19)
" Only refactor actions for range
call CocAction('codeActionRange', 9, 19, ['refactor'])CocAction('doCodeAction', action)Apply a specific code action object.
Parameters:
action: Code action object from codeActions()Example:
let actions = CocAction('codeActions', 'cursor')
if len(actions) > 0
call CocAction('doCodeAction', actions[0])
endifCocAction('doQuickfix')Apply first available quickfix action for current line.
Example:
nnoremap <silent> <leader>qf :call CocAction('doQuickfix')<CR>CocAction('quickfixes', [mode])
" Returns: listGet available quickfix actions.
Parameters:
mode: Optional scope modeReturns: List of quickfix action objects.
Example:
let fixes = CocAction('quickfixes', 'cursor')
echo len(fixes) . ' quickfixes available'CocAction('organizeImport')Organize and sort imports for current file.
Example:
nnoremap <silent> <leader>oi :call CocAction('organizeImport')<CR>
" Auto-organize on save
autocmd BufWritePre *.ts,*.tsx call CocAction('organizeImport')CocAction('fixAll')Apply all available auto-fixes for current buffer.
Example:
nnoremap <silent> <leader>fa :call CocAction('fixAll')<CR>CocAction('rename')Trigger rename operation for symbol at cursor.
Example:
nnoremap <silent> <leader>rn :call CocAction('rename')<CR>CocAction('refactor')Open refactor window showing available refactoring operations.
Example:
nnoremap <silent> <leader>re :call CocAction('refactor')<CR>CocAction('codeLensAction')Execute code lens action at cursor position.
Example:
nnoremap <silent> <leader>cl :call CocAction('codeLensAction')<CR>Code action settings in coc-settings.json:
{
"coc.preferences.enableCodeActions": true,
"codeAction.disabledSupport": false,
"codeAction.showDocumentation": true,
"codeLens.enable": true,
"codeLens.separator": "‣"
}CocCodeLensHighlight group for code lens virtual text.
Example:
highlight CocCodeLens ctermfg=Gray guifg=#999999" Code actions
nmap <leader>ac <Plug>(coc-codeaction-cursor)
nmap <leader>al <Plug>(coc-codeaction-line)
nmap <leader>as <Plug>(coc-codeaction-selected)
xmap <leader>a <Plug>(coc-codeaction-selected)
" Quick fix
nmap <leader>qf <Plug>(coc-fix-current)
" Rename
nmap <leader>rn <Plug>(coc-rename)
" Refactor
nmap <leader>rf <Plug>(coc-refactor)
" Code lens
nmap <leader>cl <Plug>(coc-codelens-action)" Apply quickfix on save
function! s:cocAutoFix() abort
call CocAction('doQuickfix')
endfunction
autocmd BufWritePre *.ts,*.tsx,*.js,*.jsx call s:cocAutoFix()
" Organize imports on save
autocmd BufWritePre *.ts,*.tsx,*.go call CocAction('organizeImport')
" Fix all on save
autocmd BufWritePre *.ts,*.tsx call CocAction('fixAll')" Show code actions with preview
function! ShowCodeActions() abort
let actions = CocAction('codeActions', 'cursor')
if len(actions) == 0
echo "No code actions available"
return
endif
" Build menu items
let items = []
for i in range(len(actions))
call add(items, (i + 1) . '. ' . actions[i].title)
endfor
" Show menu and apply selected action
let choice = inputlist(['Select action:'] + items)
if choice > 0 && choice <= len(actions)
call CocAction('doCodeAction', actions[choice - 1])
endif
endfunction
nnoremap <silent> <leader>ca :call ShowCodeActions()<CR>" Show only quickfix actions
function! ShowQuickFixes() abort
let fixes = CocAction('quickfixes', 'cursor')
if len(fixes) == 0
echo "No quickfixes available"
return
endif
echo len(fixes) . " quickfix(es) available"
call CocAction('codeAction', 'cursor', ['quickfix'])
endfunction
nnoremap <silent> <leader>qx :call ShowQuickFixes()<CR>
" Show only refactor actions
nnoremap <silent> <leader>rr :call CocAction('codeAction', 'cursor', ['refactor'])<CR>" Rename with confirmation
function! SmartRename() abort
let current = expand('<cword>')
let new_name = input('Rename "' . current . '" to: ')
if empty(new_name) || new_name == current
echo " Cancelled"
return
endif
call CocAction('rename')
endfunction
nnoremap <silent> <leader>rn :call SmartRename()<CR>function! OrganizeImports() abort
echo "Organizing imports..."
call CocAction('organizeImport')
echo "Imports organized"
endfunction
nnoremap <silent> <leader>oi :call OrganizeImports()<CR>" Preview what actions are available
function! PreviewCodeActions() abort
let actions = CocAction('codeActions', 'line')
echo "=== Code Actions ==="
if len(actions) == 0
echo "No actions available"
return
endif
for action in actions
echo "• " . action.title
if has_key(action, 'kind')
echo " Kind: " . action.kind
endif
endfor
endfunction
nnoremap <silent> <leader>ap :call PreviewCodeActions()<CR>" Fix all issues in file
function! FixAllIssues() abort
echo "Applying all fixes..."
" Apply quickfixes
call CocAction('fixAll')
" Organize imports
call CocAction('organizeImport')
" Format
call CocAction('format')
echo "All fixes applied"
endfunction
command! FixAll call FixAllIssues()
nnoremap <silent> <leader>fix :FixAll<CR>function! RefactorMenu() abort
let actions = CocAction('codeActions', 'cursor', ['refactor'])
if len(actions) == 0
echo "No refactoring actions available"
return
endif
let menu = ['Available refactorings:']
for i in range(len(actions))
call add(menu, (i + 1) . '. ' . actions[i].title)
endfor
let choice = inputlist(menu)
if choice > 0 && choice <= len(actions)
call CocAction('doCodeAction', actions[choice - 1])
echo "Applied: " . actions[choice - 1].title
endif
endfunction
nnoremap <silent> <leader>ref :call RefactorMenu()<CR>" Auto-update code lens
autocmd CursorHold,CursorHoldI * silent call CocActionAsync('codeLensAction')
" Toggle code lens
function! ToggleCodeLens() abort
if get(g:, 'coc_codelens_enable', 1)
let g:coc_codelens_enable = 0
echo "Code lens disabled"
else
let g:coc_codelens_enable = 1
echo "Code lens enabled"
endif
endfunction
nnoremap <silent> <leader>tl :call ToggleCodeLens()<CR>" Apply code action to function
function! CodeActionForFunction() abort
" Get function range
normal! [m
let start = line('.')
normal! ]M
let end = line('.')
call CocAction('codeActionRange', start - 1, end - 1)
endfunction
nnoremap <silent> <leader>af :call CodeActionForFunction()<CR>
" Apply to visual selection
xnoremap <silent> <leader>a :call CocAction('codeAction', visualmode())<CR>" Apply multiple quickfixes
function! QuickFixMultiple(count) abort
for i in range(a:count)
call CocAction('doQuickfix')
endfor
echo a:count . " quickfixes applied"
endfunction
nnoremap <silent> <leader>qf :<C-u>call QuickFixMultiple(v:count1)<CR>" Source-specific actions
function! SourceActions() abort
" Source actions are typically file-level
let actions = CocAction('codeActions', '', ['source'])
if len(actions) == 0
echo "No source actions available"
return
endif
for action in actions
echo "• " . action.title
endfor
call CocAction('codeAction', '', ['source'])
endfunction
nnoremap <silent> <leader>sa :call SourceActions()<CR>function! CodeActionStats() abort
let all_actions = CocAction('codeActions', '')
let quickfixes = CocAction('codeActions', '', ['quickfix'])
let refactors = CocAction('codeActions', '', ['refactor'])
echo "=== Code Action Statistics ==="
echo "Total actions: " . len(all_actions)
echo "Quick fixes: " . len(quickfixes)
echo "Refactorings: " . len(refactors)
endfunction
command! CodeActionStats call CodeActionStats()Problem: Code action command shows no available actions.
Solutions:
" Check if language server supports code actions
if CocHasProvider('codeAction')
echo "Code actions supported"
else
echo "No code action provider"
endif
" Verify cursor is on valid symbol
echo expand('<cword>')
" Check language server status
:CocInfoProblem: Symbol rename operation fails or doesn't affect all occurrences.
Solutions:
" Ensure file is saved
:w
" Check if symbol is renameable
let hover = CocAction('getHover')
echo hover
" Restart language server
:CocRestart
" Check workspace folders are configured
:CocList foldersProblem: Quick fix command doesn't change the code.
Solutions:
" Check available quick fixes
let fixes = CocAction('quickfixes', 'cursor')
echo 'Available fixes: ' . len(fixes)
" Try showing all code actions
call CocAction('codeAction', 'cursor')
" Manually apply with selection
nmap <leader>ca <Plug>(coc-codeaction-cursor)Problem: Long delay when requesting code actions.
Solutions:
:CocCommand workspace.showOutputcursor mode instead of buffer-wide actionsProblem: Organize imports command has no effect.
Solutions:
if CocHasProvider('organizeImport')
echo "Supported"
endif:set filetype?:wProblem: Rename operation changes unintended occurrences.
Solutions:
:CocSearch <symbol><Plug>(coc-refactor)Problem: Code lens actions don't appear above code.
Solutions:
"codeLens.enable": true:echo has('nvim-0.5'):CocCommand workspace.showOutput:CocCommand document.refresh