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
- Command Mode
- 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
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
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
Now lets add some text
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
To save content in file press :w and hit enter
You will see a message that contents are written at bottom of screen. See screenshot below
To quit file press :q and hit enter
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
we will open file again by typing vi file1
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