Introduction to Vim & Vi editor
vim is an improved version of vi
- vim is by far the most distributed and used text editor in the Linux
- Depending on your Linux distro, vim may or may not be installed
- They are terminal applications, So you’ll have to start and used them in the terminal with commands
Use cases to use text editor in CLI:
- Small modifications can be faster, especially when you are currently working in the CLI
- Supports multiple formats
- When working on a remote server
Installation of Vim editor
$sudo apt install vim
Opening a file
$vim <filename> = open file with vim
In vim editor there are 2 mode
- Command mode
- Insert mode
Command mode:
- This is the default mode
- You can’t edit the text
- Whatever you type is interpreted as command
- Navigate, Search, Delete, Undo and etc..
- To switch to command mode press esk key
esc key
Insert mode
- Allows you to enter text
- To switch to insert mode press i key
i key
Write and Save
- After writing in the insert mode we can save and quit
:wq
This will write files to the disk and quit vim
Without saving the changes
:q!
Type :q! => Quit vim without saving changes
Delete entire line
dd
dd => Delete the entire line
Delete next lines
d10
d10 => Delete the next 10 lines
Undo
u
u => undo the changes
End of line
A
Type ‘A’ => Jump to end of line and switch to insert mode
Start of line
0
Type ‘0’ => Jump to start of the line
Specific Line
<linenumber>G
10G
Type 10G => Go to line 10
Pattern
Type /pattern => search for pattern (enter)
Type n = Jump to the next match
Type N = Search in the opposite direction