Vim EasyMotion: Insert mode after movement? - vim

I have remapped the EasyMotion commands ,,w and ,,b with the following:
imap ,w <ESC><leader><leader>w
imap ,b <ESC><leader><leader>b
This way i'm able to use EasyMotion in Insert mode and navigate quickly without entering Normal mode.
Though, after the move, Vim stays in Normal mode (naturally). How can I specify that after EasyMotion's
employment, Vim should enter Insert mode, in order to continue typing without delay?
Thanx!

You can use <C-o> to execute one normal mode command from insert mode. Once this command has been executed, you will be returned to insert mode:
imap ,w <C-o><leader><leader>w
imap ,b <C-o><leader><leader>b

Related

Assigning keys to function in insert mode also in vim

I am trying to bind control-F to find dialog box in vim. Following code in .vimrc works:
:map <C-F> :promptfind<CR>
However, it works only in the command mode. How can I set it up so that it also works in insert mode?
In insertmode, your binding will just insert :promptfind and then move to the new line. Use <C-O> to execute a single command-mode operation before going back to insert mode (:help i_ctrl-o), or <Esc> in its place to go to command mode and stay there afterwards.
:nnoremap <C-F> :promptfind<CR>
:inoremap <C-F> <C-O>:promptfind<CR>

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

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

Vim key mappings in insert mode

I have edited my .vimrc file and mapped some commands. They are only working in normal mode. Is there any way to map commands in insert mode? (e.g. commands involved with special keys such as Ctrl) For example, can I copy in insert mode using Ctrl+c?
The first letter in the :map commands determines which modes (:h map-modes) they apply to. So :nnoremap is for normal mode, and :inoremap for insert mode.
You usually cant' just use the same right-hand side; you need to consider that you're in a different mode. To invoke a (normal mode) command from insert mode:
prepend <Esc> if you want to stay in normal mode after the mapping
prepend <C-o> if you want to continue in insert mode after the mapping; this command switches to normal mode for just one command
For example, to map :w to <C-s>, you'd use this: :nnoremap <C-s> :w<CR>. The corresponding insert mode mapping (staying there) is:
:inoremap <C-s> <C-o>:w<CR
See :help imap. You can map keys, including keys with control, to various things within insert mode. For instance, if you wanted to copy the current word in insert mode with Ctrl+c you could use
inoremap <C-c> <esc>yiwea

Map :w to Escape in insert mode and normal mode

In order to save time in Vim, I came up with an idea. To map :w key binding to Esc in both normal mode and insert mode. However it only works in insert mode whereas in normal mode things are getting messy when I open a new file. This is what I added in .vimrc:
:inoremap <Esc> <Esc>:w<CR>
:nnoremap <Esc> :w<CR>
As I said the first command alone, works fine. But adding the second command, keys are messed up ESPECIALLY when I open a new file. For instance, although I have explicitly added in .vimrc:
map <up> <nop>
map <down> <nop>
map <left> <nop>
map <right> <nop>
by adding the second command for the normal mode, pressing up down left or right keys cause to enter in insert mode and add A B C D.
Could you help me to achieve my idea?
Information on Vim FAQ 10.9 may be useful:
10.9. When I use my arrow keys, Vim changes modes, inserts weird characters
in my document but doesn't move the cursor properly. What's going on?
There are a couple of things that could be going on: either you are using
Vim over a slow connection or Vim doesn't understand the key sequence that
your keyboard is generating.
If you are working over a slow connection (such as a 2400 bps modem), you
can try to set the 'timeout' or 'ttimeout' option. These options, combined
with the 'timeoutlen' and 'ttimeoutlen' options, may fix the problem.
The preceding procedure will not work correctly if your terminal sends key
codes that Vim does not understand. In this situation, your best option is
to map your key sequence to a matching cursor movement command and save
these mappings in a file. You can then ":source" the file whenever you work
from that terminal.
For more information, read
|'timeout'|
|'ttimeout'|
|'timeoutlen'|
|'ttimeoutlen'|
|:map|
|vt100-cursor-keys|
From :h vt100-cursor-keys:
Other terminals (e.g., vt100 and xterm) have cursor keys that send <Esc>OA,
<Esc>OB, etc. ...
So probably your nnoremap is causing the Esc on the arrow's key sequence to save the file, and the remaining characters are being interpreted alone, so the A is entering insert mode.
You could consider using option 'autowriteall', or using a different mapping to save your file; these are defined in $VIMRUNTIME\mswin.vim:
" Use CTRL-S for saving, also in Insert mode
noremap <C-S> :update<CR>
vnoremap <C-S> <C-C>:update<CR>
inoremap <C-S> <C-O>:update<CR>
The :update command is similar to :w, but only writes only if the file has been modified.
Also, you can use
autocmd InsertLeave * write

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