Making small changes

3.9
(89)

Mastering Vim is all about efficiency of movement and editing. No matter the size or complexity of your edit, there is an optimal way in Vim. Even if you need a single-character change, Vim’s got your back. Perhaps you have a small typo. For example, the period below should be a comma.

When in Rome. do as the Romans do.

You could move your cursor to the character, enter insert mode with i, then backspace, enter . and exit insert mode with ESC. Or you could x to delete it first before entering insert mode.

But, of course, even for this small change Vim has a more efficient way: Enter r, the replace command.

Using r{char} we can replace the character under the cursor with {char} without ever leaving command mode. Most commonly, I combine this with f to jump to the character I wish to replace. For example, if I wish to replace that period with a comma and my cursor is at the beginning of the line, I use f.r,.

When you have to replace one character with another, Use the pattern f{char} r{replacement char} move your cursor to the next occurrence of {char} and replace it with {replacement char}.

You can also use a count before the r to replace multiple characters at once. For example, to make the next 10 characters into asterisks, use 10r*.

Lastly, try r followed by ctrl-e to replace the character under the cursor with the character from the line below. Or use ctrl-y to change the current character to the one from the line immediately above.

How useful was this tip?

Average rating 3.9 / 5. Vote count: 89

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