Social Icons

Wednesday, September 25, 2013


              I have already installed vim editor into my machine hence I wont be talking about any steps on how to install the vim editor. If anyone needs any assistance regarding the installation please refer to my previous posts.


The vim editor is a text editor written by Bram Moolenaar and was first released publicly in 1991. We will be using this text editor to write our Linux programs which I will be discussing with you. The reason why I am writing an article on how to use the vim is because this editor initially feels very confusing and irritating until you get used to it. 

                                                               There are many features present in the vim editor but I will only discuss with you only a few of the necessary operations you need to know in order to write a program, a text document, or almost anything that is related to text editing and how to save the file we edited etc....


To open the vim editor first you need to open the terminal and just type vim.
vim -> This command will open the vim editor.


In vim we do not have separate commands for creating and opening the file. We can create a file by using the following command vim <<filename.type>> . The ".type" is nothing but the extension i.e if you are going to write a c-program give filename.c or if you are going to edit a text file write filename.txt, or if you are going to edit an .c file you will give filename.c .

for example : you cannot compile a c program if the file has no .c extension as the compiler will not recognise the format. (in my future posts I will give you more clarity as few more concepts are need to be covered to understand it if its confusing for you.) 
                                                        If you want to open an existing file just use the same command vim <<existing filename>>. If the file exists in the present working directory it will open the file and display its content , else if the file is not present it will open a new file with the name we mentioned and display it to you as an empty file. 

Look I am opening a file with my own name akhil.
Now the vim will check if a file with name akhil is present in the current working directory or not.
If its present in my current working directory it will open the file and display it to me , else it will just open a new empty file and display it to you. If you want to open a file which is already saved in the disk first we have to move to that directory and then specify the command vim <<existing file name>>, and it will open and display the file to you.
                                                    Once the file is opened it will look something like this.

 Because this is a new file and I don't have any file with the name akhil previously stored in my disk its showing a blank window. The above screen shot shows what your screen looks like. The tilde (~) lines indicate lines not in the file. In other words, when Vim runs out of file to display i.e if there is no file with the file name I have specified it displays the tilde lines. At the bottom of a screen, a message line indicates the file is named "akhil" [new file] and shows that you are creating a file. The message information is temporary and the other message overwrites it when you type the first character.

Now look carefully I have opened a new file i.e it was not existing in my disk previously hence its displaying  "akhil" [new file].



Now I will save the file with the name akhil and will type the command vim akhil. This time it will open the existing file as I just saved. It will show something like this.



Here it shows only "akhil" 0L, 0C. It means zero lines and zero columns.
This is one of the  modes in the vim editor. We use this mode to enter the text into the file. Open a file using the editor and now press the letter " i "  in the keyboard. This will initiate the insert mode and it will be displayed at the end of the screen. 
 Now you can enter what ever text you want to enter and save it inside that file.

Once you are done entering the text into the file you should press the ESC key to exit from the insert mode. After you have exit from the insert mode you can do the following 

1:  :q-> this is called quit. If we have not done any specific changes and just wanted to view the file and close it you can use " q " to quit from the vim editor window.
This is only applicable if you have not made any changes to the file from the time you have opened it using the vim editor.

If we make any changes in the file after we have opened it we have to instruct the vim editor to either save the changes or to discard the changes.

If we made any specific changes in the file even if we add a space into the file (adding a space is also a specific change we make in the file as space will also be counted.) and try to quit by pressing " :q " it will display an error message as follows.

 Its showing no write is performed since the last change i.e the changes we made has not yet been written into the disc.
note: " : " the colon must be added before " q "
step1:   ESC
step2:   press " :q "
step3:  press enter key.

2:  :q! ->  after pressing ESC key if you want to perform a quit without saving any changes you can do it by adding an " ! " after " q " (quit).

3:  :wq -> after pressing ESC key to escape from the insert maode you can perform a save and quit operation by pressing " :wq ", which says write and quit.

NOTE: If you create a file with the name " akhil " and another file with the name " akhil.txt " and yet an other file with the name " akhil.c ", all three of them are different files though they have same name with different extension vim will treat them as three distinct files.

These are the few very basic operations you need to know. There are many more features in VIM that I am not discussing here as its not necessary for time being..
                                                         Any doubts please feel free to ask me.You can drop your query in the " ASK ME " section of my home page.

No comments:

Post a Comment