Wednesday, October 2, 2013

VI editor Shortcuts and Tricks



VI editor Shortcuts and Tricks

Definition -

The VI editor is a screen−based text editor available on all Unix computers.

Importance of VI editor -

1)      When we log on a remote server using SSH from a PC, only the text editors can be used to edit files.
      2)  Typists love working on it as it reduce the keyboard – mouse switch over time.
Creating new text file or editing an existing text file –
To edit a file named (say) "mytext" on a Unix computer, type the Unix command "vi
mytext".
# vi mytext
Or
#vim mytext
Different modes of VI Editor –
1)      Insert Mode - To switch to insert mode: press i (or a, or o)
In insert mode, everything you type appears in the document at the place where the
blinking cursor is.

2)      Command Mode - To switch to command mode: press Esc
In command mode, keystrokes perform special functions just like mouse and menu bar.
Exiting the VI Editor –
When you want to get out of the editor, switch to command mode (press Esc) if
necessary, and then
· type :wq RTN to save the edited file and quit, or
· type :q! RTN to quit the editor without saving changes, or
· type ZZ to save and quit (a shortcut for :wq Rtn), or
· type :w filename to save the edited file to new file "filename"
Note – RTN means enter key
Moving around in the Editor without Mouse
First Enter into your command mode then you can use the arrow keys to move the cursor up,
down, left, right. In addition, these keystrokes will move the cursor.
h left one character
l right one character
k up one line
j down one line
b back one word
f forward one word
{ up one paragraph
} down one paragraph
$ to end of the line
Control + B = back one page
Control + F = forward one page
17G to line #17
G to the last line.
Inserting Text
From command mode, these keystrokes switch you into insert mode with new text being inserted.

i just before the current cursor position
a just after the current cursor position
o into a new line below current cursor
I at the beginning of the current line
A at the end of the current line
O into a new line above current cursor

Cut, Copy, Paste Shortcuts-

 x delete (cut) character under the cursor
 24x delete (cut) 24 characters
 dd delete (cut) current line
 4dd delete (cut) four lines
 D delete to the end of the line from the cursor
 dw delete to the end of the current word
 yy copy (without cutting) current line
 5yy copy (without cutting) 5 lines
 p paste after current cursor position/line
 P paste before current cursor position/line

Search –
 We can go directly forward or backward to specified text using "/" and "?"

 /adarsh Rtn Move cursor to the next occurrence of the string "adarsh"
?adarsh Rtn  Move cursor backward to the previous occurrence of the string "adarsh"
 n repeat the last search given by "/" or "?"

Replace Commands -

 r replace 1 character (under the cursor) with another character
 8r replace each of the next 8 characters with a given character
R overwrite; replace text with typed input, ended with Esc
 C replace from cursor to end of line, with typed input (ended with Esc)
 S replace entire line with typed input (ended with Esc)
 4S replace 4 lines with typed input (ended with Esc)
 cw replace (remainder of) word with typed input (ended with Esc)

Source - Internet

No comments:

Post a Comment