[Intro music]
Hello everyone. In this lesson we will cover the
Linux file editor. An editor is a program which
enables you to create and manipulate data or text
in a Linux file. There are several standard text
editors available on most Linux systems. You could
use vi, ed, ex, emacs, pico, vim, which is,
vim is the advanced version of vi. In our
lesson, we will focus on vi. Why? Because vi is
available in almost every Linux distribution,
regardless of which Linux system you log in.
Whether it's Ubuntu, SUSE, Red Hat, CentOS, you're
gonna find vi everywhere. Plus, it's very easy to
learn. The vi editor actually supplies commands
for inserting or deleting text, replacing text,
moving around the file, finding substitute
strings, and cutting and pasting text inside. So
that's what vi does when you have to write to a
file. The most common keys are i which should
insert, escape, replace for r, d for delete,
and quit without saving, and quit write with save.
So let's look at our Linux machine. And at this
time, we will log into our Linux machine, the
Linux virtual machine through our PuTTY session.
[No audio]
Okay, let's cd, whoami, and which directory we are in.
[No audio]
In this directory, I want you to create
your first file, so call it vi, which is a
command, space myfirsteditor,
or just say myfirstfile, how's that? Much easier, hit enter.
Now, when you hit enter, you are still in command
mode, you are not in insert mode. So there are two
different modes once you are in the vi. This first
mode is to when you run the command, second mode is
when you're inside of the vi and you start typing it.
If you want to get out of that typing mode, you
have to hit escape. So now let's start, let's say
if I wanted to type into this file, you hit i for
insert, lowercase i. As soon as you hit it, you will
see at the bottom, it says INSERT. It means we are
in insert mode. Now our regular Linux commands
won't work because we are in insert mode.
So let me type something like Hello World.
This is my first lesson to learn vi. And you could
hit enter again, I am so excited about it. So it's
just simple as if you're typing a word document.
Now if you wanted to exit out of it, if you want
to save this file, all you have to do is hit
escape right here. And once you hit escape, you'll
see the insert mode is gone. Now how do you save
this file? You could do through two different
ways. One, you hit shift zz,
the zz twice, and it
will save the file. Let's see, if you do cat again,
and do myfirstfile, you'll see it's there.
Another way to save a file, let's go back in the
file, myfirstfile, and let's say you want to type
something, hit insert again, and then hi space.
And now you want to exit out and save, hit escape,
and this time instead of shift zz, you type shift,
colon, write, quit, and exclamation point, which is
also referred to as bang, you hit enter. How do you
verify? You read the file myfirstfile, and there
you go, the file is there. So these are the
two modes, is once you get out and once you get in.
Now I want you to go back into your file and hit
vi and myfirstfile,
and I want you to replace
[No audio]
hi, no I want you to delete hi and leave that as is.
Now how do you go move around inside of a file
when you are already in your editor. You could use
up, bottom, left, right keys on your keyboard to move around.
[No audio]
But remember, you cannot use these
keys if you are in insert mode. So let's say if
you hit i, and you now in insert mode, and now
you do a left, right key, it can work, but most
likely it will break your commands. So it's better
you hit escape, and go back, and then you do d on your keyboard, d.
[No audio]
This will delete the entire line.
I wanted to delete only the word hi. So how do I
get back my last line that I deleted. So you hit
u, just simple u, and it brings back your last
line. Now, if you wanted to remove the hi, just
type x, once, which will remove one character, type
x again, which will remove another character, now
you have a space, hit x again. Now you don't have a
space. Now what if you want to save now, again do
shift zz, twice, and it saves it. How do you verify
it? You do cat myfirstfile, and it is there.
Now let's go back in again, and let's practice the replace command.
vi myfirstfile. I want it to
replace, I am so excited about it.,I want it to
replace the word excited with the word happy. So
how do we do it? We cannot hit i right now
because we don't want it to get into the insert
mode. So we will use our arrow keys to go down to
the word excited, so I could do r.
Once I hit r, I
could do h, it only replaced one letter. If I
wanted to do again the second letter, you move
your cursor to the right, hit r, and then do a, and so
forth. But I would prefer that you do escape out
of it. And then do x x x x x, and then you hit i,
and then ppy. You hit escape again. so you can
move around. You could go up, you could go down if
you wanted to move around. Now what if you
want to go to the end of this file, and now you
want to create a new line right below it, before I
am so happy about it, you hit o. This will create
a new line for you, and also it will automatically
put you into the insert mode. So you could type
anything like blah blah blah. But you just
noticed that you just type j by mistake. Now how
do you remove j, you type x. Now remember if you're
going to type x, it will write x instead of deleting
it. Look, see it is typing the x. So why to delete that
character? How do I delete that character, it is
because you cannot delete. Why? Because you are in
insert mode. How do I get out of insert mode? You
hit your life saving key which is escape.
As soon as you hit escape, you see the insert mode
is gone. Now when you hit x x, it deletes those
two characters that you added by mistake, it was a
typo that you added. Same way if you want to go up,
you could, if you want to write something right
here, and if you type i, you notice it start
writing right in the middle. Hellohi, see without
giving the space. So let's do escape again. Hit x,
move the cursor to the left, and x, okay. And now
you have that space in the middle. Now how do I
write by putting a space in the middle? I do a, So
a will automatically advance to the next space. So
Hello hi bye space world. Okay, now you're
still in insert more, what if you want to save it?
Before you save it again, get out of insert mode
by hitting escape, and do shift zz. This saved
your file, cat, myfirstfile, and it is saving
your first file. Now what if I wanted to delete
the line, blah blah blah, the third line. So
I'll go back into my file vi myfirstfile, move
your cursor down to blah blah blah, and hit d d
twice, one, two. There you go, it removes the line
that you added wherever you want to go. If
you want to, oh, you removed it by mistake, no
problem, u will bring it back. Again, you
could save it through shift zz, or you could save
it through colon write quite,
and bang, you could hit enter. And that's how you
save the file. Now let's go back into the file one
more time. vi myfirstfile, and I want to let's
say, I have a file, which is huge, and I wanted to
search for a specific word in a file. Let's say I
wanted to search for lesson, I wanted to search
for l. So you could do search for it two ways. I
hope you remember the command grep. And then you
search for lesson, from where, myfirstfile. And it
will only bring that one line that has the word
lesson. But if you are in vi mode, you could also
do that same search. And now you could just simply
make sure you are not in insert mode, simply hit
slash and type lesson, lesson, hit enter. And
there, it will bring you to exactly the word that
you want to change perhaps, or you are looking for it. So
these are the few commands that we have about
creating a file using vi. We do have many different
commands or keys that we could use in vi. I have
included in this lecture, I have included all those
keys as a handout. So please
look into those and try every single key, I really
want you to be very familiar with this and become
eventually become an expert. So when you're typing
in something in your text file, you know exactly
which key you're going to work into, or which
exactly key you're going to use. One common
mistake that everybody is going to make is, when
you are in insert mode, you're probably going to
have to get out of it, you can, your typing x, this
and that, but you cannot. So your lifesaver is you
do colon, no first, sorry, first is escape colon
Q. Now this time don't write W. The W will write
your changes the file, you don't want to make
changes. So just to quit, colon quit, bang, hit
enter, verify it. There you go. See now you don't
have that garbage that you were going to write
your file into. So this is another way you get out
and change your file. So please practice, man also
give you different options to use it. There's
again, there's another editor if you like to use
which is vim, which is an advanced version of vi or
you could use that too, which is also available
most of the Linux distribution, whichever you
like, but please do practice. Practice as much as
you can. And I will recommend that you create
files many files in your home directories or
within your subdirectories create your files and
write wherever you want to. Write ABC, or write a
story or whatever and change your text. This is
this is going to allow you to practice vi editor.
[No audio]