Vim Tips Wiki
Advertisement
Tip 770 Printable Monobook Previous Next

created August 17, 2004 · complexity basic · author Dave Silvia · version 6.0


You can use this to toggle between normal and insert mode using the same keystrokes for either.

map <silent> ti :let &insertmode=1-&insertmode<CR>
imap <silent> ti <Esc>:let &insertmode=1-&insertmode<CR>

map <silent> ta :let &insertmode=1-&insertmode ^V| if &insertmode ^V| :startinsert! ^V| endif<CR>
imap <silent> ta <Esc>:let &insertmode=1-&insertmode ^V| if &insertmode ^V| :startinsert! ^V| endif<CR>

The difference between ti and ta is the former toggles inserting at the cursor position while the latter toggles inserting at the end of the line.

Note: You have to use one pardigm or the other at one time. Combinations of 'i' and <Esc> work fine. Combinations of ti and ta also work fine, they just don't mix!

That is, if you ti into insert, you must ti or ta out, <Esc> won't do it. On the other hand, if you 'i' into insert, ti or ta will take you out and back in again.

ti and ta were originally meant to be acronyms (toggle insert and toggle append) for illustration purposes. A better mapping, and just as easy to type, would be '/i' and '/a'. These won't conflict so easily with normally typed expressions (like function).

References

Comments

For ease (and speed) of typing, I would suggest choosing a sequence of two keystrokes that requires the use of opposite hands: /a can be done quite quickly (/ with the right hand, a with the left) on a QWERTY keyboard, though /i isn't quite as fast.

I'm not sure I understand the need to actually toggle the 'insertmode' option -- why not simply go into insert mode via the normal mechanism (i, a, or A) and come back via the escape?

:nmap <Leader>i i
:imap <Leader>i <Esc>

etc.


Advertisement