Search code examples
vimvisual-studio-codeeditormulticursorediting

Visual Studio Code - "Add Selection to find next match" losing selection when vim keymap enabled


I have a new installation of Visual Studio Code (default configuration, with vim keymap). I want to use the command "add selection to find next match". I think this is equivalent to how multicursors work in sublime text and vim (with an extension), or Atom (find and replace: select next).

When I try to run the command directly by using Ctrl D it does not work, taking me to the last line in the file. This might be some kind of conflict that might be easy to solve.

More interestingly, when I run the command from the command palette Ctrl Shift P.

  1. The first time selects the word I am at (well done!)
  2. The second time selects the next occurrence of that word, but loses the selection, moving the two cursors right after the two first occurrences of the word.
  3. The third time and next times nothing happens, the selection was lost in the previous time.

How to get the awesome normal behaviour that other editors have?

PD: I have now confirmed that this behavior is not present without the vim keymap, may they be compatible somehow?


Solution

  • Ctl + D is a default Vim keybind that scrolls the window down in the buffer. This is expected behavior for the Vim extension, and you should disable the mapping if you wish to use it to run the VSCode command "Add Selection To Find Next Match".

    To do this, open the extension's setting by clicking File > Preferences > Extensions. From there, choose the Vim extension and select the option to Configure Extension Settings. There will be an option called Handle Keys, where you can enter JSON data to send certain key combos back to VSCode (and there's another option to turn off all Ctl + Letter combos, called Use Ctrl Keys). You can find some examples of such data on the extension's Marketplace page. Below is the snippet of code that I added to my settings.json file to get Ctl + D back.

    "vim.handleKeys": {
        "<C-d>": false
    }
    

    From my testing, it looks like changing this setting also fixes the behavior when using the Command Palette. It might be a bug, but who cares when you are most likely just going to use Ctl + D anyway. :)