0

I'm using the Visual Studio Code Vim plugin to write my master's thesis. VS Code has an 'autosave' setting, which I have enabled, which means I don't need to use :w to save my files. However, I can't seem to unlearn the muscle memory that intuitively :ws every time I make a change to the file. While this isn't a huge problem, I'd rather unlearn this behavior.

One way I thought of unlearning this behavior is to bind :w to the system warning sound (echo "\a"), but I can't seem to find any way to do so.

Tips on other ways to unlearn the :w muscle memory are also welcome.

5
  • 4
    Unless the solution could also be applied to standard Vim I believe this would be off topic here. (It may...I don't know yet...just giving you a heads up.)
    – B Layer
    Jun 18 at 10:19
  • Good point. Here is a list of supported features: github.com/VSCodeVim/Vim/blob/master/ROADMAP.md
    – Niek
    Jun 18 at 10:27
  • Thanks. Actually, it looks like it uses the Neovim API. It might be fully on-topic for that reason. As long as nothing VS Code specific needs to be done. (...is my thinking. Mods should be able to answer definitively.)
    – B Layer
    Jun 18 at 10:28
  • 1
    @BLayer my thinking from a quick glance is that, as long as it can be done at the vim level it’s ok. They can also include a vscode path, if one exists, but admitting the latter without the former would belong elsewhere. OTOH, there’s a bit of chicken-and-egg there. I don’t see anything here that’s too specific to vs code, so I’m not particularly worried
    – D. Ben Knoble
    Jun 18 at 10:58
  • 1
    There is one important question tho: Do you want to disable :w completely (i.e. even usages like :w filename) or only :w but not :w filename? In the first case a command line abbreviation like here would work, in the second case it's a bit harder because you have to trigger your guard only when you're not going to type something after :w.
    – statox
    Jun 18 at 11:12
2

While I normally advise against remapping <CR> I think here that could be a correct solution:

cnoremap <expr> <CR> (getcmdtype() == ':' && getcmdline() =~ '^w$')? '<BS>echoerr "Dont use :w"<CR>' : "<CR>"
  • cnoremap greats an mapping only in command mode (i.e. :, / or ?) when you type <CR>.
  • (getcmdtype() == ':' && getcmdline() =~ '^w$') checks that we are in the : prompt and that the content of the line is only w (otherwise you might be typing something like :w filename, :windo, :wincmd, etc...)
  • If the line is only :w then replace it by an error message, (echo "\a" doesn't work on my system but you can replace this command by whatever you prefer)
  • Otherwise just use <CR> as usual.

Here is this code in action in neovim, it works the same in vim and I suppose it is the same in VScode with the vim plugin:

enter image description here

2
  • I recently test drove a Vim plugin in VS Code and I just verified that it's the same one used by OP. Based on that I bet this won't work. It had a lot things missing. It felt more like an emulator (like the one in Intellij) than something built on real (Neo)Vim code.
    – B Layer
    Jun 18 at 12:15
  • I can confirm that this works in vim on my terminal, but it seems that the VS Code plugin, indeed, lacks this feature. Unfortunately, it seems that this is off-topic for this site. Thanks for the help though!
    – Niek
    Jun 18 at 12:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.