Reputation: 7100
How can I map Command S
to :w
in vim?
I have tried everything from other threads but nothings seems to work in Mac OS X El Capitan.
This supposedly worked in previous versions, but I tried it, and had no success.
noremap <silent> <C-S> :update<CR>
vnoremap <silent> <C-S> <C-C>:update<CR>
inoremap <silent> <C-S> <C-O>:update<CR>
Everything on this topic that I could find is several years old and doesn't work anymore. I could find this thread or this thread, but they are both more than three years old and no longer work. That is why I am asking for the current version of Mac OS X (El Capitan)
Upvotes: 8
Views: 6492
Reputation: 1061
Is totally possible to map cmd+s
or any cmd
combination by using Iterm2 and a custom map key map binding.
So, to map cmd+s
to save in terminal vim you first need to make a change in Iterm2 configuration:
Preferences -> Keys
and press the +
button in the bottom of the window, then, add the keyboard shortcut and select the action named Send text with "vim" Special Chars
and the special chars: \<C-s>
(note the \
prefix), like this:noremap <silent> <C-s> :w<CR>
inoremap <silent> <C-s> <ESC>:w<CR>i
Note that Iterm2 will convert the keystroke cmd+s
to ctrl+s
and send it directly to vim. So you can make some other things with this in mind. For example, you can disable cmd-w
(which normally will close the window in Iterm2) and send directly to vim as <C-w>
to split windows, in this case you don't need to change anything in the vim side, only map <cmd-w>
to \<C-w>
in Iterm2. Additionally, you can disable cmd+p
to prevent the annoying Iterm2 print dialog and call :fzf o something like that in vim.
The only downside is that you need to create all the maps individually:
Btw, if you want to map some "really special" keys like cmd+/
(for example to comment lines with NERDCommenter), you need to do a slightly different keymap by using the "Send Scape sequence" action:
and then, in vim, set the map for the ^[/
keystroke (note that the special ^[
symbol is made by pressing <ctrl-v>
and then <esc>
).
Upvotes: 5
Reputation: 51
The answer from romainl put me on the right track but unfortunately did not work for me (OS X Sierra, MBP 2017). Also using the (arbitrary) key F6 as intermediate mapping:
Add the following line to your ~/.vimrc:
nnoremap <F6> :w<CR>
Create a new keyboard shortcut in Iterm2:
Iterm2 keyboard shortcut config
Hope this helps.
Upvotes: 5
Reputation: 196576
<C-
is the notation for the Control key. It's <D-
for the Command key.
The Command key is only visible to MacVim and only when using the GUI front end. No matter what Vim you use in Terminal.app or iTerm.app (MacVim or any other build), it will never see the command key.
In other words, you can't expect <D-
mappings to work in CLI Vim. It is simply impossible.
The only way to get CLI Vim to do anything upon pressing Cmd+something on Mac OS X is to map that combo in your terminal emulator to emulate pressing on another key, itself mapped to the desired command in Vim.
In ~/.vimrc
…
nnoremap <F6> :w<CR>
In iTerm.app…
FYI, 0x112
is <F1>
, 0x113
is <F2>
, etc. My choice of <F6>
is arbitrary, use whatever works for you.
In Terminal.app…
I don't have El Capitan so I have no idea of what can be done in the new Terminal.app but its previous incarnations didn't allow you to remap arbitrary keys — only a predefined set — so I don't think it's doable here without some hypothetical third-party tool.
The whole idea of using Cmd+S in Vim is very bad. Get used to the Vim way instead.
Upvotes: 10
Reputation:
The C
in <C-S>
stands for Control, not Command. If you want to map Command+S, you want <D-S>
. (And no, I have no idea what the D stands for.)
Note that this will only work in MacVim. The Command key isn't passed to terminal applications.
Upvotes: 4