Professional Documents
Culture Documents
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
This will open the file and place your cursor to the left of the first occurrence of text by
default.
rn
: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
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
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
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
[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
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