Vi editor
When you work on Linux server vi is the default text editor you will find. The oldest and most commonly used text editor. Vi is visual editor, you can use only keyboard and perform text editing operations.
vi works on two modes.
Command Mode - where all the key strokes are treated as commands and executed on the text file.
Insert Mode - in this mode whatever you type will get recored/inserted in the file.
Vi examples
Esc
is used to go to command mode
Some commands will take you to edit mode
Lets see step by step how to work with vi editor.
Creating file
We can create file using vi filename
it will display editor as follow
Please note when we use filename to start vi editor, if file exist it will open it else vi will create the file and open it.
If we start vi editor without specifying the file name then while saving you can specify the name
Now we have created the file lets edit it.
Vi starts the editor in command only so all the keystrokes are now considered as commands no they are not inserted in the file.
To go to insert mode you can user following commands
- i - To insert before the cursor position
- o - Open a line below the current line
- O - Open a line above the current line
Lets follow the instructions to create file, edit it and use curson movement keys.
Step 1 : Start vi editor using vi filename
Step 2 : Press i
once, you wont see anything on screen but mode is changed to insert mode
Step 3 : Type following lines in the editor
Use only use enter button to insert new line, not mouse or any other button.
Step 4 : Once the lines are typed go back to command mode using Esc
button
Now you know how to open file and swithc to insert mode using i
and come back to command mode.
Lets now use cursor movement keys. You can move your curson in command mode using the following keys
- H - Move to upper Left corner
- M - Move to middle line
- L - Move to lower left corner
- h - Move to one char back
- j - Move to down a line
- k - Move to up one line
- l - Move to one char forward
- ^ - Move to beginning of line
- $ - Move to end of the line
- w - Move one word forward
- b - Move one word backword
Try to practice all above keys to move your cursor in the file.
You will be able to do it very quickly once to practice enough.
Lets now also use the small o
and Capital O
to insert line belo and above the curson positions
Go to the last line of the file using cursor movement keys and use O
to insert line, your mode is now changed to insert mode and you can type in the file.
Remember once you finish typing press esc
to go back to command mode.
Similarly you can also do o
to insert line below the curson position.
Now you lets move forward and save the file.
Step 1 : You must be in command mode to save file. Press Esc
if you are not in command mode.
Step 2 : User :w
to write changes to file. Editor will tell you few charactors are wrtten in the file.
Step 3 : If you have not give name of file then while saving you have to specify file name as :w filename
Step 4 : If you want to quite from file then use :q
command. You can only use it if you have saved all your work.
Step 5 : To save your work and quite from vi you can combine command like :wq
Step 6 : To quit without saving your work use :q!
Summary of file saving commads
- :w - to save file
- :w filename - specify the file name in which we want to save
- :wq - Save and quite file
- :q! - quite vi without saving the file
Lets now see window movement commands and how to use them
To see the effect of window movement commands we will need a file with good amount of content in it, We can make use of Lorem Ipsum website to get dummy content.
So lets go to https://www.lipsum.com/ and copy content in file
Once you save the file, go back to command mode and use following keys to scroll through screen content and do a pattern search
- d - Scroll down (half a screen)
- u - Scroll up (half a screen)
- f - Page forward
- b - Page backward
- /string - Search Forward
- ?string - Search backward
- n - Repeat search forward
- N - Repeat search reverse
- G - Go to last line
- :n - Go to line number n
Note : you can use :set number
to see the line numbers in the vi editor, and use Ctrl + g to see the current line number
Lets now see how we use delete commands, all these commands will be used in the command mode.
- dd - to delete entire line under the cursor ( acts like cut)
- ndd - n is number of lines to delete from the cursor position ( acts like cut)
- dw - delete the word under the cursor ( acts like cut)
- dnw - delete n words from the cursor
- d) - delete till the end of the sentance
- db - delete previous word
- D - delete to end of the line
- x - delete the character under the cursor
Note first three commands acts like cut means you can paste the deleted content using paste commands which are as shown below
Lets see copy and paste commands
- yy or Y - to copy the line
- yw - copy word
- p - Paste after the cursor
- P - Paste before the cursor
Undo commands are as follows
- u - undo last change
- U - undo all changes on the line
Finally we will see search and replace commands The syntax for the search and replace command is as follows
[address]s/old_text/new_text/
Address or addresses can be specified as follows . –> specifies current line n –> specifies Line number n .+m –>current line + m lines $ –> Last line /string/ –> Line containing the string % –> Entire file
Lets see some of te examples so that you know how to use the find and replace
Example 1 : Search for the first occurrence of the string apple in the current line and replace it with banana
:s/apple/banana/
Example 2 : Replace all occurrences of the search pattern in the current line
:s/apple/banana/g
Example 3 : Search and replace the pattern in the entire file
:%s/apple/banana/g
Example 4 : Search and delete the pattern
:%s/apple//g
Excercise
Create file helloVi.txt using vi editor
Type 5 lines in the file as shown following
Save file and exit the vi
Open helloVi.txt file and make wash work Capital from 5 line
Set line numbers in the vi editor
Convert the tears to fear using search and replace in vi
Delete the 3rd line
Copy paste the 4th line and paste below the last line