There are several tools available in Linux to edit a regular file. The most popular and user text file editor is VI, and VI Modified is called VIM.As part of providing Linux support services to clients, here we listing out some the useful commands in vi editor
file β use file command to know the file type
vi is very hard to learn, but really powerful
Either learn VI or learn Unix :-
VI:- VI is a vey powerful text editor. The name vi is derived from the shortest unambiguous abbreviation for the command visual in βexβ; the command in question switches the line editor ex to visual mode.
The VI has three mode
- Command Mode β simple one/two letter commands
- Edit Mode β Allows to append and insert text
- Ex Mode β allows to execute commands
Going back and forth modes requires to go to command modeby pressing escape.
Command Mode:-
Arrow keys β hjkl
w β moves cursor forward by a word
b β moves cursor backward by a word
0,$ β beginning and end of line
Ctrl + f , Ctrl + b β Page UP, Page Down
Home β gg
End β G
:25, 25G β to the 25th line
% β matching braces
1. Insertion
Insertion β I, i
Append β A, a
Insertion on a new line β O, o
2. Deletion
Delete a single character under cursor β x
Delete a single character left of cursor β X
Delete all the stuff from cursor to end of line β D
delete 10 words β 10dw , d10w
Replace a single character β r
Undo the last change β u
To repeat last command β .
To join two lines together β J
3. Search in command mode
Search for a word downwards β /word_to_search
Search for a word upwards β ?word_to_search
Repeat the previous search forward β n
Repeat the previous search backward β N
4. Search and Replace in ex mode
Change a single character β r
Replace mode by typing over β R
Search and replace first occurence of old_word with new_word in the entire file β :%s/old_word/new_word/
Search and replace first occurence in the current line β :s/search/replace/
Search and replace all occurences in the entire file β :%s/search/replace/g
Search and replace all occurences in current line β :s/search/replace/g
Search and replace the first occurence of old_word between n1 and n2 with new_word β :n1,n2s/old_word/new_word/
Search and replace all occurences of old_word between n1 and n2 with new_word β :n1,n2s/old_word/new_word/
Search and replace all occurences of old_word with new_word, but prompt for confirmation β :1,$s/old_word/new_word/gc
5. Cut/Copy/Paste
Cut the entire line β dd
Copy the entire line β yy
Cut the word from the current cursor position to its end β dw
Paste β p, P
Swap the adjacent lines β ddp
Cut 10 lines β 3dd
6. Exiting VI
Save in ex mode β :w
Exit in ex mode β :q
Forcefully save in ex mode = :w!
Forcefully exit in ex mode, when a change is made accidentally = :q!
Save and Exit in command mode β ZZ
Save and Exit in ex mode β :wq, Γ°ΒΒΒ‘
Save and Exit in ex mode forcefully : wq!
7. Advanced VI to ponder with
:map to map the new defined command to a sequence of commands or keystrokes
:map Ctrl+v_Ctrl+v_Space Ctrl+v_Ctrl+f means pressing space will enable page down. Ctrl + v is to input a special character such as Esc.
Encryption β :X and disable encryption :set key=
So βEither learn VI or learn Unixβ Γ°ΒΒΒ
As part of improving the quality of our Linux support services, we welcomes your feedbacks on the command box Γ°ΒΒΒ