Skip to content

Using coc extensions

NullVoxPopuli edited this page May 5, 2022 · 180 revisions

Why are coc extensions needed?

The main reason is that some language servers provided by the community not well behaved compared to the extensions of VSCode. Coc extensions can be forked from VSCode extensions providing a better user experience.

Compared to configured language servers, extensions have more features.

  • Extensions can contribute properties to the schema coc-settings.json, like in VSCode you can write the configuration with completion and validation support when you have coc-json installed.

    Screen Shot 2019-06-26 at 3 22 05 PM
  • Extensions can contribute commands (like VSCode), you can use the coc commands in different ways:

    • Use the command :CocList commands to open the command list and choose one you need.

      screen shot 2018-09-07 at 4 53 12 pm
    • Use :CocCommand with <tab> for command line completion.

    • An example config to use the custom command Tsc for tsserver.watchBuild:

      command! -nargs=0 Tsc    :CocCommand tsserver.watchBuild
  • Extensions can contribute json schemas (loaded by coc-json)

  • Extensions can contribute snippets that can be loaded by coc-snippets extension.

  • Extensions can specify more client options, like fileEvents to watch files (requires watchman installed), and middleware which can be used to fix results that return from the language server.

For a deeper dive into the purpose and implementation of coc extensions, please see How to write a coc.nvim extension.

Differences between coc extensions and VSCode extensions

  • Coc extensions use coc.nvim as a dependency instead of VSCode
  • Coc extensions support language server features by using the API from coc.nvim instead of vscode-languageclient which can only be used with VSCode.
  • Coc extensions support some features of VSCode extensions:
    • activate and deactivate api.
    • activationEvents in package.json.
    • Configuration support: contributes.configuration in package.json.
    • Commands support: contributes.commands.
    • JSON validation support via JSON Schema: contributes.jsonValidation.
    • Snippets support.

Manage coc extensions

Single file extensions

Coc.nvim will try to load javascript files in the folder $VIMCONFIG/coc-extensions, each javascript file should be extension of coc.nvim.

An example coc extension that convert character to unicode code point at cursor position.

const { commands, workspace } = require('coc.nvim')

exports.activate = context => {
  let { nvim } = workspace
  context.subscriptions.push(commands.registerCommand('code.convertCodePoint', async () => {
    let [pos, line] = await nvim.eval('[coc#util#cursor(), getline(".")]')
    let curr = pos[1] == 0 ? '' : line.slice(pos[1], pos[1] + 1)
    let code = curr.codePointAt(0)
    let str = code.toString(16)
    str = str.length == 4 ? str : '0'.repeat(4 - str.length) + str
    let result = `${line.slice(0, pos[1])}${'\\u' + str}${line.slice(pos[1] + 1)}`
    await'setline', ['.', result])

Note: it's not possible to use package.json to add additional contributions for single file extensions.

Install extensions

Using :CocInstall:

:CocInstall coc-json coc-css

One or more extension names can be provided.

Note: VSCode extensions can't be used by coc.nvim for now.

Extensions will be loaded and activated after the install succeeds.

Note you can add extension names to the g:coc_global_extensions variable, and coc will install the missing extensions after coc.nvim service started. For example:

let g:coc_global_extensions = ['coc-json', 'coc-git']

To install extensions with shell script, use command like:

# install coc-json & coc-html and exit
vim -c 'CocInstall -sync coc-json coc-html|q'

Using custom registry

You can customize npm registry for coc.nvim by add coc.nvim:registry in the file ~/.npmrc:


Installing specific versions (for rollback/revert/etc)

If you need to rollback/revert to a specific extension version, or you just want to install a specific version regardless, you can add @version to your install command.

Using coc-prettier as an example, in order to install the 1.1.17 version, you type:

:CocInstall coc-prettier@1.1.17

Use vim's plugin manager for coc extension

You can manage coc extension by using a plugin manager for vim, like vim-plug, coc will try to load coc extensions from your &rtp

Example for coc-tsserver:

Plug 'neoclide/coc-tsserver', {'do': 'yarn install --frozen-lockfile'}

After adding this to your vimrc run PlugInstall.

Note: For coc extensions written with typescript, you have to build them when using git, most of time you should install yarn and use yarn install --frozen-lockfile in extension root.

This has the limitation that you can't uninstall the extension by using :CocUninstall and that automatic update support is not available.

Update extensions

Use the command :CocUpdate or :CocUpdateSync to update extensions installed by :CocInstall to the latest version.

For extensions loaded from vim's rtp, you will need update them by your plugin manager.

To enable automatic update, change configuration coc.preferences.extensionUpdateCheck to "daily" or "weekly", which defaults to "never".

To upgrade extensions with shell script, use command like:

vim -c 'CocUpdateSync|q'

Uninstall coc extension

Use vim command CocUninstall for extensions installed by :CocInstall, for example:

:CocUninstall coc-css

Manage extensions with CocList

Run command:

:CocList extensions

to open CocList buffer, which looks like:

screen shot 2018-09-10 at 10 28 06 pm
  • ? means invalid extension
  • * means extension is activated
  • + means extension is loaded
  • - means extension is disabled

Supported actions (hit <Tab> to activate action menu):

  • toggle default action. activate/deactivate selected extension(s).
  • enable: enable selected extension(s).
  • disable: disable selected extension(s).
  • reload: reload selected extension(s).
  • uninstall: remove selected extension(s).
  • lock: toggle lock of extension, locked extension won't be updated by :CocUpdate

Debug coc extension

If an extension throws uncaught errors, you can get the error message by: :messages.

For extensions using a language server, you can use the output channel. Check out

Use console for log messages to log file of coc.nvim, supported methods including debug log error info warn, checkout :h :CocOpenLog.

You can also use Chrome to debug extensions, checkout

Implemented coc extensions

You can find available coc extensions by searching coc.nvim on npm, or use coc-marketplace, which can search and install extensions in coc.nvim directly.

Tips: use :CocConfig to edit the configuration file. Completion & validation are supported after coc-json is installed.