Going through the advanced Vi(m) settings
In the first part of this chapter, we learned some basic Vim operations, which were moving around, copying and pasting, saving, and exiting. Let's take care of some more advanced operations, such as working with find and replace, regular expressions, and similar concepts.
Getting ready
We need to leave our CLI virtual machine running. If it's not powered on, we need to power it back on.
How to do it…
Finding content in Vim is a multi-step process, and it depends on a couple of things. First, it depends on the direction that we want to take, forward or backward, as there are different key sequences for these operations. Let's open the /root/words
file again to find some text:
Vim /root/words
Let's start by finding the word fast
. For that to work, we need to use the /
character from normal mode, as it tells Vim that we're about to use the search
function. So, /fast
will search for the words fast forward
from our cursor. This is the expected result:
If we now press Enter and then the n key, we will search for the next appearance of the word fast
. This is the expected result:
However, if we want to find the 10th appearance of the word fast
, we need to either press the correct key sequence or use a regular expression. Let's start with a key sequence, which is going to be (again from normal mode) 10/fast
. This is the expected result:
If we want to find the previous appearance of our word (basically, search backward), we need to press the N key (capital N). This is the expected result:
Let's now do a bit of search and replace. Let's say that we want to find all appearances of the word airplane
and change them to metro
, starting from the beginning of our file. The key sequence used for that would be gg (to go back to the file beginning) and then :%s/airplane/metro/g
, followed by the Enter key. This is the expected result:
This syntax presumes the automatic replacement of all occurrences of the word airplane
with the word metro
placed anywhere in the file. If we just wanted to replace the first appearance of a string in any line, we need to first find that word by using the /word
key sequence. Then, we need to use the :s/word1/word2/
key sequence to only change the first appearance of word1
with word2
. Let's use the word airship
for that example and change that word to ship
. If we type in /airship
, followed by the Enter key, Vim will position us to the first next appearance of the word airship
. If we then use the :s/airship/ship/
key sequence followed by the Enter key, we should get this result:
It's a subtle difference, but an important one.
We could also use many more commands in vi – for example, using a dot sign (.
). That can be used to repeat the last change made in normal mode, which you might also find to be very useful.
We're going to stop here, as we will cover more advanced text search patterns by using regular expressions in Chapter 7, Network-Based File Synchronization.
How it works…
String replacement in Vim works by using an external command called sed
, a stream editor. This command is regularly used by system engineers all over the world to quickly replace simple or complex text patterns of any given file (or multiple files) to another complex text pattern. It uses regular expressions as a basis (explained in Chapter 7, Network-Based File Synchronization), which means that, by default, doing search and replace in Vim is quite powerful, albeit a bit complex, as we need to learn the ins and outs of sed and the way Vim treats it as a plugin.
That being said, most of us focus on the quite powerful part of the last paragraph, as using a Vim/sed combination to quickly replace complex text patterns yields fast and precise results – as long as we know what we're doing, of course.
There's more…
Using these concepts requires a bit of extra reading. So, we need to make sure that we check the following additional links:
- Vim Tips Wiki – search and replace: https://Vim.fandom.com/wiki/Search_and_replace
- Vim tips: the basics of search and replace: https://www.linux.com/training-tutorials/Vim-tips-basics-search-and-replace/