When you start learning Vim you’re introduced to it’s most used modes:
VISUAL. That’s fair enough! You must get used to these modes because they’ll be the core of your workflow.
But… Vim has more modes! Each one of them matches some specific needs, and that’s the case of the
REPLACE mode is a very useful tool to edit tables, and you’ll probably find some other use cases for it as well.
When you’re in this mode, vim will overwrite what’s under your cursor with what you’re typing:
Let’s understand how it works and it’s behaviours.
Before start to use it, it’s important to know a Vim improvement (not present on Vi), that is the Virtual Replace (
VREPLACE) mode, and understand why does it exist.
VREPLACE mode isn’t available when Vim is compiled without the
+vreplace feature. On terminal type
vim --version and check if
+vreplace is there.)
— From Vim Docs:
In Replace mode, one character in the line is deleted for every character you type…
… is similar to Replace mode, but instead of replacing actual characters in the file, you are replacing screen real estate …
— What’s the point?
The point is basically the
Let’s say your
<Tab> is set to 4 spaces, that means it’s (4 spaces of screen real estate), and you’re overwiting a spacing provided by the
<Tab> character, with the letter
a, let’s see what happens:
When you type
a to substitute the
<Tab> character, it substitutes the 4 empty screen real estates to only 1 screen real estate, presenting the
a character. The table looses the formatting.
On Virtual Replace mode, you’re substituting screen real estates, that means, if you have a table or a file with tab spacing/indentation, the content will not loose it’s actual position and the table will keep the formatting.
How to use it:
You already know what to expect from each one of these two modes, so let’s go to the commands.
Put the cursor where you want to start replacing and:
Delete or Undo in REPLACE / VREPLACE modes
If you delete characters (with
<C-w> , or
<C-u>), what happens is that you delete the changes:
Now you know almost everything about these modes! They are simple and useful, play with it and keep them in mind.
If you experience some unexpected behaviour, just type
:help replace-mode and learn a little bit more… or ask me on the comments section, I’ll be glad to help!
Useful links & references:
- Vim Documentation (
- “Mastering Vim Quickly” Book (and screencasts) by Jovica Ilic. It’s an awesome book, make sure you subscribe to the newsletter to get free Vim tips and book excerpts on your email.
(👆 This is an affiliate link. If you got any value from this post and are also interested in buying the MVQ book/screencasts, please consider visiting the affiliate link and you’ll be gifting me a coffee ☕ or a beer 🍺.)
- alldrops.info is the TRUE and FOREVER FREE home for the previously used medium.com/vim-drops publication. At alldrops.info you’ll always find all posts without Medium.com restrictions.
- If you have any doubts or tips about this article, I’d appreciate to know and to discuss it on the comments section, or via email.
- Do you have any other vim tips? Would you like to publish that on this blog? Please send an email to vimdrops.
- As English is not my native language, I apologize for errors. Corrections are welcome.
- Contact: vim [@] alldrops [.] info.