In last tutorial we have discussed how to view Linux file content.

In this tutorial we will learn how to edit, delete, copy, cut, and paste text in Linux files and navigating in file. There so many file editors in Linux e.g gedit, pico, emac, nano, vi etc.VI is most popular editors on Linux/Unix operating system. We will discuss basics of VI editor in this tutorial

There are two modes of operation in VI Editor

  1. Command Mode
  2. Insert Mode

 

In Command mode you can navigate in file and manipulate text like searching/replacing texts.

In insert mode you can edit and write text in normal way

In part 1 we will only discuss about opening file, writing file , closing file. Later in second part we will learn about searching/replacing text, copying/deleting lines etc

If there is already a file you can open file in VI editor by vi filename if file does not exist it will create file with filename

VI_Editor_1

In above screenshot we can see there is file file1 in list. If we want to view/edit content of file we can type

vi file1. VI editor will open file in vi and will display name of file at bottom see screenshot below

VI_Editor_2

 

By default VI mode is command mode. to switch to insert mode we need to type ‘i’. You will then see INSERT at bottom of screen. This means now you can type text in normal way

VI_Editor_3

Now lets add some text

VI_Editor_4

Now we will save contents in file. As now we are in insert mode we need to change mode to command mode

So we will press ESC key

VI_Editor_5

To save content in file press :w and hit enter

VI_Editor_6

You will see a message that contents are written at bottom of screen. See screenshot below

VI_Editor_7

To quit file press :q and hit enter

 

VI_Editor_8

 

To write and quit type :wq and hit Enter

If file has been modified and you do not want to save changes type :q! and hit Enter. We have added text This is seventh Line but we do not want to save this line so we will press ESC and then :q! and hit Enter

 

VI_Editor_10

 

we will open file again by typing vi file1

VI_Editor_11

 

This is end of part 1 tutorial. We have learnt basics creating/opening file writing , saving contents and quitting file without saving modification. I hope you have enjoyed tutorial . In next part(s) we will learn more about VI editor

SUBSCRIBE OUR NEWSLETTER
I agree to have my personal information transfered to MailChimp ( more information )
Join us by subscribing to our newsletter and learn IT subjects for free
We hate spam. Your email address will not be sold or shared with anyone else.