3

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?

Crofty
  • 73
  • 1
  • 6

4 Answers4

5

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.
Dolan Antenucci
  • 15,432
  • 17
  • 74
  • 100
  • 1
    Thank a bunch for the hint. I want to make this a bit more concrete. I mapped cmd-s to send hex code 0x1b 0x4f 0x51 which corresponds to F2. Then, in vimrc I add imap :update . – Piti Ongmongkolkul May 26 '12 at 17:12
  • +1 for iTerm2. It is a great terminal emulator with many extra nice features that make it great for using vim in the console. – JeremyFromEarth Dec 17 '12 at 21:39
  • @PitiOngmongkolkul how did you get that hex code for F2??? I could find out that F2 has the 0x78 keycode, but no idea how to transform that into a scape hex code – SystematicFrank Jan 16 '14 at 07:45
  • I recalled that I have it printed out the key I press in vi somehow. Can't exactly remember how though... – Piti Ongmongkolkul Feb 25 '14 at 05:29
3

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>

Alex Dong
  • 975
  • 2
  • 9
  • 18
1

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.

Andrew Mcveigh
  • 474
  • 5
  • 8
  • 1
    This doesn't seem to work when using command line Vim in iTerm2. It looks like the ⌘S is not getting through to Vim, it's just calling the iTerm command instead (Save text as). – Crofty Feb 09 '11 at 14:29
  • 1
    If iTerm is catching it, then you're out of luck (as far as I know). You could always try to map ctrl-s, or try pressing alt-s and mapping that. – Andrew Mcveigh Feb 09 '11 at 15:44
0

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.

b4winckler
  • 42
  • 1