tags:

views:

148

answers:

3

There are several commands that do something and then enter insert mode. Are there also commands that leave insert mode and do things?

For example, I frequently do this...

control[ : w return

Before I create a mapping, is there already Vim command that does that?

+6  A: 

The only one I can think of is c-o, which lets you run one command in normal mode then drops you back into insert mode.

For example, a<c-o>~b would result in Ab.

David Wolever
But it doesn't do things, does it?
Mykola Golubyev
No, you're right – it only lets you do stuff, it doesn't "do things for you", so to speak.
David Wolever
For the record:imap <Leader>w <C-o>:w<CR>"does do things". Choose an appropriate key combination for the map and there you go.
kotarak
Ah, yes, I hadn't thought thought about mentioning imap because I thought the poster was asking about "stock" commands. Very good point.
David Wolever
+3  A: 

In addition to Esc (which is identical to ^[), ^C also exits insert mode.

Greg Hewgill
+2  A: 

If you'd like to map a few keys to do things like save a file while in insert mode, you can use the imap command. This binds F2 to exit insert mode and save the file:

:imap <F2> <Esc>:w<CR>

This binds F2 to exit insert mode, save the file, and re-enter insert mode:

:imap <F2> <Esc>:w<CR>a
Jack M.