How to Use the Vi Text Editor in Linux



Linux


In this article, we show how to use the vi (vee-eye) text editor in Linux.

Many ask why the vi text editor should be used in linux when there are easier text editors such as nano available.

The answer is, there are a few good reasons to use the vi text editor in linux.

For one, vi is always available. This can be important if you have a system with no graphical interface. While nano is being increasingly used and is popular, it is still not universally available. Another reason is vi is lightweight and fast. For many tasks, it's easier to bring up vi than it is to use other methods.

So with these reasons at times, it may be useful and beneficial to use vi as the text editor to either create or an edit a document.

To start vi, we simply specify the following line below in the shell.



So now the vi editor is started up.

This is shown below.

vi text editor in linux

To close out (quit) of this vi editor, we specify the following line below.



We press :q and then Enter.

If we want to quit out the editor after we have created a document or made any updates, then we specify the following line below.



We type in :q! and then enter.

This will save changes and then close the vi text editor.


How to Create a New File with the Vi Text Editor in Linux

So we can create a new file with the vi text editor in linux.

We can do this using the vi keyword followed by the file name.

In the following code below, we create a new file with the vi text editor named note1.txt



We can now write in this file.

To write to this file, go down the vi editor and make sure the editor is in INSERT mode. If it is not, press Esc and i. This gets it into the INSERT mode.

and then we need to save it.

Once you want to save the file, press ESC once and type in the following code below.



Once this is done, then the file is saved.

It will tell you how many lines and characters were written.

In the linux shell, I created a file named "note1.txt" and wrote in it, "This is good.", a 1-line program, with 14 characters.

This is shown below.


Saving a file in the vi text editor in linux


So now we know that the file is officially saved onto the hard drive. Without this, the file does not get saved.


How to Edit a File with the Vi Text Editor in Linux

Now if we want to edit a file, then we can do so in the same way we create a file.

If we want to edit the file, note1.txt, then we can do so with the following code below.



This opens up the file in the vi text editor.

You can now edit this file.

In order to do this, if you go down the vi text editor, the option should be, INSERT. Again this is by pressing Esc and i. This places the option in the INSERT mode.

And this is how to use the vi text editor in Linux.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...