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.


From :help i_CTRL-[:

<Esc> or CTRL-[ End insert or Replace mode, go back to Normal mode. Finish abbreviation. Note: If your <Esc> key is hard to hit on your keyboard, train yourself to use CTRL-[.

To me, Ctrl-[ is just as hard to hit.

Since 'insert' - 'normal' is a very common operation, quick easy keystrokes, i.e. a toggle, makes life easier.)


How do you easily type Ctrl-[ on a keyboard where the [ is located at AltGr-8?

For that matter, my / is at Shift-7, so the suggested /i and /a mappings are not much good either.

I suppose us poor users with a Danish keyboard (and I suspect many other non-US too) will just stick with i and <Esc>.


Advertisement