Lua API
Call coc.nvim diagnostics, configuration, commands, symbols, and extension status from Neovim Lua with explicit blocking boundaries.
Lua API
The lua/coc/init.lua module exposes a small Neovim-only bridge to the coc.nvim Node.js process. Load it with require("coc"). This is not the TypeScript extension API and it is not available in Vim.
| 1 | local coc = require('coc') |
| 2 | |
| 3 | -- Read all diagnostics currently known to coc.nvim. |
| 4 | local diagnostics = coc.get_diagnostics() |
| 5 | |
| 6 | -- Execute a registered coc.nvim command. |
| 7 | coc.execute_command('editor.action.organizeImport') |
Available functions
| Function | Result |
|---|---|
get_diagnostics() | Diagnostics for all loaded documents. |
get_config(section) | The resolved configuration object for a section. |
execute_command(name, ...) | Result of a registered command invoked with optional arguments. |
workspace_symbols(query) | Workspace symbols matching the query. |
document_symbols([bufnr]) | Symbols for a buffer; uses the current buffer when omitted. |
command_list() | Registered command identifiers. |
extension_stats() | Loaded extensions and their current states. |
The bridge returns nil when the coc.nvim RPC channel is unavailable or the request fails. Check that coc.nvim has started and use :CocOpenLog when an expected result is missing.
Practical examples
| 1 | local coc = require('coc') |
| 2 | |
| 3 | vim.api.nvim_create_user_command('CocLuaCommands', function() |
| 4 | local commands = coc.command_list() or {} |
| 5 | vim.notify(table.concat(commands, "\n")) |
| 6 | end, {}) |
| 7 | |
| 8 | vim.api.nvim_create_user_command('CocLuaSymbols', function(opts) |
| 9 | local symbols = coc.workspace_symbols(opts.args) or {} |
| 10 | vim.notify(vim.inspect(symbols)) |
| 11 | end, { nargs = 1 }) |
Use get_config() with a section name rather than reading coc.nvim internals:
| 1 | local coc = require('coc') |
| 2 | local suggest = coc.get_config('suggest') or {} |
| 3 | vim.notify(vim.inspect(suggest)) |
Blocking behavior
Every function is synchronous and uses vim.rpcrequest. Neovim’s UI remains blocked until the Node.js process replies. execute_command(), workspace_symbols(), and document_symbols() may wait for a language server or a slow user command and have no timeout or cancellation.
- Do not call these functions from redraw callbacks, statusline evaluation, completion callbacks, or other latency-sensitive paths.
- Prefer an explicit user command or a separate coroutine for occasional calls.
- Use the asynchronous TypeScript API inside a coc.nvim extension when work is frequent, cancellable, or language-server dependent.
Source of truth: lua/coc/init.lua and :h coc-api-lua.