VIM is one of the most popular and widely used text editors among Linux users. It stands for Vi IMproved, as it is an enhanced version of the original Vi editor, which was created by Bill Joy in 1976. VIM is free, open-source, cross-platform, and highly customizable. It can be used for editing any kind of text, from code to configuration files, from plain text to markup languages.
In this blog post, we will cover the following topics:
- How to install and launch VIM on Linux
- How to use different modes and commands in VIM
- How to edit, save, and quit files in VIM
- How to search, replace, and navigate text in VIM
How to Install and Launch VIM on Linux
VIM comes pre-installed on most Linux distributions, but if you don’t have it on your system, you can install it using the package manager of your choice. For example, on Debian-based systems, you can use the following command:
sudo apt update
sudo apt install vim
To verify that VIM is installed correctly, you can run the following command:
vim --version
This will show you the version and features of VIM on your system.
To launch VIM, you can simply type vim
in the terminal, followed by an optional file name. For example, to create and edit a new file called test.txt
, you can use the following command:
vim test.txt
This will open VIM with a blank screen and a message at the bottom:
"test.txt" [New File]
How to Use Different Modes and Commands in VIM
One of the most distinctive and important features of VIM is its modal nature. This means that VIM has different modes of operation, each with its own purpose and set of commands. The main modes in VIM are:
- Normal mode: This is the default mode when you launch VIM. In this mode, you can use various keyboard commands to manipulate and navigate the text, such as deleting, copying, pasting, moving, etc. You can also switch to other modes from this mode.
- Insert mode: This mode is used to insert and edit text in the file. You can enter this mode by pressing
i
in normal mode. You can exit this mode by pressing Esc
to return to normal mode. - Command-line mode: This mode is used to enter and execute commands that affect VIM or the file, such as saving, quitting, searching, replacing, etc. You can enter this mode by pressing
:
in normal mode. You can exit this mode by pressing Enter
to execute the command or Esc
to cancel it. - Visual mode: This mode is used to select and highlight text in the file, which can then be operated on by other commands. You can enter this mode by pressing
v
in normal mode. You can exit this mode by pressing Esc
to return to normal mode.
There are also other modes in VIM, such as replace mode, select mode, and operator-pending mode, but they are less frequently used and beyond the scope of this post.
To use VIM effectively, you need to master the different modes and commands in each mode. The following sections will introduce some of the most common and useful commands in VIM.
How to Edit, Save, and Quit Files in VIM
To edit text in VIM, you need to switch to insert mode by pressing i
in normal mode. This will change the message at the bottom to:
-- INSERT --
Now you can type and edit text as you would in any other text editor. You can use the arrow keys, the Home and End keys, and the Backspace and Delete keys to move and modify the text.
To exit insert mode and return to normal mode, you need to press Esc
. This will change the message at the bottom to:
"test.txt" 4L, 27C
This shows you the file name, the number of lines, and the number of characters in the file.
To save the file, you need to switch to command-line mode by pressing :
in normal mode. This will show a colon at the bottom left corner, where you can type the command. To save the file, you can use the w
command, which stands for write. For example:
:w
This will save the file and show a message like:
"test.txt" 4L, 27C written
You can also save the file with a different name by specifying the name after the w
command. For example:
:w new.txt
This will save the file as new.txt
and show a message like:
"new.txt" 4L, 27C written
To quit VIM, you can use the q
command, which stands for quit. For example:
:q
This will quit VIM and return to the terminal. However, if you have unsaved changes in the file, VIM will not let you quit and show a message like:
E37: No write since last change (add ! to override)
This means that you need to either save the file before quitting or force quit without saving. To save and quit in one command, you can use the wq
command, which stands for write and quit. For example:
:wq
This will save the file and quit VIM. To force quit without saving, you can use the q!
command, which stands for quit with a bang. For example:
:q!
This will quit VIM without saving the file and discard any changes.
How to Search, Replace, and Navigate Text in VIM
To search for a text pattern in VIM, you can use the /
command in normal mode. This will show a slash at the bottom left corner, where you can type the pattern. For example, to search for the word “text”, you can use the following command:
/text
This will highlight the first occurrence of the word “text” in the file and show a message like:
/text
text.txt" 4L, 27C
To find the next occurrence of the word, you can press n
in normal mode. To find the previous occurrence, you can press N
in normal mode. To clear the highlight, you can press :noh
in command-line mode.
To replace a text pattern with another text in VIM, you can use the :%s
command in command-line mode. This command stands for substitute and takes the following syntax:
:%s/old/new/g
This will replace all occurrences of the old text with the new text in the file. For example, to replace the word “text” with the word “file”, you can use the following command:
:%s/text/file/g
This will show a message like:
4 substitutions on 4 lines
You can also use various options and modifiers with the :%s
command, such as case sensitivity, confirmation, and regular expressions. For more information, you can use the :help :s
command in command-line mode.
To navigate text in VIM, you can use various keyboard commands in normal mode. Some of the most common and useful commands are:
h
: move the cursor leftj
: move the cursor downk
: move the cursor upl
: move the cursor rightw
: move the cursor to the next wordb
: move the cursor to the previous worde
: move the cursor to the end of the word0
: move the cursor to the beginning of the line$
: move the cursor to the end of the lineG
: move the cursor to the end of the filegg
: move the cursor to the beginning of the fileCtrl-f
: move the cursor one page forwardCtrl-b
: move the cursor one page backward
Ctrl-d
: move the cursor half a page downCtrl-u
: move the cursor half a page up:n
: move the cursor to the line number n
You can also use a number before any command to repeat it that many times. For example, to move the cursor 10 words forward, you can use the following command:
10w
VIM Cheatsheet :
https://devhints.io/vim
Comments
Post a Comment