- Published on
Doing minor changes without leaving insert mode
It's a good practice to leave insert mode and enter normal mode as soon as you finish typing something in VIM, but sometimes you might want to perform some minor modifications without leaving insert mode, perform the modifications, and then immediately return to insert mode.
For example, you have the following text in your buffer:
This is a note that I want to share
^ cursor is here
And you want to add critical
before the text note
, here are one of the solution:
- Press
<Esc>
to leave insert mode - Press
6b
to navigate to the beginning of wordnote
- Press
i
to enter insert mode, typecritical
- Press
<Esc>
to leave insert mode - Press
A
to go back to the end of the line and enter insert mode again
You will find that step 4 and 5 are tedious because you have to leave insert mode and re-enter it. In VIM, there is a command <ctrl-o>
that allows you to execute a single normal mode command and return to insert mode. With the same example above, the steps would be:
- Press
<Esc>
to leave insert mode - Navigate to the beginning of word
note
by pressing6b
- Press
i
to enter insert mode, typecritical
- Press
<ctrl-o>A
to go back to the end of the line and enter insert mode again
<ctrl-o>
can be used with other normal mode commands as well. Here are some examples:
<ctrl-o>O
: insert a new line above current line<ctrl-o>o
: insert a new line below current line<ctrl-o>cc
: change the current line<ctrl-o><ctrl-o>
: go to the previous cursor position and keep in insert mode