Replacement techniques in vi you should know
Sometimes, you don't want to insert new text into a file but instead replace a letter, a word, or a text sequence. Vi provides some handy commands to do such replacements. Let's have a look at them!
Replacing Text
Vi provides the change command (c) to replace any text in your file. Using the change command will put you into insert mode. The change command needs a movement command to be provided as well to define how much text should be replaced. For example, if you want to replace to the end of a word, you would type cw
followed by the text you want to insert.
Let's assume we have the following file content:
This is some example file.
If you wanted to change the word "file" to "content", you would do the following:
- navigate to the beginning of the word "file"
- type
cw
- type "content"
- press ESC to go back to command mode
You could as well e.g. change text until the end of the line by typing c$
or to the beginning of the line by typing c0
. Keep in mind that $
and 0
are movement commands in vi as well, i.e. moving to the end of the line and to the beginning of the line, respectively. Feel free to go fancy with the movement commands appended to the change command.
Replacing lines
Vi offers the possiblity to replace whole lines with one command: cc
.
Let's go back to the previous example and assume we have the following text in our file:
This is some example file.
If you wanted to change the line to "We are learning how to replace text.", you could do the following:
- navigate to the line you want to change (which is pretty simple in our example)
- type
cc
- type "We are learning how to replace text."
- press ESC to go back to command mode
Replacing characters
Vi makes it easy to replace single characters. To replace a single character, you can type r
followed by the character you want to be at the cursor position.
Let's assume we have the following text, containing a typo, in our file :
This is same example file.
To fix the typo, you would do the following:
- navigate to the 'a' in "same"
- type
ro
(r
followed by 'o')
Note that r
does not set you to insert mode but let's you stay in command mode. Therefore, pressing ESC is not necessary.
Bonus: Changing Case
You can change the case of a letter by typing ~
. This will transform an uppercase letter to a lower case letter and vice versa.
Recap
In this article we learned
- that we can change text by using the change command, e.g. we can use
cw
to replace text from the cursor to the end of the word - that we can replace whole lines using the
cc
command - that we can replace single characters using
r
followed by the desired character - that we can change case by typing a tilde (
~
)
You can check the following table to get another overview about the commands and their effects.
Command | Result |
---|---|
cw | Replace the text beginning from the cursor position to the end of the word |
c$ | Replace the text beginning from the cursor position to the end of the line |
c0 | Replace the text beginning from the cursor position to the beginning of the line |
cc | Replace the whole line with the desired text |
r | Replace a single character |
~ | Change case of a character |