VSCodeVim / Vim

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

Keys after commands in key bindings #9029

Open edemaine opened 2 months ago

edemaine commented 2 months ago

Is your feature request related to a problem? Please describe. I'm trying to define nice key bindings for LaTeX Workshop's various commands. The one I'm working on now is latex-workshop.wrap-env, which wraps the selection in \begin{}...\end{} and puts multiple cursors on the two }s so you can type the environment name. This is naturally an operation for Visual Mode, but afterwards I want to be in Insert Mode. The only way I know to do that is to type <Esc>i.

Describe the solution you'd like I'd like to bind keys to execute a command and then simulate some keystrokes.

Describe alternatives you've considered It seems that after keys are executed before commands instead of after, which is when I want these keys to execute. This is what I tried:

    "vim.visualModeKeyBindingsNonRecursive": [
      {
        "before": [ "\\", "b" ],
        "commands": [ "latex-workshop.wrap-env" ],
        "after": [ "<Esc>", "i"],
      }
    ],

Probably both times to execute keys are useful, so in addition to after we could have another property (afterCommands?).

Additional context I'm worried that the commands are executed asynchronously and it's not easy to know when they're done. But hopefully that isn't the case? I could try to implement this if you agree it's useful and could give some pointers on where this happens.

flczcy commented 1 month ago

"commands": [command...]

Here, I guess only for command in VS Code and extension, so you need to find command of vim extension in VS Code.

Try this:

{ "before": [ "\\", "b" ], "commands": [ "latex-workshop.wrap-env", "extension.vim_insert"  ]}

// or

{ "before": [ "\\", "b" ], "commands": [ "latex-workshop.wrap-env", "extension.vim_escape", "extension.vim_insert"  ]}

You can find vim extension command in Keyboard Shortcuts Panel.

image
edemaine commented 1 month ago

Thank you! I didn't realize all these commands existed because they aren't listed in Ctrl-Shift-P. { "before": [ "\\", "b" ], "commands": [ "latex-workshop.wrap-env", "extension.vim_escape", "extension.vim_insert" ]} works well.

This seems like a pretty good solution, so perhaps we should just mention the existence of these commands in documentation.