Categories
Linux Tutorial Series

Linux Tutorial Series – 92 – Not-so-basic usage of vi – searching, global search-and-replace and managing multiple files

Here is the video version, if you prefer it:

Let’s continue our discussion of vi and learn how to search for and replace strings (which are just consecutive characters) within a file, as well as how to manage multiple files. This is modeled after (Shotts, 2019)⁠.

The file we have is:

mislav@mislavovo-racunalo:~/Linux_folder$ cat aba.txt

Money is how we transfer wealth

Some stuff Abba Money

Money Money

Money Money

It's the Money

In the Rich Man's World

Let’s open it up in vi with vi aba.txt.

First, let’s search for the word Money. To do that, type / (while in command mode (which is default)) and then type Money. Then press Enter (on your keyboard). That will find the first match. To go to the next match, press n.

Great. Let’s now talk about global search-and-replace. What does this “global” search-and-replace mean? It means, in essence, we are looking at the entire file. If this weren’t the case, then only the first occurrence of the string we are looking for in each line (if it exists) would be replaced and other occurrences on the same line would be skipped over.

Here is the syntax for global search-and-replace (I remember it by heart):

:%s/<originalString>/<replacementString>/g

Let’s break this down – : signifies the beginning of a command, % says “search everything from the first line to the last line”, s specifies the search-and-replace operation, originalString is the original string we are searching for and replacementString is the string we are replacing originalString with. g means global.

Let’s write the following command and then press Enter:

:%s/Money/Mustard/g

Now we have:

Mustard is how we transfer wealth

Some stuff Abba Mustard

Mustard Mustard

Mustard Mustard

It's the Mustard

In the Rich Man's World

~

A sidenote: If you wanted to confirm every single replacement (by typing y), then you would add c to the end of the above command, so it would be:

:%s/Money/Mustard/gc

Let’s now talk about how to manage multiple files. First, write your changes (:w). In order to open up another file within vi, type

:e <filename>

and press Enter. I have written :e ab.txt. ab.txt is in the same folder as aba.txt and contains the following lines:

AB

Ab

aB

ab

To follow along, feel free to create the ab.txt file via a text editor with a graphical user interface and then type :e ab.txt. vi will automatically switch to the newly opened file.

To switch between the two files (aba.txt and ab.txt) type :bn and press Enter. Running :bn once gets you to aba.txt.

Now let’s talk about how to copy contents from one file to another. I use the following procedure:

  1. Place yourself in the file you want to copy the line from and press yy (to copy the line your cursor is on)
  2. Type :bn to switch to the other file (and press Enter)
  3. Type p or P depending on where you want to paste the line (p pastes it below the current line, P pastes it above the current line; remember to press Enter)

To insert an entire file in another file, do this:

  1. Place your cursor on the desired location in the file you want to insert another file
  2. Type in :r <fileToInsert> where <fileToInsert> is the filename which to insert
  3. Press Enter

Let’s combine these two steps. Let’s first copy the line “Mustard is how we transfer wealth” from aba.txt to ab.txt, below the first line. Do it using the above instructions. Then you can switch to aba.txt. Remember to write the changes with :w – otherwise you get an error, or you can use the exclamation mark (as suggested by vi) to forcibly do change the file back to aba.txt.

Then, let’s copy the entire ab.txt file to aba.txt file at the end. Use the instructions above.

In the end, you get this in aba.txt:

Mustard is how we transfer wealth

Some stuff Abba Mustard

Mustard Mustard

Mustard Mustard

It's the Mustard

In the Rich Man's World

AB

Mustard is how we transfer wealth

Ab

aB

ab

~

Now, write :wq to write changes and quit vi at the same time and press Enter.

There you have it. You now know how to search, conduct global search-and-replace and manage multiple files in vi. This concludes our vi journey.

Thank you for reading!

References

Shotts, W. (2019). The Linux Command Line, Fifth Internet Edition. Retrieved from http://linuxcommand.org/tlcl.php. Pages 176-184

NewsletterUpdates on interesting things I am doing

Subscribe to my newsletter to keep abreast of the interesting things I'm doing. I will send you the newsletter only when there is something interesting. This means 0% spam, 100% interesting content.

Leave a Reply

Your email address will not be published.