I'm using vim for programming along with a plugin that allows me to open a command console in the editor, which helps me execute my script, and as you know you need to save before executing, the problem I have with this is that I get it makes it very slow to have to escape and type :w every time I want to save a file to later execute it, so I need a keyboard shortcut that allows me to just save the file, just like in other editors you press ctrl + s to just save the file File, Archive.
I know a shortcut in vim that allows me to save and close a file: shift + zz , but the problem is that the editor closes me, how do I save only the file using a keyboard shortcut?
Creating shortcut
Ctrl + s
to save in VimHello, a solution to this problem is to create a keyboard shortcut in your
.vimrc
. Most editors have aCtrl + s
save shortcut. You can recreate this shortcut in vim or nvim with the following code:Save the code in your .vimrc, this will enable the save command with the Ctrl + s shortcut. That yes, it is possible that some plugin already uses this shortcut for some function, if so; you just have to reassign that shortcut to another and leave the Ctrl + s to save.
Code explanation
nmap
It will create shortcut in normal mode.imap
It will create shortcut in insert mode.<c-s>
Indicates that the shortcut will be Ctrl + s. This indicates that if you want a Ctrl + w shortcut it would look like this:<c-w>
:w<CR>
This indicates that the command to execute will be :w (Save), the<CR>
indicates an enter.<c-o>:w<CR>
Here the difference from the previous one is that includes<c-o>
this is added to the shortcut in insert mode, this allows the :w to be executed in the vim command area, otherwise the :w would appear in the text you are editing.Shortcut in lua
Nvim 5.0 now has better lua support. Therefore you can create this same shortcut in the lua configuration file. The code would be the following:
Conclusion, with these settings in the Vim configuration file, you can get the save shortcut
Ctrl + s
that is very popular in many editors.