VSCodeVim / Vim

:star: Vim for Visual Studio Code
http://aka.ms/vscodevim
MIT License
14.02k stars 1.32k forks source link

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

Open Hubro opened 5 years ago

Hubro commented 5 years ago

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:

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

elnygren commented 5 years ago

@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.

Hubro commented 5 years ago

@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.

elnygren commented 5 years ago

@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

Hubro commented 5 years ago

@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.

sebenik commented 5 years ago
  • 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?

j3zz3r commented 4 years ago

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

haifengkao commented 4 years ago

@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

jkryanchou commented 4 years ago

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

fnurl commented 2 years ago

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.

nathancy commented 2 years ago

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!!!

huyz commented 2 years ago

@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?

nathancy commented 2 years ago

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

narze commented 2 years ago

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

huyz commented 2 years ago

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?

nathancy commented 2 years ago

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.

huyz commented 2 years ago

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.

dklanac commented 2 years ago

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
justinlaughlin commented 2 years ago

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.

huyz commented 2 years ago

@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.

pencilcheck commented 1 year ago

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.

mihow commented 1 year ago

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.

anon-legion commented 1 year ago

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

gerroon commented 9 months ago

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

brucee63 commented 7 months ago

@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.