DEV Community

Igor Irianto
Igor Irianto

Posted on • Edited on • Originally published at irian.to

Do you know Vim can execute normal mode command while in insert mode?

Follow @learnvim for more Vim tips and tricks!

Vim has been my go-to editor for the last 1.5 years, yet I am still learning something new. Recently I learned about "Insert Normal" mode and I'd like to share how you can use this mode to code faster in vim!

typing-300wpm

Feedbacks are highly appreciated. If you think of more ways to save keystrokes using this mode, feel free to share in the comments!

What the heck is insert-normal mode?

Glad you asked!

It is a mode in insert mode where you can execute normal mode commands. Instead of switching modes from insert -> normal -> (do normal mode stuff) -> insert, you can execute normal code commands while remaining in insert mode.

All you need to do is to press C-o while in insert mode.

How can you tell that you're in insert-normal mode? In insert mode, you can see -- INSERT -- indicator on bottom left. If we press C-o, it will change into -- (insert) --.

Important: you can only execute one command in this mode. After executing a normal mode command, you're back to insert mode.

Even with this limitation, this is still a powerful feature. Let's give some examples what we can do with insert-normal mode.

Example 1: Navigating while remaining in insert mode

Let's say while typing, you wanted to add a word in the beginning of current line. Do C-o 0 to jump to the beginning of the line, add the word, then jump back with to the end with C-o $.

Maybe you are adding something after comma in the same line, just do C-o T , and you'll jump right after the comma. C-o $ to jump back to the end.

You can always mix it with other vim motions. I find H, M, L, [], () useful for quick navigation.

Example 2: Centering screen

I am typing this article in vim. As I am typing this, I am at the bottom of the screen. I can center my current position by doing C-o zz.

Example 3: Repeating characters

Say I want to type * 100 times. I can just type C-o 100 i * <esc> (the downside is, this will force you to leave insert mode).

Example 4: Deleting faster

If I wanted to delete a block of text from my current position to an anchor, say a comma, I can just do C-o d T ,

What's next?

These are some ways we can take advantage of insert-normal mode.

I am interested to learn how other devs can take advantage of insert-normal mode. What other ways do you guys think insert-normal mode can be used?

Top comments (16)

Collapse
 
bootcode profile image
Robin Palotai • Edited

Nice! Let me add my favorite here:

Shift-j to merge next line
gqip to reformat paragraph
gqj to reformat over next line

These together are mostly enough to avoid manual reflow to honor line length limit.

Collapse
 
iggredible profile image
Igor Irianto

Good use cases! I never really knew about the last two - thanks! Always learning something new about vim.

Collapse
 
skydevht profile image
Holy-Elie ScaΓ―de

I've known about insert-normal, but I never used it really. I've remapped to and my brain is already wired to leave insert more whenever I pause. But thanks for the insight, it's always helpful to know more about vim!

Collapse
 
drhyde profile image
David Cantrell

I've been vimming for ... I don't know how many years. probably since 1994 because I see that it was in Slackware 2. I didn't know about this. Thanks!

Mind you, now that I know about it I'm not sure whether I'll use it. <ESC> command i is soooo deeply ingrained in my muscle-memory that I'm not sure I'll ever be able to stop doing it :-)

Collapse
 
iggredible profile image
Igor Irianto

I agree. It is ingrained in my muscle memory too - and I haven't used vim for more than 2 years!
Learning to be more thoughtful when I'm typing and breaking from the <Esc> normal-command habit slowly... :) This is going to take a while!

Collapse
 
kr1ssxd profile image
Kriss

Thx for sharing !

What I find helpful is mapping some combinations to shortcuts in insert mode to ease cursor movement and/or scrolling :

" Movement in insert mode
inoremap <C-h> <C-o>h
inoremap <C-j> <C-o>j
inoremap <C-k> <C-o>k
inoremap <C-l> <C-o>l
" Also allow line-wise scrolling
inoremap <C-e> <C-o><C-e>
inoremap <C-y> <C-o><C-y>
Collapse
 
iggredible profile image
Igor Irianto

Woah. This is mind-blowing. I'll have to try this!

Collapse
 
ahferroin7 profile image
Austin S. Hemmelgarn

Despite having used Vim for almost a decade (possibly more, I don't quite remember exactly when I started using it), this is actually completely new to me.

OTOH, I'm not too surprised when I find something I didn't know about Vim (or just vi in general), I still average about one new thing I learn about it every week or so these days, but then, I never really went out of my way to learn anything but the absolute basics originally (and it was Nethack, not Vim, that got me in the habit of using hjkl for movement).

Collapse
 
robertomaurizzi profile image
Roberto Maurizzi

Nice, now I can stop invoking Anubis every time I want to switch to another tab while staying in insert mode ;-)

Collapse
 
iggredible profile image
Igor Irianto

Haha, good use case! Stay away from the underworld >:)

Collapse
 
madox2 profile image
Martin Bielik

My favourite is C-o o or C-o O, starting on next/previous line.

Collapse
 
iggredible profile image
Igor Irianto

I used o/ O all the time and didn't think of that. Good rec!

Collapse
 
mikaoelitiana profile image
Mika Andrianarijaona

😲😲😲😲😲😲 thanks for sharing!!!!! it's a real discovery for me, I really love it.

Collapse
 
iggredible profile image
Igor Irianto

Haha, glad you found it helpful! It totally changed my workflow when I discovered this too!

Collapse
 
bakadesu profile image
バカです

There's mine:

inoremap <up> <C-o>:resize +5<cr>
inoremap <left> <C-o>:vertical resize -5<cr>
inoremap <right> <C-o>:vertical resize +5<cr>
inoremap <down> <C-o>:resize -5<cr>
Collapse
 
iggredible profile image
Igor Irianto

Haha nice!! I thought of mapping my arrow keys as well, good call!