VSCodeVim / Vim

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

Ability to define custom ex commands #4454

Open rsslldnphy opened 4 years ago

rsslldnphy commented 4 years ago

Is your feature request related to a problem? Please describe. I use neovim integration, but I am unable to define the custom ex commands I use in neovim, and the commands defined in my ~/.config/nvim/init.vim are not respected. To give you a flavour, these are the ones I currently have defined for neovim:

command! W   :w
command! Wa  :wa
command! Wq  :wq
command! Wqa :wqa
command! E   :e

Describe the solution you'd like I am able to define custom ex commands either in VSCode settings.json or have the ones in my neovim config respected.

Describe alternatives you've considered

Additional context I'd be happy to have a go at implemeting this myself if this is something you'd accept a PR on and someone could point me in the right direction.

J-Fields commented 4 years ago

http://vimdoc.sourceforge.net/htmldoc/map.html#E174

http://vimdoc.sourceforge.net/htmldoc/usr_40.html#40.2

rsslldnphy commented 4 years ago

On further investigation - after running my own amended version of the extension with the patch in #4456 applied - I was able to get a bit more information.

It looks like running :command! W w does in fact create a command :W, but unfortunately the :W command doesn't work. It gives produces an error about a missing filename:

image

I think this is because:

When running :w in VSCode the command is caught and handled by VSCodeVim using the VSCode API. It never reaches neovim, and doesn't need to.

However when running :W the command is not understood by VSCodeVim, so it is forwarded on to neovim.

Neovim then maps :W to :w and runs that command in neovim.

The neovim :w command can't work as the file isn't actually open in neovim. It needs to be the VSCodeVim version of the command that's run.

So on reflection I think this is likely to be a trickier task than I initially thought, unless someone who knows the codebase well thinks otherwise?

schreifels commented 4 years ago

Just wanted to add that custom ex commands are an important part of my Vim workflow, and I would also love to see these supported in VSCodeVim! For example, in my Vim config I have:

command! Etodo edit ~/Desktop/todo.txt
command! BO BufOnly

The first lets me use :Etodo<Enter> to jump to my todo doc, and the second allows me to use :BO<Enter> to invoke a script that closes other buffers. I could define keyboard shortcuts for these things, but there's a point at which I can't remember any more shortcuts! I also use them for more complex things (e.g. where an argument is required), but I'd be satisfied even with basic support.

By the way, I think this would be useful outside of the nvim integration as well, e.g. I'd love to just be able to define something like this:

    "vim.exModeCommands": [
        {
            "before": [":BO"],
            "commands": ["workbench.action.closeOtherEditors"],
        }
    ],

Or maybe this is possible? I wasn't able to find anything like this in the docs or other issues.

Cheers!

kendallroth commented 3 years ago

Similarly, I would really like to be able to define custom commands like :bn and :bp to move between tabs.

J-Fields commented 3 years ago

@kendallroth :tabn/:tabp do this. :bn and :bp should probably be built-in aliases for the same, since VSCode doesn't have the concept of a buffer separate from a tab.

kendallroth commented 3 years ago

@J-Fields I apologize for not already knowing that (did I miss in docs?)! Yeah, since there is no difference/concept it would make it easier/quicker by a few characters 😄

J-Fields commented 3 years ago

did I miss in docs?

If you did, I wouldn't blame you - our docs need serious work lol

J-Fields commented 3 years ago

@kendallroth #5518

kendallroth commented 3 years ago

Excellent, thank you for that! I really, really appreciate the improvements to my workflow that combining Vim and VSCode bring...great package!

djsavvy commented 3 years ago

For anyone else who stumbles across this, I wrote up a stackoverflow answer that provides kind of a hacky workaround:

https://stackoverflow.com/a/67560051/2378475

It's far from perfect (some of the downsides are discussed in the comment), but it works, and is good enough for the custom commands that are stuck in my muscle memory.

PaNaVTEC commented 2 years ago

Since this is still open and it was really conflicting with my workflow, I decided to hack it so at least it works in my machineâ„¢.

The expression parser registers the command like this:

[['w', 'rite'], WriteCommand.argParser],

So what we need is another command registered like:

[['W', 'rite'], WriteCommand.argParser],

Please note the uppercase W. You can pull the repo, edit the code and package the extension, or you can edit the bundle since it's a simple change, I went for the later since I'm lazy.

Edit:

~/.vscode/extensions/vscodevim.vim-1.23.2/out/extension.js

Search for "rite", you can think of that as a keyword that will point you straight to the point where you have to make the edits and copy pasting that array: Screenshot from 2022-08-14 10-56-03

Yeah, it's not ideal and if I update to a new version I'll have to do it again, but it works as intended, not like the other workaround I've seen :shrug: So just sharing this with you guys in case it helps anyone until we get native support.

mikkelsvartveit commented 9 months ago

@PaNaVTEC Amazing, thank you so much