Hello, everyone.
In this lesson, we will cover the Linux file.
Editor and 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 can use VI, EDI, X, Emacs, Pico and VIM, which is VIM is the advanced version of VI.
In our lesson we will focus on VI y because VI is available in almost every Linux distribution, regardless
of which Linux system you log in and whether it's a bun to su c red hat Santos, you're going to 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.
And you have to write to a file.
The most common keys are AI, which is to insert escape, replace for r d for delete and quit without
saving and quit right with save.
So let's look at our Linux Machine OC hit CD who am I and which directory we are in?
In this directory, I want you to create your first file.
So call it the AI, which is the command space of my first editor.
Or just say my first file.
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 is first mode is to when you run the command,
second mode is when you 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 see if I want to type into this file you hit I or insert lowercase I as soon as you hit I.
You 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 an insert mode so that we could 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 were typing a word document.
Now if you want it to exit out of it, if you want to save this file, all you have to do is hit escape.
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 z.
Z.
The twice and it will save the file.
Let's see if you do Cat again and do my first file.
You'll see it's there.
Another way to save a file.
Let's go back in the file.
My first file and let's say you want to type something, hit insert again and then high space and now
you want to exit out and save hit escape.
And this time instead of shift Z, you type shift colon right quit and exclamation point, which is
also referred to as bang, you hit enter.
How do you verify you read the file?
My first file and there you go.
The file is there.
So this is 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 my first file and I want you to replace.
Hi.
No, I want you to delete high and leave that as is.
Now, how do you go move around inside of a file than you are already in the editor?
You could use up bottom left right keys on your keyboard to move around.
But remember, you cannot use these keys if you are an insert mode.
So let's say if you are hit I and you now insert more and now you do a left right key.
It can work, but it most likely it will break your command.
So it's better you hit escape and go back.
And then you do dx on your keyboard.
DX.
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 you?
Just simple you.
And it brings back your last line.
Now, if you wanted to remove the high, just type x once, which will remove one character type x again,
which would 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 Z twice and it saves it.
How do you verify it?
You do that.
My first file and it is there.
Now let's go back in again and let's practice the replace command, the I my first file.
Now I want it to replace.
I am so excited about it.
I want wanted to replace the word excited with the word happy.
So how do we do it?
We cannot hit II right now because we don't want 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
p y, you hit escape again.
So you can move around.
You could go up.
You could go down if you wanted to move around.
Now, if what if you wanted 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.
Oh, 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.
You just notice that you just typed 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 C it typing the x.
So I wanted to delete that character.
How do I delete that character is because you cannot delete y because you are in insert mode.
How do I get out of insert mode?
You hit your saving 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 wanted 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.
Hello.
Hi.
C without giving a space.
So let's do escape again.
Hit X, move the cursor to the left and x, 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.
By Space World.
Now you still insert more.
What if you wanted to save it?
Before you save it again, get out of insert mode by hitting escape and do shift z z.
This saved your file cat my first file 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 by my first file.
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 that wherever you want it to go, if you want to.
Oh, you removed it by mistake.
No problem.
It you.
It will bring it back again.
You could save it through shift z.
Or you could save it through column.
Right.
Quit 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, my first file.
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 two ways.
I hope you remember the command grep and then you search for lesson from where?
My first file.
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 less so and lessen it enter.
And there it will bring you to exactly the word that you want to change perhaps, or you're looking
for.
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 in an as a 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't you typing x this and that, but you cannot.
So your lifesaver is you do.
Colon No.
First sorry.
First escape.
Colon.
Q Now this time don't write w the W will write your changes to 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 an advanced version
of VI, 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 directory or within your subdirectories.
Create your files and write whatever 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.