Change, Replace, and Substitute substitution in Vim

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 are not in any sense redundant, instead, they give the user more freedom in 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 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 a 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 capitalized, 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 starts Insert mode.
  • ck – Deletes the line above the current line and starts Insert mode.
  • c$ or C – Deletes from the current character to the end of the line and starts Insert mode.
  • cG – Deletes from the current line to the end of the text and starts Insert mode.
{Visual}c

Change commands 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 characters as a parameter. It can be particularly helpful if you are thinking of deleting the next few characters/lines, without going into much hassle of the motion keys in the change command.

[count]["x]s

Deletes [count] the number of characters 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] the 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 commands or shortcuts people use make their daily work comfortable, you don’t need to learn all the complicated things at the beginning to start hacking in vim.

With a 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.

What is the role of command in vim text editor?

In vim, a command refers to an action that you can perform within the text editor. This can include operations such as find and replace, search, replace in vim, and more.

How can I perform a substitute operation in vim?

To perform a basic search and replace in vim, you might use a tutorial to understand the substitution command better. substitute text in Vim, you can use the substitute command. This command allows you to find a pattern within the text and replace it with another string.

How can I replace all occurrences of a specific term in Vim?

To replace all occurrences of a term in Vim, you can use the appropriate commands to search for the pattern and replace it with your desired text throughout the document.

Is it possible to find and replace in vim based on a specific search pattern?

Yes, you can perform find and replace in vim by specifying a search pattern that matches the text you want to replace. This allows for targeted replacements within the content, it’s often necessary to search for a pattern and perform a substitution.

What is basic search and replace in Vim?

Basic search and replace in Vim allows you to quickly find and replace text within your file using simple commands.

How can I do a basic search and replace using Vim?

To do a basic search and replace in Vim, you can use the `:s` command followed by the search pattern and the replacement text.

Can I use find and replace on a specific word only?

Yes, you can use the `:s` command followed by the search word and replacement text to replace only that specific word.

How do I replace text in multiple lines in a file in Vim?

You can use the global flag `:s` command to replace text in multiple lines throughout the file in Vim.

How can I navigate to specific lines in a file for find and replace?

You can use commands like `:line_number` to navigate to specific lines in a file before performing find and replace in Vim.