0% found this document useful (0 votes)
23 views

Oracle Linux Vi Overview

Uploaded by

Cristina
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Oracle Linux Vi Overview

Uploaded by

Cristina
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Welcome to the Oracle Linux vi Overview training.

In this video we will demonstrate creating, editing and


saving files using the vi editor in Oracle Linux.
Vi is a text editor that can be used to edit plain text or ASCII files in Linux. There are a few ways you can create
a new text file with vi. You can open vi without a file name, and then supply a file name when saving the
document. Or you can open an existing file or create a new file, by passing the file name as an argument to vi.
Let’s look at some examples.
In our first example, we launch vi without a filename. Vi has several modes of operation. When you start vi, it
opens into command mode. Command Mode interprets keystrokes as commands, allowing you to perform
things like insert, delete, and file navigation. Insert Mode allows text content to be inserted into the
document. There are multiple ways to switch from command mode to insert mode which we will
demonstrate in this video. To return to command mode from insert mode, you press the ESC key.
To exit vi, press the : key and then you can either quit the file without saving, or write your changes and then
quit. We decide we don’t want to save this file. So we press the : key, then q for quit, and then we press the
<enter> key. If you made any changes, vi won’t let you quit the file. If you want to quit without saving the
changes, type :q!.
Now we’ll start vi with a file name. Notice when we list the directory contents, we don’t have a file named
file.txt. When we type vi file.txt, it opens a new blank document called file.txt. To enter insert mode, type a
lower case i, which will allow you to insert text at the cursor location. Notice when we type i, it shows us at
the bottom of the screen that we are in INSERT mode. We type a few sentences, then we want to save the
file and quit vi. First we press the ESC key to exit INSERT mode. Then to save and quit type :wq for write and
quit. You can save the filename after entering vi as well. After entering vi and adding contents to our file, we
type :w file2.txt to save the file with filename file2.txt. Now that the file is saved, we can type :q to quit vi.
So far we have demonstrated how to create a file using vi, and how to save and exit vi. Next we’ll show you
how to navigate in vi. This list shows the keys to use in command mode to move the cursor around.
Let’s edit an already existing file called myfile.txt. if we press the l key several times, the cursor moves to the
right. We can also move to the right by pressing the right arrow key. Or the spacebar key will also move us to
the right. We can move to the left using the left arrow key. And we can also move to the left using the h key.
To move down in the file, use the down arrow key. Or use the j key to move down as shown here. The k key
will move you up in the file, or you can use the up arrow key. The lower case w key will move the cursor
forward one word. The lower case b will move back one word. The lower case e will move the cursor forward
to the end of the current word. Use the $ to move the cursor to the end of the current line. The 0 (zero) key
will move the cursor to the beginning of the current line.
Here we are pressing w several times to move forward a few words so we can demonstrate the next
navigation option. The ^ caret or circumflex key moves the cursor to the first non-white character at the
beginning of the current line. If you press the return or enter key, the cursor will move to the beginning of the
next line.
The capital G key will move the cursor to the last line in the file. And if you add a number in front of the capital
G, it will take you to that line. Here we’ll type 1G, and it takes us back up to the first line in the file.
You can enter a command in command mode to set numbering in your file. Type :set number then press
enter. Now we see the line numbers displayed in our file. This allows us to demonstrate typing 4G, which
takes us to line 4 in the file.
There are other commands that allow you to move around in the file. For example, here we type CTRL D to
scroll down ½ screen.
In order to demonstrate moving around in a larger file, we created a bigger version of the same file. The
cursor is at the top of the file and we want to quickly move to the end of the screen or page. We type CTRL F
to scroll forward one page. If we scroll to the end of the file by pressing CTRL F again, then we can
demonstrate how to quickly scroll up a page at a time. To scroll up one screen or page, type CTRL B. And to
scroll up one half the screen, type CTRL U.
So far we have demonstrated how to navigate in a vi file using the commands shown in these tables. Next
we’ll look at commands to change and delete text.
To replace characters on the line, press capital R. Notice at the bottom it tells you vi is in replace mode. This
will allow you to replace characters to the right of the cursor. When you are done replacing text, press the
ESC key to exit replace mode. Pressing capital C allows you to replace text from the cursor location to the end
of the current line. Notice at the bottom vi tells you it is in INSERT mode. Press ESC when you are finished
entering the text to exit INSERT mode. Now we’ll press the enter key a few times to move down in the file.
On line 8, if we type a lower case s, this allows us to substitute a string of characters for the character at the
cursor location. When we press the s key, vi enters INSERT mode. Notice the S in the word Starting is
replaced with “here we are” then a lower case s. Always press the ESC key to exit INSERT mode.
Now we’ll hop back a few words by pressing the lower case b key. Here we are pressing the x key to delete
one character at a time.
You can also delete a word at a time by pressing dw for delete word. Here we put our cursor at the beginning
of the word starting, and then we press dw to delete the word. To delete the current line, type dd. Here we’ll
delete line 8, then we’ll delete the new line 8 using dd.
Vi has the ability to undo the last change you made by pressing the u key. We will undo the last few delete
commands by pressing the u key several times.
To append text after the cursor location, press a lower case a. Here we press a and type in some text.
Pressing capital D will delete the text from the cursor to the end of the line. Notice here we forgot to exit
INSERT mode. So the D showed up as text in our file. We exit INSERT mode by pressing the ESC key, then
when we type a capital D, the rest of the line is deleted.
In this last section, we showed how to change text using the r, c, and s commands. We also looked at how to
delete text using the x, d, and u commands. In the next section we talk about appending and inserting text.
Now we’ll move down a few lines to demonstrate how to append text at the end of the line. To append text
to the end of the current line, type capital A. Y
ou can also insert text starting at the current location by typing lower case i. We move to a different location
in the file, then we type lower case i. Notice we are in INSERT mode. We’ll type some text and then press the
ESC key. We move down in the file using the enter key, and then move to the right word by word using the e
key.
To insert text at the beginning of the current line, type capital I.
To open a new line below the current line, type a lower case o. To open a new line of text above the current
cursor location, type a capital O.
You can add text from another file by typing :r filename. Here we insert file.txt by typing :r file.txt. The
contents of file.txt are inserted into the current file below the cursor location.
In this last section of the video, we reviewed how to insert text using the a, i, and o commands. We also
inserted another file into the current file using the r command. Now we’ll review how to save a file in vi, and
how to exit vi. We decide to save this file to a different filename. So we type :w anotherfile.txt.
Since we made some changes to the file, vi won’t let us quit without writing our changes, or using an ! to quit
without saving changes. When we try to quit here by typing :q, we receive a warning message saying to save
our changes or use the !. We decide to quit without saving changes because we just saved this file to another
file name. So we type :q! to exit without saving changes.
Let’s go back into the myfile.txt.
:w enter writes or saves changes to the current file. Now we’ll save and exit this file by typing :wq and the
enter key.
Now we’ll look at what happens when you edit a read only file. We’ll open the read only file by typing vi
readonly.txt. When you open the file you receive a warning that this is a read only file. Notice when we press
o to insert a new line, we receive a warning at the bottom of the screen telling us we are changing a read only
file. Let’s enter some text here. When we try to save the file by typing :w, we receive another warning saying
the read only option is set, and we should add ! to override the read only option and save the file. When we
type the :w!, it saves our changes to the read only file. We make some more changes, and again if we want to
write and quit the file, we receive the warning that the file is readonly. So to save the changes and exit vi, we
need to type :wq!.

In the last section of our video, we talked about how to save and quit vi using the w and q commands.

That concludes the Linux vi demonstration video. We did not demonstrate all the commands available in the
vi editor. There are other useful vi commands. Be sure to review the vi quick reference sheet attached to the
associated MOS knowledge article. Thanks for watching this video.
This table is here for your information. The commands shown here were not covered in the video but are very
useful.
This is a vi cheat sheet. This cheat sheet is also available here:
https://stbeehive.oracle.com/content/dav/st/Linux%20Advocate%20Accreditation/Public%20Documents/Vi
%20editor%20quick%20reference%20sheet.pdf.

You might also like