Saturday, April 10, 2010
8:30 PM

Learn Linux -6 VI editor. Basics.

Aim: Introduce user to usage of the "vi" editor.

Now that we have learnt how to create directories, and use them, lets look at how do we create simple text files.
Linux by default comes with quite a few editors, the most famous ones being the VI and emacs.
If you happen to be using a new distro of Linux like Ubuntu, you sure will have a text editor just like the famous notepad of Windows.
The editors, vi and emacs are famous mainly because the support  a number of programming and scripting languages and they are easy to use from the terminal while writing programs or scripts.

We will look into the VI editor, its usage and various options available with it.

Note: In the new versions of ubuntu, the vi editor installed in not a fully equipped one, please use the synaptic package manager to update it by installing the package "vim". To learn more about how to install packages using synaptic package manager, have a look at
 " http://tuxthink.blogspot.com/2010/03/using-synaptinc-package-manager.html"


Creating Files using the "vi" Editor:


Syntax : vi  "filename"


For Eg:

user@desktop:~$ vi file1


A file will open up, with a blinking cursor, and the name of the file on the left corner below.  As shown below


In "vi" editor you can not just start typing right away like the usual editors.
The usage of mouse is very restricted in "vi" , more often only for copying and pasting only, which also can be done by commands.

"vi" editor has three modes of working
          Text mode :  In text mode, you can enter the text in to the file, delete it edit it etc,just like you can do in any other editor.
           Command Mode: This is some thing special you might not have come across. The command mode is basically to execute commands, when you are in command mode you can not enter any thing in the file but you can run commands. For Eg: You can run command to copy a few lines or paste a few lines . Etc.
           Visual mode:  This is used when you want to use certain commands on specific set of text, we will not be looking into this now.
  


Text Mode: 


To enter into text mode press "i" after you open the file using the above mentioned command, you should see a tag "insert" appear at the screen below, where there was file name before. As shown below. 










Now you can start typing into the file. You an use the arrow keys to move about the lines. The delete and backspace work as usual.

Command Mode: 


Once you are in the text mode, you can change to command mode by pressing the "esc" key. This will remove the "insert " tag that had appeared while you were in the text mode. As shown below.


To run any command in the command mode first you have to type ":", i.e. a colon, and then type the command.
Some simple commands  :
To save the file that we just created type ":w" as shown below


and press "enter", if the file was saved successfully you will see the tag below change as shown below.




To quit the file, with out saving  the command is "q".

To save and quit, combine both the commands that we used , i.e. "wq".

Deleting a line:  "dd"  To delete a line in the vi editor, move the cursor to the line and just press "dd" , i.e. d twice. Remember you need to be in command mode,and  this need not be entered with ":". Just hit "dd" and the line will get deleted.

Deleting multiple lines: "numberdd": To delete multiple lines just press the number of lines you want to delete followed by the "dd". For Eg if we want to delete 3 lines
press "3dd" on the keyboard and 3 lines should get deleted.

Note: "dd" actually cuts the lines, you can use "dd " when you want to cut-paste also. 

Copying a line: To copy a line, move the cursor to the line you want to copy, enter the command mode, and hit "yy" (two times "y"). The line will get copied

Copying Multiple lines: "numberyy"To copy multiple lines the logic is similar to the delete, i.e type the number of lines to copy followed by "yy". For eg To copy 3 lines type "3yy" .

Pasting lines: To paste the lines that are copied using the "yy" or cut using "dd", move the cursor to the line below which you want to paste this new line and hit "p". The new line will get pasted.

These are the very basic commands of the "vi" editor. "vi" is very powerful editor with a number of options and we surely will look into it more detail once you get comfortable using it.

Note:Some times the editor might not quit when you run "q", then you can force it to quit by using the command "q!". 


If you want to learn more about vi you can refer http://www.vim.org/index.php

Next: More commands of Linux.

0 comments:

Post a Comment