Toggle between insert and normal mode with Esc
From Vim Tips Wiki
Tip 1275 Previous Next Created: July 2, 2006 Complexity: basic Author: pulp Version: n/a
I always forget to type "i" before typing some text. So I thought using only one key to switch between normal and insert mode could solve the problem:
nnoremap <silent> <Esc> :startinsert<CR>
With this map you can easily toggle between insert and normal mode using the Esc key. I also switched the Esc key with the Caps Lock key (VimTip166) to make it faster.
[edit] Comments
I think the good thing about the way the Esc key works is that no matter what mode you're in, you know what mode you will be in when pressing it -- normal. When you make it a toggle then you will always have to look to see what mode you're in before pressing Esc.
The map has problems with escape sequences in terminal Vim.
So using the Esc key was not a good idea. I changed it to use the "^" key. Positioning the cursor can be done with the "Pos1" key.
function Stopinsert() stopinsert echohl boldtext | echo "-- NORMAL --" | echohl None endfunction hi boldtext gui=bold nnoremap <silent> ^ :startinsert<CR> inoremap <silent> ^ <C-o>:call Stopinsert()<CR>
For most Vim users, this is going to introduce more trouble than good.
Consider: We often do not want to remember the "mode".
So if we want to do something in Normal mode, we press Esc even if we're already in Normal mode. If we want to do something in insert mode, we press Esc then press i, this will send us into insert mode regardless of the current mode.
If we want to do something in normal mode, we press Esc then press :, this will send us into normal mode regardless of the current mode.
The unique use of Esc is mandatory, it must always means: "go to normal mode" regardless of the current mode.
This tip is for Vim users who do NOT like the currrent behavior (or are not used to it). If your fingers are trained to use the Esc-i stuff this script is not four you. See it as an additional way of doing things.
I think it is more "human" to use a toggle to change a "mode". So in the end using this script will propably increase efficiency.
If you use this tips in combination with this script: VimTip1279 you will get an visual feedback in which mode your are in.
To insert the "^" character you can use (in gvim):
inoremap <A-^> ^
To jump to the first character in a line:
nnoremap <A-^> ^
