You are on page 1of 4

SUSE Linux Enterprise Administration

4- 1 Use vim to Edit and Manipulate Text Files

Description:
In this exercise, you will use the vim editor to open vim with various options, create

y
nl
new files, save and exit vim.

O
Task 1: Open a terminal session

e
te Us
1. On server2, as the tux user, open a terminal by clicking on the Activities menu and
type:
terminal

bu r
tri ne
When the Terminal icon appears, click on it to start a terminal session.

is rt
Task 2: Open an Existing File for Editing with Various Options
D Pa
1. Copy an existing file to your home directory with:
cp /usr/share/doc/packages/zip/WHATSNEW ~/zipwhatsnew.txt
ot d
N an

2. Open the new file for editing/display with:


vim zipwhatsnew.txt
D al

This will open the file and place your cursor to the left of the first occurrence of text by
default.
rn

3. Quit the VIM editor with:


te

:q
o
In

4. Open the file again and automatically position your cursor on the first line that has the
word zip30.ann in it:
SE

vim +/zip30.ann zipwhatsnew.txt


Verify that your cursor is on the first line in the file with the filename zip30.ann in it,
which should be line 6.
SU

5. Open the file again and automatically move your cursor to line 25 with:
vim +25 zipwhatsnew.txt
Verify that you are indeed on line 25 of the file by inspecting the positional indicators on
the right bottom of the editor screen, which should read 25,1.
6. Quit the vim editor with:
:q

48
SUSE Linux Enterprise Administration
Task 3: Navigate in and Save an Existing File as a New File with Changes
1. Open the zipwhatsnew.txt file with the vim editor:
vim zipwhatsnew.txt
2. Navigate to the top of the file and then to the blank line just above the 4 th full paragraph
that starts with the text - AES encryption with the keystrokes:
1G

y
4}

nl
This should move you to the blank line between the two paragraphs.

O
3. Navigate down a line to the start of the line that begins with - AES encryption with the
down arrow key or use the keystroke:

e
j

te Us
4. With the cursor on the text line, delete the line from the buffer with the keystrokes:
dd

bu r
5. Using your cursor/arrow keys, navigate so that the cursor is on the first letter of the word

tri ne
Unicode three lines below your current line, and delete the word with the keystrokes:
dw is rt
D Pa
6. Re-add the word Unicode back in to the buffer, entering Insert mode with:
i
Uni-code
ot d
N an

7. Add a space to separate Uni-code from the word paths, then:


[ESC]
D al

8. Write a new file from the changed buffer with the command:
rn

:w zipwhatsnew-edited.txt
te
o

Notice that the filename at the bottom left of the interface indicates have just created a
In

[New] file.
9. Exit the zipwhatsnew.txt file WITHOUT saving the edits to the file with:
SE

:q!
SU

Task 4: Search and Replace Text in a File


1. Edit the zipwhatsnew-edited.txt file with:
vim zipwhatsnew-edited.txt
2. Go to the top of the file and begin searching down the file for a string with:
1G
/Unicode
[ENTER]
3. Notice that your cursor automatically goes to the string as you type it.

49
SUSE Linux Enterprise Administration
4. Now search for more instances of the string with:
n
n
n
5. Notice that your cursor goes to the next string you searched for, and the next etc.
6. Now reverse search back up the file with:
N

y
N

nl
7. Search for instances of a text string in the buffer with:

O
1G
:%s/zip/Zippy/g

e
te Us
Notice we had you start at the top of the buffer, so all instances of the found text are
replaced throughout the entire file.

bu r
Notice also that the search and replace uses the g character to indicate that you are

tri ne
wanting to globally replace zip with Zippy, otherwise it would only replace the first
instance of each found string.
is rt
8. Undo your search and replace, restoring the file to it’s initial state with:
D Pa
u
ot d

This will work if you have not saved the buffer to disk, if you save to disk, the undo
N an

feature will not work.

9. Quit the file without saving:


D al

[ESC]
rn

:q!
te
o
In

Note:
The search and replace syntax in vim is similar to that of sed, with the /g
SE

characters at the end of the command invoking global replacement of the text
string, as opposed to just replacing the first found instance of the string on a given
line.
SU

Task 5: Compare Two Similar Files for Differences


1. From the command line, compare the two versions of the GPL License file with:
vimdiff zipwhatsnew.txt zipwhatsnew-edited.txt
You should see a dual-pane interface with the two files loaded and synchronized,
showing the differences between them marked in various colors.

50
SUSE Linux Enterprise Administration
2. Exit the vimdiff instance, including both files with:
:qa
3. Exit the terminal with:
exit

Summary:

y
nl
In this exercise, you used the vim editor to open, navigate within, delete text from, add

O
text to and save another copy of a changed file buffer, as well as showing you how to
search and replace text and compare two similar files for their differences.

e
te Us
(End of Exercise)

bu r
tri ne
is rt
D Pa
ot d
N an
D al
rn
te
o
In
SE
SU

51

You might also like