Introduction to VIM text editor

Unix

Share

vi is an editor which was first written by Bill Joy in the year 1976 who later co-founded the Sun Microsystems.

Read more about Bill Joy

It derives its name from the word visual as it was made to allow editing of files on a video terminal with a moving cursor. Latest versions of Linux distribution comes with an enhanced version of vi called vim or vi improved and was written by Bram Moolenaar.


Read more about Bram Moolenaar

If you are using Unix, any linux distribution or Mac then chances are you already have vim installed in your system. [Version of vim may vary]

How to open vim?

To open vim you first have to open the terminal.

Once you have your terminal window on you screen all you have to do next is write the command vim and press enter and you will see the following.

 

 

 

How to write the famous “Hello World” in vim?

First we need to enter into the INSERT mode by pressing

i

The insert mode is highlighted at the bottom of the window as -- INSERT --


 

Now we can type Hello World.

 


What tilde ~ sign in each line signifies?

The tilde sign ~ in every line indicates that the line is empty.

 

How to save “Hello World” text in a file named hello.txt?

To save our work in a file we first have to come out of the INSERT mode. For this we have to press the Escape [Esc] key.



Note the -- INSERT — from the bottom of the window is gone.

Now to save it as a file named hello.txt we have to type

:wq

followed by the file name and press enter and hello.txt file will be created in the current directory where we have opened the terminal. We can also type the full path and the filename with extension (if any) to tell vim where exactly we want our file to be saved.


Note!
:w saves the file while :q helps in quitting vim. So, we can combine the two commands into one by tying :wq which tells vim to save the file and quit.

For example

:wq hello.txt

Will save the text we wrote in a file hello.txt inside the directory /Users/yusufshakeel/ because that was the current directory when terminal was opened.

To save the file in a different location we have to specify the complete path.

:wq /Users/yusufshakeel/Documents/hello.txt

This will save our Hello World text in a file named hello.txt inside the directory /Users/yusufshakeel/Documents/

 

Now to view the file in the current directory type the ls command and you will see the hello.txt file.

 

How to open the hello.txt file back in vim?

To open any file in vim all you have to do is type vim followed by the filename and extension (if any). Or we can provide the full path of the file like

$ vim /yusufshakeel/hello.txt

 

 

 

The bottom line "hello.txt" 1L, 12C tells us the file name and number of line and characters in the file.

 

How to quit vim?

To quit from vim simply press the Escape [Esc] key to enter into the command mode and then type :q and hit enter.

 

 

How to quit from a file in vim without saving the changes?

Say for instance we have written another line in the hello.txt file but we don’t want to save the change.

 

Using the :q will not work in this scenario as we have made some changes in the file and vim expects us to save it.

 

 

 

So, to quit from the file without saving we have to type

:q!

The exclamation mark will tell vim to quit without saving any changes made in the file.

 

 

How to create a new file and open it using vim command?

It is very simple to create a new file and open it using the vim command. Just type vim filename and hit enter.

For instance, lets create a new file by the name helloworld.txt and open it using vim.

$ vim helloworld.txt

 

A new file is created and opened in the vim text editor.

 

 

We can then write some lines and save the file using the :w command.

 

 

The last line tells us that the new file helloworld.txt has been saved and it has 2 lines and 98 characters.

To quit we will type :q

 

 

Using the ls command we can check the new file in the current directory.