The Vim text editor is among the best editors when it comes to editing in the command-line interface. You can use it not just as a text editor but also to quickly and efficiently navigate large texts or log files.
In this article, you will learn the different operational modes that Vim uses, how to navigate and edit text files, and as well as some tips and tricks that you can use to be more productive when using this editor.
What is VIM?
Vim stands for Vi Improved and is the enhanced version of the Vi text editor. It is one of the most popular CLI text editors and is highly regarded as the most versatile and powerful. It can also support a variety of programming languages, such as Python, Perl, Ruby, and Lua.
The “improved” concept came from the fact that not only it can support other programming languages, but it can also be customizable through the use of open-source plugins. VIM also now supports macros, visual modes, and an online support system, among others.
Installing VIM on your Linux device
If you still use the default Vi, and haven’t installed VIM yet, then you can install it by simply invoking the command:
$ sudo install vim
You can set VIM as your default text editor by simply adding the variable below in your bashrc script:
export VISUAL=vim export EDITOR="$VISUAL"
The different VIM modes
For starters, VIM has three basic modes which you will often use. These are the “Normal Mode“, “Command Line Mode” and the “Insert Mode”.
The normal mode will be the first mode when you invoke the vim editor to edit your file. You can do basic navigation within this mode and do searches for keywords as well.
To navigate through the entire text file you can press the following letters:
- k – to move up
- j – to move down
- l – to move right
- h – to move left
To search for a particular word or words, first, you need to type the forward slash symbol followed by the word you are searching for. Press ”Enter” to search for more similar words.
Command Line Mode
The most fundamental step you must do to invoke the command line mode is to first use the colon symbol ‘:’ when you are still in the Normal mode. In this mode, you can invoke certain commands that can affect the entire file you are editing.
Some of the most common commands are:
:w– means to write or save
:w <filename>– if you want to name a new file.
:q– means to quit and exit the editor
:q!– means to exit the editor without saving the changes
The command line mode can be easily recognized as it is found on the lower bottom of the Terminal when trying to invoke a command.
If you think that the colon is not appearing or if you are not in the command line mode, simply press the “Esc” key two or three times for good measure, just so you can return to the normal mode.
The insert mode is where you will do most of the editing on your file. You can enter it by simply pressing “i” when you are still on the Command Line mode.
In this mode, you can edit the words on a text similar as if you are using Word or Google Docs. You can navigate from line to line or character by character using the arrow keys on your keyboard.
VIM Tips and Tricks
In this section, we will be showing you some neat little tricks, along with some tips when using VIM. If you have other useful tips or tricks, you can share them by commenting below.
Getting started with Vimtutor
VIM has a dedicated tutorial program designed to kickstart the unfamiliar user with its functionalities. It comes together with VIM once you install the latter.
To get started with Vimtutor, execute the following command on your Terminal:
In this tutorial, vimtutor will start things off by letting you navigate its field in normal mode. The next succeeding lessons will get more and more complex but you will surely have fun while learning along the way.
Setting number lines in your VIM workspace
A helpful tip is that you can set number lines when working in VIM. Number lines within VIM can be beneficial when operating with large files WITH hundreds if not thousands of lines of text.
To set the number lines, you need to be in the command line mode. Once you are in the command line mode, execute the command:
Navigating to specific lines in an instant
If you ever needed to go to a specific line indicating a particular error when testing a program or compiling a code, then this helpful tip would surely benefit you.
After adding the number lines for clarity, you can directly go to a specific line by simply typing the command:
Gone are the days when you have to constantly scroll or press something repeatedly just to go to that specific line. We hope you like this tip. As a bonus trick, you can search and replace certain keywords in VIM as well. Simply click the link provided for more details.
We’ve only scratched the surface of what VIM is capable of. As a powerful text editor, it can do more and give you a very convenient and efficient work program.
You can only appreciate the versatility it provides and the functionality it offers if you continue to learn and practice VIM as your primary and default editor.
If this guide helped you, please share it. ?