1

I would like to be able to setup MacVim so that it switches back to Normal mode after saving a buffer. For example: lets say I am adding some text in Insert mode and I hit "Command + S" to save, I would like to be in Normal mode after the save operation has completed.

Is this possible?

Note: Incase the above is unclear, I do not want to spend more time in Insert mode, but less. I would like to exit Insert mode automatically upon save.

f1lt3r
  • 2,176
  • 22
  • 26
  • 2
    Why not do it the Vim way instead? Go back to normal mode with `` and write with `:w`. – romainl Mar 07 '16 at 16:57
  • I'm working with LiveReload a lot so I will typically want to be Insert mode, type some text, hit save, the web browser will update when the file changes, and then I find I almost always want to be back in Normal mode and jumping to somewhere else in the file using "}" ")" "/search" etc. – f1lt3r Mar 07 '16 at 19:05

2 Answers2

2

Adding the following lines to your .gvimrc will disable MacVim's [Cmd + S] shortcut, and switch the mode back to Normal before saving.

It will also block Vim from entering Insert mode when hitting [Cmd] + [S] keys (as this would have activated the substitute command). Note: you will still be able to substitute hitting the [S] key as usual.

" Disable MacVim save shortcut
macmenu File.Save key=<nop>

" Exit to Normal mode upon [Cmd+S]
inoremap <D-s> <Esc>:w<CR><Right>
vnoremap <D-s> <Esc>:w<CR>

" Save in Normal mode (block Substitute)
nnoremap <D-s> :w<CR>

Thanks to @Amadan for pointing me in the right direction.

f1lt3r
  • 2,176
  • 22
  • 26
0

Wanting to typically be in insert mode is using Vim wrong. If it works for you, that's fine; but you will never reach the potential of Vim while sticking to that habit; might as well use Notepad++/SublimeText3/...

If you really really want it, you can have something like :inoremap <C-s> <C-o>:w<CR> to save and stay in insert mode, or :inoremap <C-s> <Esc>:w<CR> to save and move to normal mode. (It will work only on graphical Vim, as terminal Vim typically won't ever receive Ctrl-S.)

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • 1
    Thanks for your answer, I will try this. Note: you may have got the wrong impression, I absolutely don't want to typically be in Insert mode. It just so happens that I don't want to hit "Caps/Esc" and then "Command + S" to save. I just want to hit "Command + S" and have it drop back to Normal mode if I was doing something in Insert. – f1lt3r Mar 08 '16 at 17:04
  • Your answer does seem to work for "Ctrl + S" but not for "Command + S". I am attempting to use: `:inoremap :w` – f1lt3r Mar 08 '16 at 17:32
  • 1
    Eh, sorry, misread the question. `` in MacVim is bound to the menu Save action, so MacVim never sees your mapping. [Disable use of MacVim keyboard shortcuts](http://stackoverflow.com/questions/13436823/disable-use-of-macvim-keyboard-shortcuts) using `macmenu File.Save key=` in your `.gvimrc` (not `.vimrc`); then you should be able to use the binding you tried. – Amadan Mar 09 '16 at 00:46