Normal mode replacement

3.8
(46)

One of Vim’s many selling points is the usefulness of normal mode. Except for the occasional session when authoring original code or content, we spend most of our time in normal mode, navigating, searching, reading and reasoning. I prefer to make use of Vim features which allow me to stay in normal mode whenever possible. This is where normal mode replacement with r shines.

While r is a simple feature, any time you need to swap a single character it can be performed without leaving normal mode. Simply navigate to the character in question and hit r followed by the character you wish to replace it with and… done! The replacement has been made, never leaving normal mode. This saves several steps: The alternative would be to erase the character with x, and then enter insert mode with i and then finally transition back to normal mode with <Esc>.

You can also replace more than one character. Here are some handy variants of the single character replace which can come in handy.

  • Prepend the replacement with a number to replace an arbitrary set of charaters from the current position. 5r* will replace the current and next four with an asterisk.
  • Press r followed by <ctrl-e> or <ctrl-y>, and the current character is replaced with the character directly below or above the current character. This is thematically related to our recent post copying words from above or below.
  • Press r is followed by <Enter> and the next character will be replace with a line break. Prepend with a number and the next x characters will be replaced by a single line break.

Take a look at the video below. Here’s how I try out two of these:

  • Go to the mistaken n with /n<Enter>
  • Replace the n with r followed by n
  • Find s<space>, go to the next occurrence with n
  • Then replace the s and the space after it with 2r<Enter>

How useful was this tip?

Average rating 3.8 / 5. Vote count: 46

No votes so far! Be the first to rate this tip.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Written by

Andy Libby

36 Posts

Rider of bicycles. Writer of code. User of Vim.
View all posts