Vim Tips Wiki
Advertisement
Tip 297 Printable Monobook Previous Next

created August 3, 2002 · complexity basic · author atkinss · version 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:

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

Don't forget to change your other scripts to use <c-l> rather than <Esc>.

References

Comments

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>

vim -c 'startinsert'

Just using

vim -c start

on the command line works perfectly well; no configuration changes are necessary. If you want it to do this all the time, you can add

alias vim='vim -c start'

to your .bashrc.


I found this map a slightly better approach in insertmode, just:

imap <ESC> <C-o>

So the user modeless-mind-oriented only has to think in press ESC before a command, then everything continues as expected. In fact, this is exactly the reverse idea of Vi workflow. The only caveat IMHO is that this map should be disabled disabling insertmode. Maybe this could be a default Vim behaviour? I've found also this maps quite useful in insert mode:

" Paragraph up
imap <C-up> <C-o>{
" Paragraph down
imap <C-down> <C-o>}
" Find / search
imap <C-f> <C-o>/
" Redo
imap <C-r> <C-o><C-r>
" Edit new file
imap <C-e> <C-o>:browse confirm e<CR>

--Khroma 07:13, 3 April 2009 (UTC)

Advertisement