vim(1)
vim(1)
about the vi editor in Unix. So, in this lecture we will understand how the vi
editor works. So, without further ado let's get into it. So, there are many ways
for editing the file in Unix. But editing the files using the screen oriented text
editor which is a vi editor is one of the best ways and widely popular way.
So, this editor enables us to edit the lines in context with the other lines in the
file. So, there is one improved version is also released called as a VIM or a VIM
editor which stands for a VI improved editor.
But let me show you how to use the vi editor first. So, it generally has some
benefits that's why we are using this vi editor here. The first one is it's usually
available in the all flavors of the Unix system. That is very huge benefit. The
next one is its implementation is very similar across the board. So, it will
suggest it is very easy to implement.
Also, it requires few resources and it is very user friendly than the other text
editors such as AD editor or EX editor. So, these are some benefits of using the VI
editor. So, now let's get started with the VI editor and how we can create one file
and edit it.
So, next topic is starting with the vi editor. So, here we will cover the basics of
vi editor. So, following are the basic commands to use the vi editor. So, if you
want to create a new file which is not already present, we will use the vi command
and the file name that you want to create. But if the file exists then it will open
this existing file instead of creating it. Let me show you how to create a file
with the vi editor.
so this is our command prompt and let me list out the items in our home directory
so to do that just use ls command so as you can see this many files are present to
create a new file we will use the vi command after that we have to give the file
name so file name let's consider shell
So, this is our new file that we will create. If you hit enter, as you can see our
new file has been created. So, this is how you create a file using the vi editor or
you can access that file if it is already existed in your directory. So, our next
command is vi with hyphen r option.
So this commands opens the existing file in the read only mode. So you can only
read the data but not manipulate that data. So let me show you how we can use this.
So now these are the items that are present in our present directory or a home
directory.
But if you want to open any file but only in read only mode, you can do that by
using the hyphen r option with the vi command. So just use vi command, give hyphen
r and just give the file name. So in this case we will be opening this grocery
file. So just give like grocery.
After hitting enter, as you can see, this has opened this grocery file but we
cannot manipulate the data inside this file. So, this is all about the hyphen r
option in this vi editor. Instead of a vi command, we can also use the view command
to open the existing file in the read only mode. So, it is very similar. We have to
just give view and the file name.
so that you can do it on your own as it is very simple command. So as of now I have
just told you how we can create a file using the vi editor and also how to open an
existing file with only the read only view. So now let me show you how to operate
this vi editor and how we can insert the data in it.
But first, I think you have noticed these approximation symbols in the starting of
each line.
So, it nothing but it represents the unused line. So, as you can see these lines
have been used, this first 8 lines have been used by this components. But here from
this line, this all the lines are unused. So, it is represented by the
approximation symbol or a tilt symbol. So, if the line does not begin with the tilt
symbol and appears to be a blank, there is a space or a tab or some new line
present in it.
So, now our next topic is operation modes. So, while working with the vi editor, we
usually have two modes present in it. First one is command mode and second one is
insert mode.
Let me tell you about command mode. So this mode enables us to perform the
administrative task such as saving the file, executing it, moving the cursor,
cutting and pasting the lines or words as well as finding and replacing the word or
lines. So in this mode, whatever you are typing on your keyboard, it is interpreted
as a command. And the second one is insert mode.
So, this mode enables us to insert text into a file. So, you can put anything in
there which can be either executed or just a text file. So, everything that is
typed in this mode is interpreted as a input and will be placed in this file and
consume some space. So, this vi always starts in the command mode. So, whenever you
create a new file or open the existing file by using the vi command, it will start
with the command mode.
But, if you want to go into the insert mode, you have to type I key. And to come
out of the insert mode, you have to just press escape key. So, which will take you
back to the command mode.
The important point is, if you are not sure which mode you are in, just press
escape key twice. So this will take you to the command mode. If you open a file
using the vi editor, start by typing some characters and then come to the command
mode to understand the difference.
So, it will be very beneficial if you are just starting working with the vi editor.
So, let me show you how to get into the command mode and the insert mode. So, now
let me create one file using the vi command. So, just type vi and the file name
which is shell.
So, after hitting enter, we are in our newly created file which is called as shell.
So, as you can see, when you go inside the vi editor, it will start with the
command mode. So, if you type something, it will not get typed. As you can see, I
am trying to type but it is not inserting the data as we are in the command mode.
So, it always expects some command in it that we are going to discuss further.
But if you want to insert some text file or some commands in it, you can do that by
going into the insert mode. To do that just type small i key and now you are in the
insert mode now. So you can type the command like echo and we can give like hello
world.
We have inserted the text by using the insert mode. So if you get out of the insert
mode just press escape and that's it. You are in the command board right now. So
this is how you can switch between these different operation modes in the vi
editor.
So, our next topic is how to get out of the vi editor. So, the command to quit out
of the vi editor is semicolon and a small q. So, once in the command mode, just
type the colon and the q key followed by the enter. So, if your file has been
modified in any way, so the editor will warn you of this and not let you to quit.
So, to ignore this, the command to quit out of vi editor without saving your work
is just give colon, q and exclamation symbol. So, this let you to exit from the vi
editor without saving your work. And the command to save the contents is small w.
So, for that you have to just give colon and small w. And we can combine these two
commands to quit and save simultaneously.
Just you want to give colon, w and q and then just hit enter. So if you want to
specify or state any particular name for the file, you can do so by specifying it
after the colon and w. But the most easiest way to save your changes and exit from
the vi editor is just type capital Z twice. So it will save your changes and you
will get out of the vi editor.
Let me show you how.
So as you can see we are in our newly created file called shell which we have
created using the vi editor and now we have inserted some text in it. So if you
want to save our changes you have to just give colon and just give like wq. After
hitting enter as you can see we are exited from the vi editor but our changes will
be saved. Let me show you.
If you type again vi shell, as you can see, our modified file has been saved. But
if you don't want to save your data, then just give colon and q after hitting
enter, as you can see, by using this command, you will not save your work. But the
most simplest way to exit and save your changes is
to use the capital Z twice so to do that just hit shift and zz as you can see this
command will save your data as well as you will exit from the vi editor.
So our next topic is moving within the file. So to move around within the file
without affecting your text, you must use the command mode. To do that, I have
already told you, if you are in the insert mode, you have to press the escape key
to exit from the insert mode and come into the command mode.
So, these following commands are used to move around one character at a time. So,
this J, K, H and L. These four letters, if you hit these keys, it will be easier
for navigating around one character within the file. So, the K will move the cursor
up one line, the J will move the cursor down one line, H will move the cursor to
the left and L will move the cursor to the right.
Let me show you how So to use this command just make sure you are in the command
mode. So to do that just hit escape key twice and That's it. You are in the command
mode now. So to navigate through this file you have to use Small J to move the
cursor down one at a time L will move the cursor to the right H will move the
cursor to the left
and K will move the cursor in the upward direction. So this is how you can move
within the file.
So, our next commands are for editing the file. So, there are many ways that you
can enter the insert mode from command mode. So, these are all the commands that we
have used to edit the file. So, the I will insert the text before current cursor
location. The L will insert the text at the beginning of the current line. Small a
will insert the text after the current cursor location. Capital A insert the text
at end of the current line.
The small o will create a new line for text entry below the cursor location and
capital O creates a new line for text entry above the cursor location. So, let me
execute these commands to get you the clear understanding.
So, as you can see we are in the vi editor now and we are in the command mode. So,
to use this editing file commands, you need to be in the command mode. So, if you
use the I key, it will insert the text before the current cursor location.
So, here I have pressed the I key here and it is inserting the text before the
cursor location as you can see here. Now, we will get out of the vi editor by using
the escape key. The A command inserts the text after the current cursor location.
Hit the A as you can see. It is inserting the text after the current cursor
location. The next one is
The capital A. So, the capital A I have hit. It is inserting the text at the end of
the current line. Our next topic is how to delete the characters. So, for deleting
the characters there are so many commands that I have given here in this table. So,
the small x will delete the character under the cursor location. The capital X will
delete the characters before the cursor location. The dw deletes the current cursor
location.
to the next word. This fourth one deletes the current cursor position to the
beginning of the line. Fifth one deletes the current cursor position to the end of
the line. The capital D will delete from the cursor position to the end of current
line. And small dd deletes the line which the cursor is on. Let me show you how we
can execute these commands.
So, when we hit dw, as you can see, it has cleared all the characters which are
before the cursor location. The next command is capital D which deletes from the
cursor position to the end of the current line. So, if we hit capital D.
So, it has cleared all the text from the cursor position. So, as you can see we
were on the first position, it has cleared all the text from that line. And the
last one is DD, which deletes the whole line on which the cursor is present. So, if
you hit DD, as you can see.