vi editor
Syntax: vi <filename>
-If "<filename>" exists and if user has appropriate permission is there, then that file will be opened
-If "<filename>" doesn’t exist and if user has appropriate permission, then new file will be opened
Once the file is opened, there will be two modes
- Command mode – This is the default mode in which vi will open file
- Insert mode – In order to make changes to the file, user has to change to Insert mode from Command mode.
Switching from Command mode to Insert mode
Any of the following characters will change to Insert mode with corresponding action
To convert all alphabets to lower case in a file, use below string.
Hit 'Esc' and then type:%s/.*/\L&/
- i (lower case) – Insert before cursor
- I (Upper case) – Move cursor to beginning of Line
- o (lower case) – Open line below cursor
- O (Upper case) – Open line above cursor
- a (lower case) – Insert after cursor
- A (Upper case) – Insert at the end of line
- s (Lower case) – Substitute at Current position of cursor
- S (Upper case) – Remove entire line
Switching from Insert mode to Command mode
- Switching to Command mode from Insert mode, hit "Esc"
- Saving the changes made in Insert mode, Switch to Command mode and then type ":wq"
- Saving the changes made in Insert mode forcefully, Switch to Command mode and then type ":wq!"
- Ignore changes made in Insert mode and revert to Initial state, Switch to Command mode and then type ":q!"
- Undo changes made in the Insert mode, Switch to the Command mode and the type "u"
- To save file with a different name, Switch to command mode and then type ":wq <filename>"
Navigation in vi editor
set -o vi
h - left
j - down
k - up
l - right
w - word
b - back
0 or ^ - beginning of line
$ - ending of line
x - delete a character
dw - delete a word
dd - delete a line
D -
u - Undo/revert
U - Revert all changes
/ - search from top to bottom
? - search from bottom to top
set -o vi
h - left
j - down
k - up
l - right
w - word
b - back
0 or ^ - beginning of line
$ - ending of line
x - delete a character
dw - delete a word
dd - delete a line
D -
u - Undo/revert
U - Revert all changes
/ - search from top to bottom
? - search from bottom to top
:set number (or) :se nu - to display line numbers
:set ignore case (or) :se ic - to display line numbers
:%s/<existing_word>/<new_word>/g - replace everywhere (global)
:%s/<existing_word>/<new_word>/gc - replace wherever we need
To convert all alphabets to upper case in a file, use below string.
Hit 'Esc' and then type:%s/.*/\U&/
Hit 'Esc' and then type:%s/.*/\U&/
To convert all alphabets to lower case in a file, use below string.
Hit 'Esc' and then type:%s/.*/\L&/
To delete all the lines matching a pattern in a file, inside vi editor
Command$ Hit 'Esc' and then type :g/<pattern>/d
We can use the same concept as below
:g/^$/d --> to delete all blank lines in the file
to delete control M (^M) characters in a file inside vi editor
Command$ :%s/(Ctrl V + Ctrl M)//g
to find and replace word by word
Command$ :%s/existingpattern/ newpattern /gc
No comments:
Post a Comment