- Lab
- A Cloud Guru
Searching and Replacing in Vim
Searching for text is more efficient than scrolling and looking for a phrase or setting. Being able to find and subsequently replace an instance of a word or phrase, or many instances, is an important skill for anyone who works with large text files, be they configuration, code, or prose. In this hands-on lab, you will practice finding the next instance of characters, a word or phrase, either forward or backward. You'll also practice searching and replacing text using various helpful options, and also how to easily and economically repeat previous actions for smoother editing. Finally, you'll practice on a small scale the replacement of a given string in multiple files using scripting and the `ex` mode of Vim.
Path Info
Table of Contents
-
Challenge
Search for Characters and Strings, and Search for Words in Specific Positions
-
Open the Vim editor:
vim
-
Create a working file (and open it for editing) with:
:help windows :w ~/vimwindows.txt :q :e ~/vimwindows.txt
-
Go to the top of the file with
gg
and then navigate to the line that starts withEditing with multiple...
-
With the cursor on the
E
in Editing, find the next occurrences of ane
by pressing:fe fe
-
Attempt to find the next occurrence of the
|
symbol by pressing:f|
Note: The
f
command can only find on a line, not across multiple lines. -
Find a string (and move to a line) with:
/The ENTER n (To go to the 2nd found occurrence)
-
Now try the
f|
command again:f| f| (to go to the next |) F| (to go to the previous |)
Note: This will work with most any character or numeral.
-
Go to the top of the file again with
gg
. -
Find a string that is at the end of a line with:
/window\. ENTER
-
Then find a numeral only at the beginning of a line with:
/^1. ENTER n
-
This finds both the
1.
and the10.
strings because the.
is being used to mean any character, not specifically a period. Redo the command and make sure the.
character is not being used as a Regular Expression with:/^1\. ENTER
-
This will result in finding the line beginning with: ```
-
Introduction ```
-
Return to the top of the file with
gg
-
Search for all instances of the string
the
with:/the ENTER
-
Make your search results more visible with the command:
:set hlsearch
-
Now set your highlight to a more pleasing color schema with:
:highlight Search ctermbg=grey ctermfg=green
-
Next, return to the top of the file and search for the strings that start with
the
, using:gg /\<the ENTER
-
Now search for strings that END with
ing
:/ing\> ENTER
-
Finally, search for the separate word
a
with:/\<a\> ENTER
> Note: This will show only the worda
, nota
in as a part of other words or strings.
Remember: If you can find it, you can do something useful like replace it...
-
-
Challenge
Search and Replace Text, Then Search for and Delete Text
Note: You must have done Steps 1 and 2 in Task 1 before you are able to do Task 2.
-
Return to the top of the file with
gg
. -
Now turn off search highlighting with:
:set nohlsearch
-
Search for the string
window
and replace it withWINDOW
with the substitute command::s/window/WINDOW/
Note that only a single instance of
window
was replaced, on the current line. -
Search for the string
window
and replace it withWINDOW
on all lines, but only the first instance on a line with::%s/window/WINDOW/
-
Note that the first instance was replaced, but not all instances, and remember the number of substitutions.
-
Return to the top of the file with
gg
and restore the file to the original state by pressingu
until you get the message:Already at oldest change
-
Run the substitute command again with the
g
addition to replace all instances ofwindow
withWINDOW
with::%s/window/WINDOW/g
-
Note the larger number of substitutions made.
-
Restore the file to it's original state by pressing
u
until the get the "oldest change" message again. -
Repeat the previous substitute command by picking it from the history by pressing:
q: (No, it's not a typo...) k (or the up arrow) A (to add to the command) %s/window/WINDOW/gc (Add the c) ENTER
-
Use the prompt interface to experiment with the confirmation actions:
-
y
Substitute match -
l
Substitute match and quit -
n
Skip match -
a
Substitute this and all other matches -
q
Quit substitutingNote: Ensure that you use either
a
for all,l
for last, orq
to quit the substitute command instance.
-
Restore the file to it's original state by pressing
u
until the get the change message again, thengg
to go to the top of the file. -
Search for and delete all instances of the word
buffer
with::%s/buffer//g
> Note: Notice the large number of deletions made, -
Now restore the file with
u
, andgg
, and delete just the instances ofbuffer
that are a separate word with::%s/\<buffer\>//g
> Note: Notice the lower number of deletions made. -
Next, restore the file with
u
, thengg
and delete just the instances of the word buffer that are followed by a.
(the period character) with::%s/buffer\.//g
> Note: Notice that many less deletions are made, -
Lastly, restore the file with
u
and do the deletion for just the first 50 lines of the file with:gg :1,50 s/buffer//g
Note: Notice the small number of deletions that occurred due to the line range restrictions.
-
Exit the demonstration file without saving any changes with:
:q!
-
-
Challenge
Search and Replace in Multiple Files
Note: You'll be making a update directory, copying some HTML files into it, and then writing two scripts in Vim to make mass changes in those files.
-
As
cloud_user
, create a subdirectory in your home directory with:mkdir ~/updatehtml
-
Change into the
updatehtml
directory with:cd ~/updatehtml
-
Copy a couple of HTML files to the new subdirectory for use in this lab with:
cp /usr/share/doc/nano/*.html .
Note: We'll be replacing some tags with others for the sake of formatting, according to a directive from the Design Team.
-
Verify the number of
<h2>
tags in the target files with:grep -w h2 *.html
-
Then run the command again and get a line count with:
grep -w h2 *.html | wc -l
-
Note this number, for later comparison.
-
Open a new buffer/file in Vim that will be the include file for our update script with:
vim update.vim
-
Enter Insert mode with the
i
command and put the following in the body of the file:%s/h2/h3/gi write quit
-
Save and exit the file with:
:wq
-
Open another new file in Vim that will become the update script for our replacement operation with:
vim updatehtml.sh
-
Enter Insert mode with the
i
command and put the following in the body of the file:#!/bin/bash for file in *.html; do vim -e -s $file < update.vim done
-
Save and exit the file with:
:wq
-
Confirm the number of
<h2>
tags in the target files again with:grep -w h2 *.html | wc -l
-
Run the update script against the HTML files with:
bash updatehtml.sh
-
Confirm that the updates happened to the HTML files with:
grep -w h2 *.html | wc -l
-
If your count is
0
, congratulations! If not, please check your script and include file for any syntax or capitalization errors. Ensure that the files below are all in the current directory and that it's the~/updatehtml
directory:faq.html nano.html update.vim updatehtml.sh
Remember: You can change the search and replace strings in the
update.vim
include file to search for and replace anything in any file you want to. -
What's a lab?
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Provided environment for hands-on practice
We will provide the credentials and environment necessary for you to practice right within your browser.
Guided walkthrough
Follow along with the author’s guided walkthrough and build something new in your provided environment!
Did you know?
On average, you retain 75% more of your learning if you get time for practice.