sponsor Vim development Vim logo Vim Book Ad

basic Tip #297: Start in insert mode without loosing your escape key

 tip karma   Rating 2/5, Viewed by 1219 

created:   August 3, 2002 17:05      complexity:   basic
author:   atkinss AT onid orst edu      as of Vim:   5.7

There are two parts to this, each is fairly simple.

First, I want to start in insert mode.  Well "set im!" in my vimrc did the job, but I lost the escape key.
Second, I have found that often times, when I'm in command mode, I hit escape trying to get back into insert mode.  I am always rewarded with a beep, telling me once again I made that mistake.

So I mapped esc in command mode to set insert mode (":set im") and I mapped esc in insert mode to unset insert mode (<c-o>:set im)  Well then I realized if you hit "i" in command mode, escape woulding work the first time.  So here's the code to add to your vimrc:

set im!                      " start in insert mode
map <esc> :set im!<cr>       " escape in command mode goes to insert mode
map i :set im!<cr>           " i in command mode goes to insert mode
map! <esc> <c-o>:set im!<cr> " escape in insert mode goes to command mode

see :help insert

 rate this tip  Life Changing Helpful Unfulfilling 

<<Attach the currently open file to email | Changing case with regular expressions >>

Additional Notes

[email protected], August 3, 2002 20:24
Its spelled 'lose' not 'loose'
atkinssc AT engr orst edu, August 5, 2002 9:09
I noticed that the comments may be pasted in when switching modes (atleast with my version of vim.)  So I recomend moving them.
eg.
" Start in insert mode and
" set escape to switch to
" command mode or back to
" insert.
set im!
map <esc> :set im!<cr>
map i :set im!<cr>
map! <esc> <c-o>:set im!<cr>

Don't forget to change your other scripts to use <c-l> rather than <esc>.  Also, about the speeling mistake, well, I'm sorry, I wasn't completely awake at the time.
atkinssc AT engr orst edu, August 6, 2002 8:30
Okay, here's a full set of mappings:
" Start in insert mode and
" set escape to switch to
" command mode or back to
" insert.
set im!
map <esc> :set im!<cr><c-o>:echo <cr>
map i :set im!<cr><c-o>:echo <cr>
map! <esc> <c-o>:set im!<cr>:echo <cr>
map a :set im<cr><c-o>l<c-o>:echo <cr>
map A :set im<cr><c-o>$<c-o>:echo <cr>
map o :set im<cr><c-o>$<c-o>:echo <cr><cr>
map O :set im<cr><c-o>^<c-o>:echo <cr><cr><c-o>k

I found that I needed all of these so that scripts would work correctly and such.
[email protected], August 9, 2002 7:34
I think a better approach would be to use <C-L> to come to Command Mode from Insert Mode.
The script would then be as simple as:
  se im
  imap <Esc> <C-L>
[email protected], July 3, 2003 14:45
vim -c 'startinsert'

;]
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
Sponsored by Web Concept Group Inc. SourceForge Logo