Remap ⌘S to save a file in VIM
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?
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.
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
.
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.
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.
精彩评论