Change, Replace, and Substitute in Vim

Change, Replace, Substitute In

The vim text editor is one of the most popular terminal-based editors. Its richness of commands and flexibility allows users to code at the speed of thought. Vim provides many commands that sometimes have overlapping functionalities. These functionalities not in any sense redundant, instead it gives the user more freedom on choosing how to do a specific task.

Introduction

Often we need to make changes to our existing text files. Vim offers mainly three high-level commands-replace, change and substitute to do this. Though these 3 words are synonyms because they do the exact same task, they differ how the task is performed and so the choice of command comes down to how complex the task at hand is. In this article, we will discuss the primary differences between these three commands and where and how to use them.

Point of DifferenceReplaceChangeSubstitute
Primary Keystroker{char}c{motion}[count]s
General functionReplaces the current character or a set of characters.Deletes the characters and places the user in enter mode.Deletes a range of characters and places the user in Insert mode.
Primary set of differences between the three commands.

Quick heads up: The {} indicates requires set of arguments i.e. the {} including the text inside must be replaced by a valid value. The [] indicates optional arguments. Modes are also represented using {} but the starting letter is capital, for example: {Insert} and {Visual}. No mode sign indicates normal mode.

Recommended read: 10 ways to exit VIM editor

Using the Change, Replace, and Substitute Commands on VIM

1. Replace command in VIM

The replace command is suitable for very small changes that you want to make in the text(buffer).

Pressing the character R in Normal mode takes the user to the Replace mode. Replace mode is like Insert mode but except for the fact that the character you type overwrites the character already present at that position. If there are no more characters present, it will behave like Insert mode allowing you to insert as many new characters as you like.

Pro Tip: If you have made a small typing error in replace mode, just press backspace without exiting the replace mode. This will revert the changes made to the preceding character in replace mode.

r{char}

If you are looking to make very minimal changes, like replacing a single character without entering the Replace mode you can just use r{char}. This command replaces the current character with {char}.

Replace mode in VIM
Fig 1: Example of replace command

2. Change command in VIM

While the replace command is useful, it can become problematic for altering large chunks of text. Thankfully the change command comes in handy for such purposes. The change command deletes the characters based on a motion and enters the Insert mode. The change command can be used in conjunction with many other commands for maximum productivity.

c{motion}

Deletes the text from the text depending on the motion keys pressed. Here are some usage examples:

  • ch – Deletes the previous character and start Insert mode.
  • ck – Deletes the line above the current line and start Insert mode.
  • c$ or C – Deletes from the current character to the end of the line and start Insert mode.
  • cG – Deletes from the current line to the end of the text and start Insert mode.
{Visual}c

Change command can be very helpful in Visual mode. The change command deletes the selected block of text and enters the Insert mode.

["x]c{motion}	

The user might want to keep the deleted text in a register, just for safety or to be pasted elsewhere. The following command deletes the {motion} text into the register X and enters the Insert mode.

Vim Change command
Fig 2: Example of change command

3. Substitute command in VIM

The substitute method in vim acts like the change command, but instead of taking the motion as a parameter, it takes in the number of lines and character as a parameter. It can be particularly helpful if you are thinking to delete the next few characters/lines, without going into much hassle of the motion keys in the change command.

[count]["x]s

Deletes [count] number of character to the right of the current cursor [to the register x] and puts the user in Insert mode. The s command performs similarly as cl.

[count]["x]S

Deletes [count] number of lines below the current line and puts the user in Insert mode.

Vim Substitute command
Fig 3: Example of substitute command

Conclusion

This brings us to the end of the article. I hope you have learnt something new about these three important commands in vim. Since vim allows a lot of commands that can be used on top of these commands, exploring all the possibilities is not a good idea.

The complicated command or shortcuts people use makes their daily work comfortable, you don’t need to learn all the complicated things at the beginning to start hacking in vim.

With few days of practice, you can slowly catch up with the complicated conjunctions, once you feel necessary. You can always get some help from the man page using the command :h [search-term] inside vim.