How to use vim for Beginners
An awesome text editor for programmers
In this tutorial, we are going to take a look at look at how we can use the vim and why it is better than the other text editors. Also, we will learn how to use it on Android Devices.
Ready?
Let’s get started!
What is vim? and why it is a better text editor?
Vim is an acronym for Vi IMproved. It is a free and open-source text editor written by Bram Moolenaar. It was first released in 1991 for UNIX variants and its main goal was to provide enhancement to the Vi editor, which was released way back in 1976.
Vim is highly customizable and extensible, making it an attractive tool for users that demand a large amount of control and flexibility over their text editing environment. There are many plugins available that will extend or add new functionality to Vim, such as linters, integration of git, showing colors in CSS.
What we are going to learn
- How to start vim
- Vim Modes
- Creating new file
- View file in read-only mode
- Edit existing file
- Vim help manual
- How to installed and run Vim on Android
How to Start and Close vim
Using Vim from terminal will be identical on Windows as well as Linux platform. Perform the following steps to start and quit Vim from terminal
$ vim
It will open Vim in the terminal as follows
to close this, press Esc
key followed by colon :
and q
. In Vim q
command stands for quit. This command will be shown in the bottom left corner of the editor itself
Vim Modes
Vim supports multiple modes. This section discusses some of the important modes which will be used on a day-to-day basis.
Command Mode
This is the default mode in which Vim starts. We can enter editor commands in this mode. We can use a variety of commands in this mode like copy, paste, delete, replace and many more. We’ll discuss these commands in later sections.
NOTE − Here onwards, any Vim command without colon indicates that we are executing that command in command mode.
Insert Mode
You can use this mode to enter/edit text. To switch from default command to insert mode press i
key. It will show the current mode in the bottom left corner of the editor.
We can enter any text once we are in insert mode. Below image shows this
Use the Escape Esc
key to switch back to command mode from this mode.
Command Line Mode
This mode is also used to enter commands. Commands in this mode starts with colon :
. For instance, in the previous section quit command was entered in this mode. We can go to this mode either from command or insert mode.
- To switch from command mode to this mode just type colon
:
- To switch from insert mode to this mode press
Esc
and type colon:
In below image colon at bottom left indicates line mode.
NOTE − Here onwards, any Vim command starting with colon indicates that we are executing that command in command line mode.
Visual Mode
In this mode, we can visually select text and run commands on selected sections.
- To switch from command mode to visual mode type
v
- To switch from any other mode to visual mode first switch back to command mode by pressing
Esc
, then typev
to switch to visual mode
In below image bottom left corner shows visual mode.
Create new file
Perform the below steps to create and save new file
Execute the following command to open Vim
$ vim
Type following command in Vim
:edit message.txt
Switch to insert mode
i
Enter some text and Switch back to command mode
Esc
Save changes to file
:w
Now message.txt file will be created. To quit Vim
:q
Open file in read-only mode
Use –R option to open file in read-only mode
$ vim -R message.txt
In below image bottom left corner shows read-only mode
Alternatively, you can use view command to achieve the same result.
Edit existing file
Perform below steps to edit existing file
Open file using Vim
$ vim message.txt
Switch to insert mode and enter some text there.
i
Quit editor without saving changes
:q!
Save changes and quit editor using following command
:wq
Vim is a feature-rich editor hence remembering everything about it will be difficult. But there is no need to worry, we can always ask for help. Fortunately, this help is provided by Vim itself.
Access help manual
The help manual is shipped with the Vim editor itself and it is really comprehensive. To access help execute below command
:help
Help on specific topic
Help manual will show entire help about Vim. But what if, we are only interested in certain topics. Vim provides command for that as well with following syntax
:help <topic-name>
In the above command replace <topic-name> with the topic in which you are interested. For instance to access help about vim mode, execute following command
:help modes
Access online help
Vim also provides online help. To access online help
How to install and run Vim on Android
- Install termux app from google play store
- After installation open the app and type $ pkg update && pkg upgrade
- After that has been done type $ pkg install vim
That it now you have vim installed in your Android Device enjoy!
Conclusion
In this tutorial, we learned how we can use the vim text editor to edit text files and we also learned how to use vim’s built-in help manual.
I hope you like this article. If you did, please share it with your friends!