Can I auto-exit Insert mode when saving in MacVim? - vim

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.

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.

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.)

Related

What does TAB do in vim?

I normally don't write tabs, so I mapped <TAB> to <ESC> in insert mode so that it's faster to leave insert mode. However, when I accidentally press TAB in normal mode, it jumps to some location in the file, without saving the jump location. I first suspected that a plugin is doing that, but I find no mapping for TAB in normal mode. Even explicitly unmapping it doesn't help, so it must be a built-in command. What does it do, why isn't this jump saved to history, and how do I disable it?
You're seeing the built-in :help CTRL-I; <C-I> and <Tab> are the same in (G)Vim. That command is the opposite of <C-O>; it goes to a newer position in the jump list. You don't see the "saving to history", because it operates on existing jumps.
If you don't want any action on Tab in normal mode, just put
:nnoremap <Tab> <Nop>
into your ~/.vimrc; :help <Nop>.
What does it do, why isn't this jump saved to history, and how do I disable it?
"Tab" (or <C-I>, which is really the same thing) goes forward through the jumplist ("undo" for <C-O>), so it's either no-op, or still there's nothing to save ;-) To see the embedded help topic use :h <tab>
If you really want to disable it, just remap it:
nnoremap <tab> <nop>

Assigning save (:w<cr>) to <leader>w in vim

I want to be able to save a file in vim while in the insert mode. I thought of using the following shortcut:
inoremap <leader>w <Esc>:w<cr>
While the shortcut saves the file in the insert mode, it leaves the cursor one spot ahead of where the cursor would be if I physically typed out the keys
Esc :w followed by Enter. This is a problem because when I use the shortcut whenever I am at the end of a line, it takes me to the next line, and I have to then come back to the spot where I initiated the save.
Any help would be appreciated on how I can map <leader>w to the exact actions that occur in Vim when I physically type out the Esc :w followed by Enter key sequence.
I should add that if I instead use the following key-mapping, things work exactly as I want:
inoremap <C-s> <esc>:w<CR>
However, I would like to avoid pressing CTRL and s at the same time. It is possible there is some problem with the <leader>, but I cannot figure out what it is (I am using , as my leader key).
Though one could discuss the suitability of your insert-mode mapping, the root cause of your problem is a trailing space in the mapping definition; i.e. Vim reads this as follows:
inoremap <leader>w <Esc>:w<cr><Space>
You'll even see this in the :imap <Leader>w output! <Space> in normal mode advances the cursor one to the right (like l); that explains the unexpected move.
Try this instead:
inoremap <silent> <leader>w <C-o>:w<CR>
The idea is Ctrl-o can be used to run commands directly from insert mode. See :help i_CTRL-O for details.
Why not simply doing
inoremap <leader>w <Esc>h:w<cr>
(not the additional h for going back one character)?

Create a command in vim for all modes

If I want to remap <C-s> to :w<CR> I'd have to do something like this
nnoremap <C-s> :w<CR>
inoremap <C-s> <Esc>:w<CR>
since insert mode would requre escaping to normal mode before entering the command (sure, another <Esc> wouldn't kill anything, but it's ugly, my terminal bell goes off and with all the other modes available [n, i, v, s, x, c and o] there are plenty of cases where extra <Esc> wouldn't cut it).
Is there an easy way to map a command "for all modes" in Vim?
You can get quite close by taking advantage of the CTRL-\ CTRL-N command. CTRL-\ CTRL-N goes to Normal mode from any mode.
We can define just two mappings with identical right-hand side to cover Normal, Visual, Select, Operator-pending, Insert, and Command-line mode.
noremap <C-S> <C-\><C-N>:write<CR>
noremap! <C-S> <C-\><C-N>:write<CR>
See :h CTRL-\_CTRL-N.
There is no easy "One mapping to rule them all" way to do such a thing. On the plus side when you do make all your mappings you put them in your ~/.vimrc and forget about it.
However I must say it is the Vim Way to do a save from normal mode (as a matter a fact do most thing from normal mode). If you did it form insert mode for example you would be breaking up your undo block if you wanted to exit from insert mode, save, and then reinsert insert mode (See :h i_Ctrl-o). Not to mention such a mapping may affect the . command which is super handy.
You may also want to avoid the <c-s> key on the terminal because it will often trigger terminal's software flow control (XON/XOFF). You can disable this for your terminal by using stty -ixon.

How to let vim keep in insert mode when pressing Alt - h to move in insert mode?

In order to edit fast in insert mode, we can use mapping keys to navigate in insert mode, for example
:inoremap <A-h> <Left>
with this mapping, user can press Alt + h in insert mode to let the cursor move left and then do insert, we don't need to exit edit mode, then move cursor. this will save some time for us, my question is when I use this mapping key, the cursor moves correctly, but vim switch to normal move after moving, it should keep in insert mode, I use vim 7.3 in Sun OS. I tried this in gvim 7.3 on windows, everything is good, so I wondered is there anything i lost?
First of all: if you think navigation in insert mode is speeding up your vim, you're using it wrong (i.e. like any other non-modal editor, when its biggest benefit is specifically modality).
That said, we can't say too much without you showing us your mapping. I suspect it is something like this:
inoremap <M-H> <Esc>h
Try changing to
inoremap <M-H> <C-O>h
I got the answer here, http://vim.wikia.com/wiki/Fix_meta-keys_that_break_out_of_Insert_mode, this will happened on Unix or Putty.

Vim insert mode: unambiguous key binding that always works as expected?

Background:
Sometimes when editing in vim it is possible to have extra characters in a file that the user did not expect to be there because he was in "insert mode" when in a hurry and rushing to get something finished.
Fortunately, even if the user is rushing, pressing ESC a couple of times is always sufficient to get them out of insert mode and into normal mode, with no surprises.
Question:
Is there a key binding that works the same way for insert mode? Pressing "i" can get you into insert mode, but if you press it multiple times, you will start inserting the letter "i" into the file.
Goal:
The goal is to have some key binding for getting back into insert mode that the user can even press multiple times with eyes closed, and still not worry about "surprises" of unexpected characters being put into the file.
<C-o>i should do the trick. <C-o> gets you temporarily to normal mode, but for only one command, if that command is "go to insert mode" than well, you simply return there.
Edit: I could reproduce your error message now, and it seems the easiest thing to do is this:
:nmap <C-i> i
:imap <C-i> <C-o>i
If do not map <C-i> in insert mode, but in normal mode only, then repeatedly hitting <C-i> will be idempotent.
Thanks to Benoit for mentioning that <C-i> inserts a tab in insert mode.
You should do a mapping that behaves differently in the distinct modes:
:inoremap <F1> <NOP>
:nnoremap <F1> i
:vnoremap <F1> <esc>i
:cnoremap <F1> <C-C>i
:onoremap <F1> <esc>i
Hitting F1 will go to insert mode then.
You can also toggle the 'insertmode' setting (:set insertmode): in this mode, the Insert mode is the default mode (to which you switch with Escape, and you go to normal mode with CTRL-L.
The answer given by bitmask works, but it apparently has the side-effect of producing the error message:
E37: No write since last change (add ! to override)
Unless you have configured your vimrc to turn that message off.
But another alternative that seems to work without producing error messages:
CTRL-C i
Which seems to work on standard vim.

Resources