Ethan
Ethan

Reputation: 60089

In Vim is there more than one way to leave insert mode?

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?

Upvotes: 16

Views: 31428

Answers (5)

zumalifeguard
zumalifeguard

Reputation: 9016

Pressing Ctrl-L leaves insert mode in evim, so why not in regular vim, too? Add this to your vimrc: :imap

Or if you don't want to configure vim use Ctrl-[, as suggested by others.

Upvotes: 2

Jack M.
Jack M.

Reputation: 32070

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

Or:

:imap <F2> <C-o>:w<CR>

Upvotes: 9

user1933429
user1933429

Reputation: 31

ALt + the command lets you execute a command from INSERT mode, for example reference:

alt+o to open newline

alt+A to append

Upvotes: 3

Greg Hewgill
Greg Hewgill

Reputation: 993163

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

Upvotes: 28

David Wolever
David Wolever

Reputation: 154494

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.

Upvotes: 18

Related Questions