Skip to content

Multiple cursors support

Qiming zhao edited this page Jul 26, 2019 · 17 revisions

By use multiple cursors feature, you can change many places of current document at one time.

Multiple cursors is actually multiple text ranges, which are highlighted by CocCursorRange highlight group (linked to Search by default).

Start multiple cursors session

There're some key-mappings for start multiple cursors:

  • <Plug>(coc-cursors-position) add current character range to cursors.
  • <Plug>(coc-cursors-word) add current word range to cursors.
  • <Plug>(coc-cursors-range) add current visual selected range to cursors.
  • <Plug>(coc-cursors-operator) use operator for add range to cursors.

Example usage:

nmap <silent> <C-c> <Plug>(coc-cursors-position)
nmap <silent> <C-d> <Plug>(coc-cursors-word)
xmap <silent> <C-d> <Plug>(coc-cursors-range)
" use normal command like `<leader>xi(`
nmap <leader>x  <Plug>(coc-cursors-operator)

Or use internal command API to add ranges, like:

import {commands} from 'coc.nvim'
// hlRanges is a list of LSP Range
await commands.executeCommand('editor.action.addRanges', hlRanges)

Use vim script like:

call CocAction('runCommand', 'editor.action.addRanges', hlRanges)

Rename current variable

One common task is rename the variable name under cursor.

Use command :CocCommand document.renameCurrentWord to start cursors session with ranges contain current word.

Note: the word are extracted by iskeyword option of current buffer.

Note: it's simply strict match of words, instead of use rename feature of language server,

Make changes for cursors

When cursors session is activated, you can change one placeholder to reflect changes for all cursor ranges.

  • You can change the text any way you want to, like normal command or insert text.
  • You can change around the text by command like ysiw" (with tpope/vim-surround installed).
  • You can undo & redo the changes.
  • You can't insert new line, it would cancel cursors session.

Jump between ranges of cursors

  • Use "cursors.nextKey" to jump to next position of cursors range, default to <C-n>.
  • Use "cursors.previousKey" to jump to previous position of cursors range, default to <C-p>.

The key-mappings would be created when cursors session activated, and remove on session cancelled.

Cancel cursors session

Use "cursors.cancelKey" to cancel cursors session, default to <Esc>.

The session would also be canceled when buffer change can't be applied for ranges or the cursor jump to other window.

Use refactor action

Use <Plug>(coc-refactor) for create a refactor window of current symbol.

Note: it requires rename feature of language server to work.

When the window get opened, related ranges would be added to a new cursors session, you can:

  • Change the placeholder for rename.
  • Save the buffer for synchronize changes to related buffers.
  • Add or remove lines.
  • Change content of related buffers, the changes would be synchronized to refactor buffer.
  • Press <CR> to open related buffer line on right split window.
  • Fold the ranges by command like zi.
Screen Shot 2019-07-26 at 7 11 52 PM

Use CocSearch command

For rename variable across files in current cwd, you can use :CocSearch command which requires ripgrep to work.

Each range of lines would be added to refactor window asynchronously and matched ranges would be added to cursors session for rename.

Tipe:

  • Use <tab> to get available options after type :CocSearch command.
  • Use :CocSearch -w [word] for word match.

REPL

Clone this wiki locally