History
Article Edit this page Discussion

Toggle between insert and normal mode with Esc

From Vim Tips Wiki

(Redirected from VimTip1275)
Jump to: navigation, search

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-^> ^

Rate this article:

Share this article:

Hubs Highlights International Sites Wikia messages
Entertainment
Gaming
Cartoons & Comics
Science Fiction
Hobbies
Sports
See all...
Grand Theft Auto
Pixar
Legend of Zelda Wiki
Terminator Wiki
Everquest II Wiki
Dark Shadows
German
Spanish
Chinese
Japanese
More...
Wikia is hiring for several open positions


Vote for collaboration of the month.

Send this article to a friend
"Toggle between insert and normal mode with Esc"
 
 
Hi!

I thought you'd like this page from Wikia!

http://vim.wikia.com

Come check it out!
Send confirmation