Sunday, March 17, 2013

Vi Cheat Sheet

Today we will talk about Vi editor one of the essential text editor for oracle dba. There are many text editor which are available however most of us use Vi predominately. Some of the other well know alternative editors are Vim and emacs. Vi was originally written by Bill Joy along with Chuck Haley original called as ex after undergoing different stage it transformed to know as Vi (Visual ex)
Create Vi text file
All we have to do is run command vi

$ vi file.txt
Editor open with ~ tilde character indicating there is no text. to insert the text and save. To save you have . File is created, verify by listing the file ls cat or more command.
Okey now we will list out the command on how to edit the vi file
OPEN file
1. View file.txt open the file in read only mode
2. vi -R                                   again to open file in read only mode
3. vi +n file.txt                        n is number, open the file at nth line
4. vi +                                     open file at last line
5. vi /oracle file.txt                 open file at first oracle occurance
1. ZZ Save and quit
2. :wq save and quit
3. :x save and quit
4. :w save without quiting
5. :q quit without saving
6. :q! quite, overriding file protection
7. :wq! save and quitem, overriding the file protection
1. i insert text in front of cursor
2. I insert text at the beggining of line
3. a insert text after the cursor
4. A insert text at the end of the line
5. o insert text below current line
6. O insert text above current line
1. j (down arrow) move down line
2. k (up arrow) move up
3. h (left arrow) move left
4. l (right arrow) move right
5. 1G go to first line
6. 10G go to 10th line
7. $G go to last line
8. 0 go to start of line
9. $ go to end of line
copy and paste
1. yy copy current line (yy - copy or yank)
2. 3yy 3 lines copied
3. p paste the copied lines below current line
4. P paste the copied lines above current line

1. dd delete current line
2. x delete character that cursor is on
3. X deleter character to the left of cursor
4. 5dd delete 5 line from the current line
5. D delete to the end of line from the cursor point
1. r Replace the character that the curser is on with the next character you type.
2. ~ Change the case of a character.
3. cc Delete the current line and insert text.
4. C Delete to the end of the line and insert text.
5. c$ Delete to the end of the line and insert text. (same as D with insert option)
6. cw Delete to the end of the word and insert text.
7. R Type over the characters in the current line.
8. s Delete the current character and insert text.
9. S Delete the current line and insert text.
1. ESC KEY + /oracle serach oracle, n to serach next occurance of sting, N to search backward for previous occurance
2. :%s/oracle/mysql/g all oracle string are replaced by mysql in the file
1. u undo the previous action
2. :e! undo all the edis to the file (instead of quiting and opening)

Display Line number
1. set number display line number
2. set nu same as above - display line number
3. set nonumber do not display number
4. set nonu same as above
1. . period will repeat the previous action, like you have deleted 10 line previously it will do same action on typing period
1. J Join the line
2. 4J Join 4 lines
Default Editor
Suppose you want to edit wrong typed query under SQL prompt
1. sql> set default_EDITOR=vi
2. For permanent change define variable _EDITOR=Vi in glogin.sql ($ORACLE_HOME/sqlplus/admin/glogin or login)
3 then type ed to open vi editor

Vi Setting
Configure Vi setting. Create file .exrc under home directory this will ensure vi is opened with setting incorporated in exrc file

create file .exrc under home directory
set number
save and exit
now every time you open file using vi, file will be displayed with line number

No comments:

Post a Comment