Wednesday, April 15, 2009

i_Ctrl-O

Derek Wyatt shared this in a recent comment, and I thought it deserved it's own tip.

If you're in insert mode, and you hit ctrl-o, Vim will accept one normal mode command and then return you to insert mode. Two examples given showed some typical usage:

<C-o>ma - Mark the current position and then keep typing.

<C-o>gql - Format the current line and keep typing

A final blurb from the comment:

"The next type you do <Esc>(thing)a you might remember that you could have done that with <C-o>".

Thanks Derek!

7 comments:

graywh said...

Except that using <Esc>...i will move the cursor. It should be <Esc>...a instead.

Travis Whitton said...

Good point... fixing.

pyrho said...

That's awesome !

dominiko said...

> Except that using <Esc>...i
> will move the cursor.
> It should be <Esc>...a instead.

Both <Esc>i or <Esc>a can move the cursor when in INSERT mode:

<Esc>i moves the cursor backward when not in the first column.

<Esc>a moves the cursor forward when in the first column.

I tend to use <C-o> in mappings but not so much interactively. For example:

map <F3> :set number!<CR>:set number?<CR>
imap <F3> <C-O>:set number!<CR><C-O>:set number?<CR>

Anonymous said...

Thanks for this. I always hoped to find a method for one-off commands. Now I just have to train my fingers to do it instead of Esc

Anonymous said...

(From Derek Wyatt - can't remember my blogger password at the moment)

I beg to differ on "i" vs "a" :)

Go on to a line with a bunch of words on it. You want to delete the next three words and continue to insert. e.g. you're on the 'l' in 'line':

This is a line with some words

Now type CTRL-O3dw. You're now on the "w" in words and are inserting. The equivalent of that is using ESC3dwi, not ESC3dwa.

The "i" was just to indicate that you went back to insert mode. Whether you would use "a" or "i" is a matter of what you're doing, not an absolute.

I feel much better now :)

Tom said...

Maybe I'm weird, but I have the opposite problem. I want things like arrow keys and mouse clicks to leave insert mode...

I've inoremapped Left, Up, Right, Down, LeftMouse, MouseDown, and MouseUp to basically append an escape. Can't post the commands here because of the apparent html...