BASISWORLD

Start, Learn and work on SAP Basis.

Tuesday, November 8, 2016

vi editor basics for SAP Basis Consultant

What is required for the correct functioning of vi?
Correct terminal type (TERM) setting  .The TERM setting depends on the type of terminal you have . 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 .   
 
How to create a new file or open an existing file ?
vi without any file name will open a  new file where you can enter the text and edit but while coming out you will be asked to enter a valid file name to save the text.
vi with a existing file name will open that file for editing.
 
What are two modes in vi and how to switch between them ?
After a file is opened it is in command mode ,that is , input from the keyboard will be treated as vi commands  and you will not see the words you are typing on the screen . To enter the text you have to put vi in insert  by pressing 'i' or 'a'  after which you can add the text and whatever is being type will be seen on the screen. . To switch between these mode Esc key is used .   Esc i  (text mode)  Esc (command mode) 
 
How to come out of vi ?
If you don't want to save the work :q  will take you out  for save and quit  :wq is used  simple :w saves the current file and don't exit and :q! , :wq! causes a forced quit from vi .
Editing commands
Moving around the file 
h  cursor left     cursor right
k  cursor up       cursor down
^  & B    Beginning of line  
$         end of line
    Next sentance 
    Previous sentance
    Next Paragraph
    Previous  Paragraph
 
:$   end of file
w    one character forward
W    one word forward
:20  go to Line no 20 or whatever number you give
displaying file info .
^g  give name of the file  current line and total lines of a file at the bottom .
Inserting and appending text :
i inserts text to the left of  cursor
I inserts in the beginning of line 
a appends text to right of cursor 
A appends to the end of line
Adding new line
o add a new line below the current line
O adds a new line above the current line.
deleting the text :
x   deletes text above the text
  deletes text character on  right of cursor
d20 deletes line 20
dd  deletes current line
D   delete till end of current line.
 
Replacing a character & word
r   replace the character above the cursor.
R   replces characters until Esc is pressed.
cw  replaces the word from cursor to the end indicated by $ sign .
C   replaces till end of line.
 
Substitute
s   subistutes current charcater.
S   substitutes entire line.
 
Repeating  last command
repeats the last text.
 
Undo the last change
u  undo last change.
U undo changes to the current line.
 
Copy and pasting lines
yy   copys the current line into buffer.
5yy  copies 5 lines from the current line.
   pastes the current buffer.
 
Recovering a unsaved vi file.
vi -r  filename  restores a unsaved / crashed file from buffer.
 
Searching
:/name    & return searches for the word name in the file 
n continues search forward.
N searches backwards.
 
Substitution
:s/<search-string>/<replace-string>/g
 
Saving
:w    saves the text does not quit.
:wq   saves & quit the editor .
ZZ    save
:q!   Quit without saving .


No comments:

Post a Comment