Vim icon indicating copy to clipboard operation
Vim copied to clipboard

README needs more information about Multi-Cursor mode for Vim users

Open Hubro opened this issue 5 years ago • 24 comments

I've been using VSCode for a couple of months after using Vim + vim-multiple-cursors for years. I'm still having way too hard a time using the Multi-Cursor mode in VSCode + Vim.

The only "official" information I've found about how to use it is in the plugin README, where it says I can use "gb" to select the next matching word. That still leaves me with many questions that I think should be addressed in the Multi-Cursor mode section of the README:

  • How do I skip selections (like vim-multiple-cursors Ctrl+x)
  • How do I undo a selection (like vim-multiple-cursors Ctrl+p)
  • How do I go from visual block mode to Multi-Cursor mode? For example, if I have visual block selection over 30 lines, is it possible to press a keyboard shortcut and convert that visual block selection to one cursor per line? I'm almost positive I've done this by accident a couple of times, but I can't figure out how I did it.
  • Is it possible to modify the keybinds to match vim-multiple-cursors (or other plugins) without breaking something? For example, can I rebind Ctrl+n to start Multi-Cursor mode without breaking the Ctrl+n keybind in other contexts, like selecting the next line in a auto-complete suggestion list.
  • How do I "Add Cursor Below" (or Above) without moving my hands away from the "Vim position" (to the arrow keys)
  • How do I "Add Cursor Below" x times? If I have 20-30 lines to select, I feel like I should be able to press 30<C-S-Down> to do that instantly, instead I have to move my right hand down to the arrow keys and spam down 30 times - very un-vim-like. Although I wouldn't ever need to use this keybind if I could go from visual block mode to Multi-Cursor mode.

I think adding some more information about how Multi-Cursor mode is used would help relatively new users like me a lot.

Hubro avatar Mar 14 '19 20:03 Hubro

@Hubro

if I could go from visual block mode to Multi-Cursor mode.

Does cmd+shift+L work for ya? (Add Cursors to Line Ends)

In general I find this plugins support for "vim and multi cursor" support a bit lacking... which is too bad since I would really like to combine my favourite features from Sublime Text with new tricks I'm learning from vim side.

elnygren avatar Mar 25 '19 22:03 elnygren

@Hubro

if I could go from visual block mode to Multi-Cursor mode.

Does cmd+shift+L work for ya? (Add Cursors to Line Ends)

In general I find this plugins support for "vim and multi cursor" support a bit lacking... which is too bad since I would really like to combine my favourite features from Sublime Text with new tricks I'm learning from vim side.

I'm on Linux so I don't have a cmd key, but I tried every combination of ctrl/shift/alt+L and unfortunately it didn't work. The cursor just jumped to the last character of the last selected line.

Hubro avatar Mar 25 '19 22:03 Hubro

@Hubro might also be SHIFT + ALT + I https://nickjanetakis.com/blog/insert-multiple-cursors-at-the-start-of-every-line-with-vscode

Search for "Add Cursors to Line Ends" in your settings

elnygren avatar Apr 04 '19 16:04 elnygren

@Hubro might also be SHIFT + ALT + I https://nickjanetakis.com/blog/insert-multiple-cursors-at-the-start-of-every-line-with-vscode

Search for "Add Cursors to Line Ends" in your settings

Well that did something :P

It adds a cursor to each line ending, but at the same time the Vim extension throws an error "ModeHandler: unexpected selection mode. selectionMode=4", and Vim mode stops working until i exit multi-cursor mode.

Hubro avatar Apr 05 '19 20:04 Hubro

  • How do I skip selections (like vim-multiple-cursors Ctrl+x)
  • How do I undo a selection (like vim-multiple-cursors Ctrl+p)

So is this possible or just lacks documentation?

sebenik avatar Aug 20 '19 14:08 sebenik

As as vs code noob, id really like more info about using vim mode with more common GUI editor features like multi line plugins.

j3zz3r avatar Jun 25 '20 14:06 j3zz3r

@Hubro might also be SHIFT + ALT + I https://nickjanetakis.com/blog/insert-multiple-cursors-at-the-start-of-every-line-with-vscode

Search for "Add Cursors to Line Ends" in your settings

I have to disable VSCodeVim for SHIFT + ALT + I to work

Oh. wait... I should have used I and A instead (https://github.com/VSCodeVim/Vim/issues/4677

haifengkao avatar Oct 12 '20 17:10 haifengkao

Have same issue. Could i customize the correspond Vim Extension?

jkryanchou avatar Nov 24 '20 07:11 jkryanchou

Just as a comment for others looking at this, I just figured out the way to do it is to select the lines I want to add cursors to using visual mode, then shift+i to add cursors to the beginning of the selected lines, and shift+alt+i to add cursors to the end of the selected lines.

fnurl avatar Mar 02 '22 11:03 fnurl

I've finally found a way to emulate terryma/vim-multiple-cursors in VSCode. I've been using Cygwin + Vim and the vim-multiple-cursors plugin for years and finally switched over to VSCode but couldn't live without this absolutely essential plugin. This was the only plugin that I couldn't find a direct replacement for from my old .vimrc file. After going through pages and pages of Google search results unable to find a solution, I decided to write my own. I wanted to emulate it EXACTLY as my old configuration. Here's my implementation although it is only about 95% the same since the vscodevim emulation plugin for VSCode has some bugs. In all my research, the same problem everyone is having is:

Any action carried out only operates on the first cursor instance

According to the VSCode basic edition command documentation, we can use the built-in commands to emulate the plugin. Here's the working code, insert this into your keybindings.json file

/*  VSCode attempt to emulate https://github.com/terryma/vim-multiple-cursors */

// Use built in VSCode pattern matcher for entire word search (ctrl + n)
{
  "key": "ctrl+n",
  "command": "editor.action.addSelectionToNextFindMatch",
  "when": "vim.active && editorFocus && vim.mode == 'Normal' || vim.mode == 'Visual' || vim.mode == 'VisualBlock' && !inDebugRepl"
},

// Skip current match (ctrl + x)
{
  "key": "ctrl+x",
  "command": "editor.action.moveSelectionToNextFindMatch",
  "when": "vim.active && editorFocus && vim.mode == 'Normal' || vim.mode == 'Visual' || vim.mode == 'VisualBlock' && !inDebugRepl"
},

// Go back to previous match (ctrl + p)
{
  "key": "ctrl+p",
  "command": "editor.action.moveSelectionToPreviousFindMatch",
  "when": "vim.active && editorFocus && vim.mode == 'Visual' || vim.mode == 'VisualBlock' && !inDebugRepl"
},

// Select all matches (ctrl + a). This is effectively the same as (ctrl + shift + l)
{
  "key": "ctrl+a",
  "command": "editor.action.selectHighlights",
  "when": "vim.active && editorFocus && vim.mode == 'Normal' || vim.mode == 'Visual' || vim.mode == 'VisualBlock' && !inDebugRepl"
}

USAGE

  1. Ctrl + n - Select single whole word/pattern matches . You can continuously do it again to select subsequent matches
  2. Ctrl + a - Select all word/pattern matches. This is essentially the same as VSCode's ctrl + shift + l command
  3. Ctrl + x - Skip current selection
  4. Ctrl + p - Go back to previous selection (Note: This keeps the default VSCode Ctrl + p command intact)

Now this is great, but what I desperately couldn't live without was the ability to select partial pattern matches already part of a word. For example

Word: pirateShipsAreCool
Pattern: pirateShips

I wanted to be able to find all partial matches from a section of the word using VISUAL BLOCK to pinpoint exactly what I wanted instead of the entire word. For instance, if you used ctrl + n or ctrl + a on the pattern pirateShips you would get something like this:

4

Notice only the three matches where it skips all other matches that also contain the pattern. What I wanted to achieve was this:

5

But I was running into the same problem of the cursor being applied only to the first cursor. After so much experimentation, I WAS FINALLY ABLE TO ACHIEVE IT. From what I understand, when adding additional pattern selections, vscodevim has a bug which only changes the mode to VISUAL instead of both VISUAL and MULTI CURSOR. There's currently no command to switch into MULTI CURSOR mode. But for some reason, when switching the window and going back to the original window, it adds cursors to all matched patterns. IF YOU DON'T SWITCH WINDOWS, any normal VIM action will only apply the change to the FIRST cursor so we must switch context to apply cursors to ALL PATTERN MATCHES. This implementation REQUIRES you to have a vertical split screen open otherwise it will not work. You could replace focusPreviousGroup and focusNextGroup with any window change action such as opening/closing the sidebar as well. This method requires you to execute multiple commands so if you want this addition, you need to install the multi-command VSCode plugin. You can experiment with other editor/window management commands to get the same effect such as opening/closing the sidebar. Remember you need to have another left or right vertical text editor for this to work.

USAGE

  1. ctrl + v to highlight desired pattern in VISUAL BLOCK mode
  2. Keep selecting patterns with ctrl + n or for all use ctrl + a
  3. When done, press shift + a to add cursors to end of line

Now here's where it get's buggy. Currently only the "A" (shift + a) command in VIM to add a cursor to the end of a pattern selection works. If you wanted to do c, I, or x, it WILL NOT WORK. But I found a workaround that requires multiple keypresses.

USAGE

  1. After selecting all your patterns, press c
  2. Now press v
  3. Any standard VIM command

For all other commands, it requires multiple keypresses since according to the VIM extension shortcut docs, there is no command to enter into VISUAL or MULTI CURSOR mode. So after selecting patterns, you have to do this sequence

VISUAL -> NORMAL -> VIM command

3

This example is: ctrl + v, j in visual mode to select pattern, ctrl + n for subsequent matches, c to enter into MULTI CURSOR mode, v to enter into VISUAL mode, then shift + i to insert cursors at beginning of pattern

NOTE: THIS IS ONLY for when you select partial matches in a word. If you select full words using (ctrl + n) or (ctrl + a) WITHOUT using visual block, then normal VIM commands to insert cursors such as (x, A, I, c) will work normally. This suffers from the same limitations as the previous "A" (shift + a) command since it requires a vertical split window to be open. Here's the code for this additional capability:

/* Add MULTIPLE CURSORS to ALL PATTERN SELECTIONS
   This one is for the "A" (shift + a) command in VIM to add a cursor to the end of a pattern selection 

   Visual block mode pattern searching for individual section already part of a word. After searching
   for sections of a pattern in a word, method applies cursors to ALL instances instead of just the first one.
  
   REQUIRES "multi-command" plugin:
   https://marketplace.visualstudio.com/items?itemName=ryuta46.multi-command

   Example:

   Word: pirateShipsAreCool
   Pattern: pirateShips

   You only want to search for all patterns matching "pirateShips" using visual block.
   Operation: 1. ctrl + v to highlight desired pattern
              2. Keep selecting patterns with ctrl + n
              3. When done, press shift + a
   It will find all matches for that specific pattern eg. "pirateShips"

   NOTE: THIS ONE IS EXPERIMENTAL and exploits the bug when adding parts of a pattern where VIM 
   thinks you are only in "VISUAL" mode instead of both "VISUAL" and "MULTI CURSOR" mode.
   For some reason, when switching the window and going back to the original window, it adds cursors to
   all matched patterns. IF YOU DON'T SWITCH WINDOWS, any normal VIM action will only apply the change 
   to the FIRST cursor so we must switch context to apply cursors to ALL PATTERN MATCHES.
   
   This implementation REQUIRES you to have a vertical split screen open otherwise it will not work.
   You could replace "focusPreviousGroup" and "focusNextGroup" with any window change action such as 
   opening/closing the sidebar as well.

   You can experiment with other editor/window management commands here to get the same effect:
   https://code.visualstudio.com/docs/getstarted/keybindings#_editorwindow-management
*/
{
  "key": "shift+a",
  "command": "extension.multiCommand.execute",
  "args": {
    "sequence": [
      "workbench.action.focusPreviousGroup",
      "workbench.action.focusNextGroup",
      "extension.vim_escape",
      "extension.vim_insert",
    ]
  },
  "when": "vim.active && editorFocus && vim.mode == 'VisualBlock' && !inDebugRepl"
},

/* This handles all other insert mode commands in VIM such as (x, I, c)

   Operation: 1. "c"
              2. "v"
              3. <standard VIM command>

   Example to use "c" command in VIM to clear current pattern selection and enter insert mode
   USAGE: c + v + c
    
   Requires multiple keypresses since according to the VIM extension shortcut docs, there is no command to enter into 
   "VISUAL" or "MULTI CURSOR" mode. :( So after selecting patterns, you have to do this sequence
   "Visual" -> "Normal" -> <standard VIM command>

   NOTE: THIS IS ONLY for when you select partial matches in a word. If you select full words 
   using (ctrl + n) or (ctrl + a) WITHOUT using visual block, then normal VIM commands 
   to insert cursors (x, A, I, c) will work normally. 

   Same limitations as the "A" (shift + a) command up there: requires a vertical split window open
*/
{
  "key": "c",
  "command": "extension.multiCommand.execute",
  "args": {
    "sequence": [
      "workbench.action.focusPreviousGroup",
      "workbench.action.focusNextGroup",
    ]
  },
  "when": "vim.active && editorFocus && vim.mode == 'VisualBlock' && !inDebugRepl"
}

In the future, if there's a new vscodevim command to switch directly into MULTI CURSOR mode let me know! Alternatively, if they implement a command to switch into VISUAL mode such as extension.vim_insert for INSERT or extension.vim_escape for NORMAL mode AND a VSCode command to simulate a keypress such as c, please let me know so we can condense the partial pattern selection into a single command.

Hope it helps!!!

nathancy avatar May 27 '22 07:05 nathancy

@nathancy That's very interesting. Would it make sense to pursue a bug fix in vscode:

From what I understand, when adding additional pattern selections, vscodevim has a bug which only changes the mode to VISUAL instead of both VISUAL and MULTI CURSOR

Would it solve all that you're trying to do if the above is fixed?

huyz avatar May 28 '22 08:05 huyz

Yes, I believe the root problem involves selecting cursors in VISUAL BLOCK mode. Here's a visualization of the already working behavior (no visual block).

1

The editor state:

  1. NORMAL
  2. VISUAL
  3. VISUAL + MULTI CURSOR
  4. INSERT + MULTI CURSOR

Now here's a demo of incorrect behavior

2

The incorrect editor state:

  1. NORMAL
  2. VISUAL BLOCK
  3. Stuck in VISUAL BLOCK
  4. Single INSERT

It should be

  1. NORMAL
  2. VISUAL BLOCK
  3. VISUAL BLOCK + MULTI CURSOR
  4. INSERT + MULTI CURSOR

If there's a bug fix for this then there wouldn't be a need for the hack to switch window focus

nathancy avatar May 28 '22 22:05 nathancy

@nathancy will the hack work if I map them to Cmd+D/Ctrl+D instead?

narze avatar May 29 '22 06:05 narze

If there's a bug fix for this then there wouldn't be a need for the hack to switch window focus

So is there an issue filed against this bug so that we can track it?

huyz avatar May 29 '22 11:05 huyz

Yes you could map to Ctrl + D or any other key as well, although I've never tested it myself. You may have to overwrite some settings to ensure that key presses happen in the correct vim mode.

I'm not aware of any open issue filed against this bug and haven't filed a bug fix issue for it.

nathancy avatar May 29 '22 22:05 nathancy

I'm not aware of any open issue filed against this bug and haven't filed a bug fix issue for it.

Ah ok. I mean, it seems to me to make more sense to file an issue than to come up with an awkward workaround. Although I do appreciate your coming up with a workaround, it would only help the few of us who happen to read this thread rather than helping everyone else who uses vscode and multiple cursors.

huyz avatar May 30 '22 01:05 huyz

On macOS, I am able to make it work by doing the following:

Normal mode:

  1. ⌘ + SHIFT + L
  2. SHIFT + A || SHIFT + I

For fine-grained selection of text, you could:

  1. Enter visual mode
  2. Select a combination of characters/words that are unique to the lines you want to edit en masse
  3. ⌘ + SHIFT + L
  4. SHIFT + A || SHIFT + I

dklanac avatar Jun 17 '22 10:06 dklanac

Any updates on this? I've been having this issue forever (sometimes will not switch to multi cursor mode - but switching to a new tab and back will fix it). I've also noticed that when closing a file and reloading it usually this issue is resolved.

justinlaughlin avatar Sep 22 '22 17:09 justinlaughlin

@justinlaughlin Not sure anyone has filed an issue against the related bug in core VS Code. If there were, that would be the appropriate place to track this issue.

huyz avatar Sep 23 '22 01:09 huyz

On macOS, I am able to make it work by doing the following:

Normal mode:

  1. ⌘ + SHIFT + L
  2. SHIFT + A || SHIFT + I

For fine-grained selection of text, you could:

  1. Enter visual mode
  2. Select a combination of characters/words that are unique to the lines you want to edit en masse
  3. ⌘ + SHIFT + L
  4. SHIFT + A || SHIFT + I

Shift + A or Shift + I doesn't work.

pencilcheck avatar Jan 26 '23 22:01 pencilcheck

This article was helpful for me, about using multiple cursors when using Vim keybindings in VSCode. None of the Ctrl, alt or Fn key combinations commands work for me on Linux with the Vim plugin.

The only command that works for me is typing gb to add a cursor to the next matching word.

mihow avatar Feb 08 '23 01:02 mihow

This article was helpful for me, about using multiple cursors when using Vim keybindings in VSCode. None of the Ctrl, alt or Fn key combinations commands work for me on Linux with the Vim plugin.

The only command that works for me is typing gb to add a cursor to the next matching word.

Same, I can't seem to insert a cursor using ctrl+d, my cursor just skips to the middle of the current visible code lines

anon-legion avatar Jun 02 '23 04:06 anon-legion

I can use ctrl shift l to edit into multi caret mode but how do I edit all instances ? :(

gerroon avatar Feb 29 '24 02:02 gerroon

@gerroon , I'm using vim mode on vscode on windows, after I generate multiple cursors (using Ctrl-Shift-l or gb), I hit d then i which deletes what is selected and puts me into INSERT mode at each cursor location. There are likely other approaches, but this works for me.

brucee63 avatar Apr 10 '24 20:04 brucee63