Tuesday, February 9, 2021

Vim - Replace Mode

When learning Vim, it's important to understand its modes, particularly the Replace Mode where you can replace text.
Photo by Alex Knight on Unsplash

Once you installed Vim, learned the basics of Vim Modes and understood Normal mode, it's time to learn yet another Vim mode: Replace Mode. This is the mode where you replace text instead of the more common insert done in Insert mode.

What you need to know

The Replace mode allows you to replace existing text by directly typing over it. It's similar to when you press the <Insert> key in a any other text editor. To enter Replace mode, either: 

  • press R in Normal mode 
  • press <Insert> while in Insert mode

Once you enter Replace mode, you should see in the message -- REPLACE -- in the status bar:

Press R in Normal mode to get to replace mode

From here, simply type or text and each original character will be replaced by the new keys. If no character exists, Vim will keep adding as if you were in Insert mode.

Interesting Behaviors

But the Replace mode is a little more than a text editor with the <Insert> key on. Here's what you should observe:

  • Some of the Insert mode keywords do not work in this mode
  • <Backspace> will not delete backwards but bring back characters previously replaced
  • <Enter> creates a line break and doesn't delete any character
  • Ctrl+W and Ctrl+U bring back replaced characters instead of deleting as in Insert mode

To learn more about the Replace mode, open its dedicated manual with:

:h replace-mode

Getting to Normal Mode

To get back to Normal mode from Replace mode (or one of Vim's other modes),  press <Esc> or <Ctrl-C> or <Ctrl-[>.

Virtual Replace Mode

For reference, there's also another mode called Virtual Replace mode which can be activated with gR. It's similar to replace mode but instead of replacing actual characters in a file you are replacing screen real estate so that characters further on in the file never appear to move.

To know more about the Virtual Replace mode, run:

:h vreplace-mode

Mode-specific help

If you want to know more about specific keys, Vim also has an intelligent mechanism to get you to the help quickly. It follows this pattern:

What Prepend Example
Normal mode command :help x
Visual mode command v_ :help v_u
Insert mode command i_ :help i_<Esc>
Command-line command : :help :quit
Command-line editing c_ :help c_<del>
Vim command argument - :help -r
Option ' :help 'textwidth'
Regular expression / :help /[

We hope you get used to the above syntax and use it regularly in you your Vim journey. It will not only help you learn more about Vim Vim but also to memorize the commands better.

Conclusion

On this post we continued in our Vim journey by learning a bit more about Vim's Replace mode. If it seems complicated, don't be concerned. It takes years to master Vim but be sure that the more you learn, the more you realize that time is that secret ingredient in getting comfortable, becoming proficient and efficient with Vim.

Learning Vim is like learning a musical instrument. It takes time, effort and discipline but once you master it, the gains are endless. You definitely won't regret.

See Also

Any comment about this page? Please contact us on Twitter

Featured Article

Vim - Ex Mode

When learning Vim, it's important to understand its modes, including the  Ex Mode  where you can continually type your commands u...

Popular Posts