vi Editor commands need to know
1.1 Introduction to vi Editor
vi editor become the most powerful editor in the UNIX environment due to the many of its features.
Earlier programmers in UNIX used had to use editor which were line editors. They could see only the line they were editing, which was causing problems for them. When vi was introduced, it could give the programmer full screen view. vi was created at the University of California at Berkeley by Bill Joy. vi stands for “visual”.
1.2 Basics of vi Editor
vi functions in three different modes – command mode, insert mode and ex mode.
Command Mode: In command mode, any key pressed by the user is assumed as commands by the editor. No text is displayed on the screen when any key is pressed. When you open a file in vi by default it will open in command mode.
Insert Mode: The insert mode helps you in inserting/editing the text. By pressing i or a vi comes in to insert mode from command mode. Pressing [Esc] in this mode take vi back to Command Mode.
Ex Mode: This mode is used for saving file, substituting a string.
vi emits a beep sound when if there is any error.
1.3 TERM settings
In order to work correctly the vi need correct terminal type (TERM) settings. Commonly used TERM types are vt100, vt220 and ansi. In most cases vt100 will work fine. In case vi is not able to understand the TERM you have given, it starts in open mode giving you a line by line display.
Generally TERM is taken from .profile or /etc/profile but can be set at the command line as follows
# TERM=vt100
# export TERM
# echo $TERM (will display the current TERM settings as vt100)
1.4 Getting started with vi
By using you can create a new file or you can edit an existing file.
vi without any file name will open a new file where you can enter the text and edit but while exit out you will be asked to enter a valid file name to save the text.
vi with a file name as argument will open that file for editing, if the file already exists it opens it otherwise it creates a new file by the argument.
# vi file1
Creates or opens the existing file called file1. Now this file opens in command mode where you can move around the file. Once you press i or a will change vi in to insert mode and will let you edit the file. Please go though following options for more details.
1.5 Cursor Movement
| k | moves cursor up | |
| j | moves cursor down | |
| h | moves cursor left | |
| l | moves cursor right | |
| 4k | moves cursor 4 lines up | |
| 4j | moves cursor 4 lines down | |
| 2h | moves cursor 2 characters left | |
| 2l | moves cursor 2 characters right | |
| w | moves cursor a word forward | |
| b | moves cursor a word backward | |
| ^ | brings the cursor to binging of line | |
| $ | brings the cursor to end of line | |
| 4w | moves cursor 4 words forward | |
| 4b | moves cursor 4 words backward | |
| G | moves cursor to end of the last line | |
| 1G | moves cursor to first line | |
| 5G | moves cursor to 5th line |
1.6 Scrolling the Screen
| Ctrl-f | scrolls 1 screen forward | |
| Ctrl-b | scrolls 1 screen backward | |
| Ctrl-d | scrolls half page forward | |
| Ctrl-u | scrolls half page backward |
1.7 Inserting Text
| i | inserts text left of cursor (existing text will move to right) | |
| a | inserts text right of cursor (existing text will move to right) | |
| I | inserts text right of cursor (existing text will move to right) | |
| A | insert text at the end of the current line | |
| o | opens new line below the current line | |
| O | opens new line above the current line | |
| r | replaces one character | |
| R | replaces entire string from cursor until you press ESC |
1.8 Deleting Text
| x | deletes a single character | |
| X | deletes a single character before the cursor | |
| dd | deletes entire line | |
| D | deletes the entire line starting from the cursor | |
| 4x | deletes 4 character from the cursor | |
| 4dd | deletes 4 lines from the current line |
1.9 Copy and Past
| yy | yanks current line | |
| 4yy | yanks 4 lines from current line | |
| p | pasts the copied lines below the current line | |
| J | joins the next line to current line | |
| 3J | joins the 3 lines to current line |
1.10 Undo and Redo
| u | reverse the last change made | |
| U | reverses the last change made to current line | |
| . | repeating last command |
1.11 Search and Replacement
:/pattern searches for the pattern
n search forward
N search backward
:s/search-string/replace-string
:s/search-string/replace-string/g
1.12 Saving Your Work
| :w | saving the text | |
| :q | quit without saving | |
| :wq | save and quit | |
| :wq! | Save and force quit | |
| :w myfile | saves current file in new file called myfile | |
| :.w myfile | saves current line in a new file called myfile | |
| :1,4w myfile | saves first 4 lines to a new file called myfile | |
| :$w myfile | saves last line in a new file called myfile | |
| :.,$ myfile | saves from the current line to last line in a new file called myfile |
1.13 Other Options
| :sh | escapes to UNIX shell, can execute shall/system commands | |
| :set numbers | assigns the numbers to the lines | |
| :set nonumbers | removes the numbers to the lines | |
| :set showmode | enables the display of the vi mode you are currently working in |
Comments