13 March 2013

Using vi Editor


Vi is the one of the mostly used editor in Linux via terminal. In most cases where Linux is used as a server operating system, admins use the terminal, where GUI uses more system resources - network bandwidth. Since Vi is used from the terminal, the lack of GUI force the user to learn function keys of the editor. Vi is a powerful and functional application if it is used with the ideas that is built on.


To run vi, type vi from the terminal, vi is installed on most of the Linux distros as default.
You may also you the improved version called "Vim" if you are developing codes or scripts.

If you don't have vim installed just run the command to install "yum install vim-enhanced -y".

To create a new file just write the name of the file after the command as follows:
vi /root/newfile.txt


For more functionality Vi is designed to work in three modes described below:

Command Mode
Edit (editing + adding) Mode
Command Line Mode

Vi starts in Command mode, thus you cannot directly start typing or editing the lines inside the editor. You can always return to command mode by pressing ESC button anytime. 

To switch to Editing mode while you are in command mode press "ESC" and type ":" or press "Insert" button on your keyboard.

Below are the mostly used functions in Command Mode:
Command  - Action
i Switches to Insert (edit) Mode
a Switches edit mode at the end of cursor
A Switches edit mode at the end of current line
ESC Closes edit mode and switches to Command Mode
u Undo
U Undo all
o Insert new line
dd Delete whole line
/string makes search for the "string"
n Search next item

To switch to Command Line mode press ESC and type ":" without quotes. 

Below are the commands that you can use in command line mode:
Command  - Action
ZZ Saves the current file and exits or
wq w for saving, q to exit
w! saves read-only files (force)
q! Quit without saving
q Quit



No comments: