This post will show you how to use VIM! Vim is a text editor that edits all kinds of text files. For instance, the text file may be a shell script with a .sh extension or a text file with a .txt extension.
You can use the following command to check its installation in your system:
dpkg -l vim
Before installing it to any distribution of Linux, first, update your system by using the update command. Then, if any command does not work during the installation or update, run it by using sudo before the command.
For Debian-based distributions like Ubuntu, Linux Mint, or Kali Linux:
apt-get install vim
apt install vim
For Fedora-based distributions like CentOS and Red Hat Enterprise Linux (RHEL):
yum install vim
Mac OS Installation
Run the following command to install it on Mac with Homebrew:
brew install vim
Then, run the following command to check its version:
What is VI / VIM Editor?
VI / VIM is the default text editor that is provided on the Linux platform. It can be quite intimidating to
What makes VIM different
VIM is split into several modes that each have different purposes. VIM starts in “normal” mode by default. However, you can use the keys below to enter the desired mode and press Esc to return to normal mode.
Here is a description of some of the modes in VIM, using the most common commands:
Normal mode - This mode allows you to open a file and move around in it. You can use the cursor keys to move around, or you can use h (left), j (down), k (up), and l (right) to move as well.
Insert mode - As the name suggests, this mode allows you to insert text. Using the 'i' key will switch the editor into insert mode. Similarly, the 'a' key will switch the editor into append mode after the current character position. Finally, the 'o' key inserts a new line below the current line and enters insert mode on the new line.
Visual mode - This is used to make selections of text rather than "viewing" per se. Press the 'v' key to enter the visual mode. Doing so will also mark a starting selection point. Now move the cursor until the desired end selection point. Remember to press Esc to return to normal mode.
Command mode - To enter command mode press ':' and then type your command. Your command will appear at the bottom of the window.
Here are some basic commands you may want to use:
- :h or :help will show you the help file.
- :w will save the changes without exiting from the editor window.
- :wq will both save the changes and exit from the editor.
- :q use on its own will discard the changes you have made.
Open/create a file
For example, run the following command to open/create a file:
If the file already exists, then you can edit that file. Moreover, If no file exists with this name, then it will create a new file.
Insert data into a new or existing file
When you run the command vim filename, an editor window will open.
Follow these steps to write data into a new or existing file.
- Press i to enter the insert mode
- Write any data you want to write to that file
- After you have written the data, press esc key to get out of the insert mode (to go back to normal mode)
- Press :w to save the changes without exiting from the editor window. Here, w means writing the changes to the disk
- Press :wq to both save the changes and exit from the editor. Here, w means you want to write changes to disk and q means you want to exit from the editor window.
- However, if you want to discard the changes you have made then press :q alone instead of :wq
- If :wq gives error then press :wq!
The above steps will remain the same whether the file already exists or you create a new file.
Create a file by inserting the data on the terminal/stdin
Follow the following steps if you want to write data from the terminal/stdin to a file.
- vim -
- When you run this command you will get a message like Vim: Reading from stdin… Now, you can write data to this file
- After you write the data, press ctrl+d (it will take you to the editor window)
- Press :wq filename to both save the changes and exit from the editor
- If :wq filename gives error then press :wq! filename
Append data to a particular line of the file
After opening the file using vim filename command, take the cursor to any line and press shift+a.
When you press shift+a, the cursor position will be set to the end of that particular line. You can then you can add data.
After adding the data, you can use again :wq or :wq! to save the changes and exit from the editor.
Delete data from a file
To delete the data character by character, first, open the file by vim filename command. Then, after opening the file, press x by taking the cursor on the character you want to delete.
To delete any word, take the cursor on the top of that word and press dw. Moreover, you can also delete to the end of the line by pressing the d$.
You can delete a line completely by pressing dd.
Here, d means you want to delete something, and w or $ determines the length for which you want to perform the deletion.
You can also delete all the contents of a file by using the following command:
Where 1 is the starting position, $ is the ending position, and d means delete.
Set the cursor position in the file
One way to move the cursor is to use the arrow keys. You can also use many other shortcuts to move around the file at different positions e.g.
- Press 3w to move the cursor 3 words forward
- Press 5e to move the cursor to the end of the 5th word in the forward direction
- Press 0 to go to the start of the line
- Press shift+g or :$ to go to the end of the file
- Press gg to go to the first line
Copy and paste data
To copy any line, press yy by taking the cursor to any position of any line.
To paste the already copied line, press p by first going to the normal mode(pressing ESC).
Moreover, you can also copy multiple lines by pressing Nyy Where N can be any number. For instance, to copy 5 lines, we can press 5yy.
Search a string
To search any word from a file, first, open the file. Make sure you are in the normal mode (by pressing Esc). Now, press / followed by the search string e.g.
Moreover, if there are multiple occurrences of the search_string, then press n to go to the next occurrence. You can also go back to the previous occurrence by pressing shift+n.
Replace a word with another word
By going to the normal mode, you can run the following command:
You can replace old_word and new_word with your own desired words. This command will replace only the first occurrence of old_word.
However, you can replace all the occurrences of old_word with new_word by the following command:
Where g means globally(to replace all the occurrences).
How to get help?
Run the following command in the normal mode to get help about different commands:
Moreover, you can get detailed help about Vim by running the following command in the terminal:
Frequently asked questions
You can check your OS name and Linux version by using the following three commands: cat/etc/os-release, lsb_release -a or hostnamectl. Open the terminal and type in any of the above commands, and hit enter. For remote servers, log in first using ssh user@server-name. Alternatively, you can use the command uname -r to find out the Linux kernel version that you are using.
VI editor derived from the word Visual is a text editor made for all *nix operating systems. It is the most widely used text editor because it works across different distributions and platforms. A VI editor works in two modes, i.e., the command mode and insert mode. The command mode only understands commands, while the insert mode allows text inputs to files.
You can only exit the VI editor while in command mode. For example, switch to command mode by pressing the Esc button if you are in the insert mode. Then type:wq or zz to save and quit or exit the VI editor.
To save a file in a VI editor, you need to switch to command mode. Switch to the command mode by pressing Esc and type and enter:wq to save your work. Wq stands for write and quit
You may also be interested in
About the Authors
Anto's editorial team loves the cloud as much as you! Each member of Anto's editorial team is a Cloud expert in their own right. Anto Online takes great pride in helping fellow Cloud enthusiasts. Let us know if you have an excellent idea for the next topic! Contact Anto Online if you want to contribute.
Support the Cause
Support Anto Online and buy us a coffee. Anything is possible with coffee and code.