Vim icon indicating copy to clipboard operation
Vim copied to clipboard

Ability to define custom ex commands

Open rsslldnphy opened this issue 5 years ago • 13 comments

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.

rsslldnphy avatar Jan 01 '20 10:01 rsslldnphy

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

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

J-Fields avatar Jan 01 '20 20:01 J-Fields

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?

rsslldnphy avatar Jan 01 '20 23:01 rsslldnphy

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!

schreifels avatar Mar 17 '20 17:03 schreifels

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

kendallroth avatar Oct 28 '20 13:10 kendallroth

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

J-Fields avatar Oct 28 '20 14:10 J-Fields

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

kendallroth avatar Oct 28 '20 14:10 kendallroth

did I miss in docs?

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

J-Fields avatar Oct 28 '20 15:10 J-Fields

@kendallroth #5518

J-Fields avatar Oct 28 '20 15:10 J-Fields

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

kendallroth avatar Oct 28 '20 16:10 kendallroth

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.

djsavvy avatar May 16 '21 18:05 djsavvy

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.

PaNaVTEC avatar Aug 14 '22 09:08 PaNaVTEC

@PaNaVTEC Amazing, thank you so much

mikkelsvartveit avatar Dec 13 '23 23:12 mikkelsvartveit