How to use VI editor in LINUX
The improved version of vi editor is vim editor. VI editor have 2 modes. By default vi editor is in common mode and if you want to write something then you have to type i which will take you to insert mode. If you want to save this then you have to first click ESC and then give commands to save or to do other operation.
dd ---> Delete the specified n number of lines yy ---> Copy the specified n number of lines
JOINING LINES:
J ---> Join two lines yyp ---> Repeat the current line ddp ---> Swap two lines
MOVE AND DELETE FORWARD AND BACKWARD:
w ---> Move one word forward b ---> Move one word backward w ---> Move specified number of words forward dw ---> Delete one word yw ---> Copy one word dw ---> Delete specified number of words
SEARCH A STRING:
/string ---> Forward search for given string ?string ---> Backward search for given string /^string ---> Forward search string at beginning of a line /string$ ---> Forward search string at end of a line n ---> Go to next occurrence of searched string /\ ---> Search for the word he (and not for there, here, etc.) /pl[abc]ce ---> Search for place, plbce, and plcce
REPLACE ALL:
:{startLine,endLine} s/{oldString}/{newString}/g Example: Commands Action
:1,$ s/readable/changed/ ---> Replace forward with backward from first line to the last line :3,6 s/letters/neww/g ---> Replace forward with backward from third line to the ninth line
To open a file:
vi file_name
If the file doesn't exist then it will create the file and open it in common mode.
COMMAND FOR SAVING AND QUITTING:
:wq ---> Save and Exit
:w ---> Save
:q ---> Exit without saving.
:w file_name ---> Save as file_name
ZZ ---> Save and Exit
:q! ---> Exit discarding changes made
:w! ---> Save (and write to non-writable file)
SWITCH FROM COMMAND MODE TO INSERT MODE:
i ---> Start typing before the current character
I ---> Start typing at the start of current line
a ---> Start typing after the current character
A ---> Start typing at the end of current line
o ---> Start typing on a new line after the current line
O ---> Start typing on a new line before the current line
TO MOVE AROUND A FILE:
j ---> To move down
k ---> To move up
h ---> To move left
l ---> To move right
You can do this with arrow keys also.
TO JUMP LINES:
G ---> Will direct you at the last line of the file
`` ---> Will direct you to your last position in the file
TO DELETE:
x ---> Delete the current character
X ---> Delete the character before the cursor
r ---> Replace the current character
xp ---> Switch two characters
dd ---> Delete the current line
D ---> Delete the current line from current character to the end of the line
dG ---> delete from the current line to the end of the file
TO REPEAT AND UNDO:
u ---> Undo the last command
. ---> Repeat the last command
COMMAND FOR CUT, COPY AND PASTE:
dd ---> Delete a line
yy ---> (yank yank) copy a line
p ---> Paste after the current line
P ---> Paste before the current line
COMMAND FOR CUT, COPY AND PASTE IN BLOCKS:
No comments