Normal mode replacement
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 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
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.
<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.
ris followed by
<Enter>and the next character will be replace with a line break. Prepend with a number and the next
xcharacters 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
- Replace the
s<space>, go to the next occurrence with n
- Then replace the
sand the space after it with
We are sorry that this post was not useful for you!
Let us improve this post!
Tell us how we can improve this post?