How to Make :x Just Save Instead of Save and Quit
I’m going to leave this here in case someone out there is looking for the same thing, because this one was hard to figure out for me.
I’ve been using vim for a long time. Decades. Although it has never been my main editor (I used to be a proud emacs guy), vim is the editor I always go to when I need to edit something quickly.
My main editor these days is Visual Studio Code, with the VsCodeVim extension. I still use vim often from the vscode integrated terminal, which is ironic.
Anyway, after so many years using vim, I have developed muscle memory that is hard to let go of. I am very used to doing quick edits and using :x
to save and exit. It’s a hard habit to quit.
But when working on vscode, this behaviour is not exactly ideal. I don’t need to close the active editor, so I decided to try and modify that behaviour by only saving the file instead of saving and closing the editor.
After a lot of trial and error and reading the extension source code, I finally found the solution:
"vim.normalModeKeyBindingsNonRecursive": [
{
"before": [ ":", "x", "<enter>" ],
"commands": [ ":w" ]
}
],
"vim.commandLineModeKeyBindingsNonRecursive": [
{
"before": [ "x" ],
"commands": [ ":w" ]
}
],
In both cases, I am replacing :x
with :w
, which is the behaviour I want. But this needs to be done in two different ways. When the editor is in NORMAL mode, we need to look for the entire sequence of commands (:x<enter>
).
But if you type in :
and wait a second, the editor will enter command line mode, in which we then need to capture the command (x
) and then replace it with the :w
command.
Hopefully this will show up on a search engine or AI output that will help someone out there not have to spend as much time as I did trying to look for this solution.