Vim icon indicating copy to clipboard operation
Vim copied to clipboard

Use `when` clause in keybinding overrides

Open vsobotka opened this issue 7 years ago • 7 comments

I would love to use something like this:

"vim.insertModeKeyBindings": [
       {
           "before": ["ctrl", "p"],
           "after": [],
           "commands": [
               {
                   "command": "editor.action.triggerSuggest",
                   "when": "editorHasCompletionItemProvider && editorTextFocus && !editorReadonly && !suggestWidgetVisible"
               },
               {
                   "command": "selectPrevSuggestion",
                   "when": "editorTextFocus && suggestWidgetVisible"
               }
           ]
       },
       {
           "before": ["ctrl", "n"],
           "after": [],
           "commands": [
               {
                   "command": "editor.action.triggerSuggest",
                   "when": "editorHasCompletionItemProvider && editorTextFocus && !editorReadonly && !suggestWidgetVisible"
               },
               {
                   "command": "selectNextSuggestion",
                   "when": "editorTextFocus && suggestWidgetVisible"
               }
           ]
       }
  ]

Use-case

Demonstrates behavior of Ctrl + P in normal and insert modes.

Normal mode

Works without change - lets me find and open a file in a project.

Insert mode

When I do not have suggestion window visible, it will trigger the suggestion window to be visible.

When I do have suggestion window visible, it will go to previous suggestion.

vsobotka avatar Mar 08 '17 10:03 vsobotka

This is a very reasonable request.

johnfn avatar Mar 09 '17 06:03 johnfn

I was thinking about the same thing that users might just want to map their familiar keybindings to Code's commands. Right now you can't always make it happen with Code's keybinding customization.

  • You can't map j j to a command in Code while it's possible in Vim xyzModeKeyBindings.
  • You want to map keys on in a specific Vim mode.

The only catch is the when clause is not possible as we don't have access the context of keybindings.

rebornix avatar Mar 10 '17 18:03 rebornix

Albeit a valid feature ask. This issue hasn't received a lot of traction in terms of upvotes, and we are missing the necessary API from VS Code to implement such a feature. As such, I'm closing this to keep our issue list more sane.

jpoon avatar Dec 30 '18 11:12 jpoon

image

I want to :nmap ]l <f4> (like unimpaired.vim), but it is impossible without when clause. Instead of using VSCodeVim's keybinding, I put them in keybindings.json then append && editorTextFocus && vim.mode == 'Normal' to when clause.

  {
    "key": "] l",
    "command": "goToNextReference",
    "when": "referenceSearchVisible && editorTextFocus && vim.mode == 'Normal'"
  },

It might not always work, but for my case this worked.

ypresto avatar Jun 24 '19 04:06 ypresto

With above method when typing r ] in normal mode VSCode swallows ] and waiting for second key of chord... 🤔

ypresto avatar Jun 24 '19 06:06 ypresto

To workaround, can we have context that represents whether VSCodeVim is waiting for second key?

"when": "referenceSearchVisible && editorTextFocus && vim.mode == 'Normal' && !vim.waitingForKey"

ypresto avatar Jun 24 '19 06:06 ypresto

Got hit with this problem too. Something like vim.waitingForKey would be a blessing.

musjj avatar Apr 25 '24 21:04 musjj