开发者

Remap ⌘S to save a file in VIM

开发者 https://www.devze.com 2023-02-09 05:51 出处:网络
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 mod

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.

Remap ⌘S to save a file in VIM


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消