Remap ⌘S to save a file in VIM

2.6k views Asked by At

I have recently switched from using MacVim to just using Vim on the command line. One feature I miss is being able to save a file with ⌘S rather than having to go back into normal mode and then :wq

I am using Vim inside iTerm2. Is there any way to get ⌘S to save a file both in insert and normal mode?

4

There are 4 answers

0
b4winckler On BEST ANSWER

It is not possible to map to ⌘ in the console version of Vim. If you want to be able to use ⌘ in a mapping you'll need to switch back to MacVim.

2
Andrew Mcveigh On

Well, I'm not sure that command-line Vim can register the ⌘ key, and I'm not on my Mac to confirm, but you can map ⌘S in .vimrc like this.

inoremap <D-s> <ESC>:w<CR>i  "insertmode
nnoremap <D-s> :w<CR>        "normalmode

Again, this will only work if command-line vim can recognize the ⌘key.

1
Alex Dong On

Here is an even simpler way to do it: using iTerm2's "Send Text". Basically you can map Cmd-S to send a text to the current vim session :w\n.

Send Text: <code>:w\n</code>

4
Dolan Antenucci On

You can use iTerm2 to map Cmd-S to some other combination that the shell and Vim will recognize (e.g. Ctrl+S) , then you can simply map that command via a .vimrc file as Andrew pointed out.

In summary:

  • edit iTerm2 Keys with a new entry that maps Cmd-S to Ctrl+S.
  • Add commands for mapping Ctrl+S to vimrc file (like Andrew describes) or like this site.