Introduction
As a programmer or a developer, we require an efficient, versatile, and fast text editor. Linux has a built-in Vi text editor. Vim is an improved version of Vi, whereas Vim stands for Vi Improved. Vim is a highly efficient and powerful text editor that is widely used by programmers, and system administrators. In this article, we will explore the different modes of Vim and basic commands for editing.
Vim may or may not be pre-installed depending on the Linux distro. You can download Vim by running this command in the terminal:
sudo apt install vim
Advantages of using CLI text editor
One of the primary advantages of using a CLI editor like Vim is the speed and efficiency. It is better and faster to use Vim rather than just using file explorer to navigate and edit files. Vim supports multiple file formats which makes it more useful and it is faster to create and edit files at the same time. Vim is majorly used when working with remote servers where GUI is not available.
Modes in Vim Editor
We use command vim
followed by the name of the file to open or create a file. Like, if we want to edit/create a file named "example.txt", use the command vim example.txt
.
You familiarize yourself with the three modes it operates in. These modes are:
Normal mode
Insert mode
Command Mode
1. Normal Mode
This is a default mode in Vim Editor which enables you to navigate through the text and execute various functions using keyboard commands. Some of the essential commands in normal mode include:
h
, j
, k
, l
: Allow you to move the cursor left, down, up, and right, respectively.
$
: Jump to the end of the line without switching to insert mode
A
: Jump to the end of the line and switch to insert mode
0
: Jump to the beginning of the line
r
: Replace the character. Move the cursor on the character you want to replace and press r
and then enter the character you want to replace with
x
: Delete the character. Move the cursor on the character you want to delete and press x
.
u
: Undo the last change
dd
: Delete the entire line
d[total number of lines]d
: Use for deleting multiple lines. For example, if you want to delete 10 lines after your cursor then press d10d
.
[line number]G
: Jump to a specific line number. To jump to line 12, we use the command 12G
.
2. Insert Mode
In insert mode, you can add text to the file. Press the i
key while in normal mode to switch to insert mode. Once you enter the insert mode, you can type in the text you want to add.
Insert mode commands:
i
: Switch to insert mode
I
: Moves the cursor to the beginning of the line and switch to insert mode
3. Command mode
We use command mode for various functionality like searching, replacing and saving the files using different commands. Press :
to enter into command mode.
Command mode commands:
:set number
: This command displays line numbers in the file.
:/example
: This command searches for the word "example" in the file.
After searching into files you can press:
n
for jumping to the next matchN
for jumping to the previous match
:%s/old/new/g
: This command replaces all occurrences of "old" with "new" in the entire file.
:w filename
: This command saves the modifications made to the file with the name "filename".
:wq
: This command saves the file and exits the Vim editor.
:q!
: This command quits the Vim editor without saving the modifications made to the file.
Conclusion
In conclusion, Vim Editor is a powerful and versatile text editing tool that offers a wide range of functionalities. Whether you are a programmer, developer, or system administrator, Vim Editor caters to your text editing needs. By mastering Vim Editor's basic and advanced commands, you can improve your productivity and efficiency.
I hope that this comprehensive guide has been helpful in your quest to effectively utilize Vim Editor.