Understand Vim Modes
Vim is a powerful text editor, and is also referred to as a modal text editor.
What does "modal text editor" mean? It means that Vim is a text editor that has a mode-based workflow. Each keystroke is paired to a command in Vim. This is in contrast to other editors where each keystroke is registered as text and is written to a text file.
So, what are these modes in Vim? Let's have a look at them.
Modes in Vim
Each mode in Vim is responsible for handling user input and processing them based on the mode you are in.
Vim has three modes. Let's discuss what each mode does.
Normal mode
The default mode that Vim opens with is Normal mode. Sometimes, Normal mode is also called Command mode.
The Normal mode is used for performing actions like navigation, text substitution, cut, copy, paste, delete, etc.
You cannot write in normal mode. For that, you need to switch to insert mode.
Insert mode
The Insert mode in Vim is what you have naturally come to expect from text editors. Press a few alphanumeric character keys on your keyboard and they appear in the text file that you have open.
In other words, you actually write text to a file in the insert mode of Vim editor.
Visual mode
Vim is a text editor designed around a terminal interface (computer mice did not exist back in the days), so you cannot use your mouse to select/highlight text.
That is why we have a Visual mode in Vim. It is used to select text with arrow keys so that various operations (like cut, copy, paste, delete, etc) can be performed on selected text.
How is it different? Well, you can start selecting from any point till any point in any line. In the normal mode, you can delete words and lines but not a selection of text. Visual mode gives you that power.
Visual Block mode
This mode is a subset of the Visual mode. Similar to Visual mode, this mode allows you to select text, but in as block shaped selection.
Changing modes in Vim
Since there are multiple modes available in Vim, how do we switch between them?
From Normal mode to Insert mode, and back
As I mentioned earlier, the default mode that Vim opens in is Normal mode. It allows us to navigate between characters, words, lines and blocks of code, but it does not type the text in the file we have open.
To make it so that the keys you press are registered as text and reflected in the active file, the mode needs to be changed from Normal to Insert mode.
This can be done by pressing the i
key when you are in Normal mode.
Once you get used to navigating with h
, j
, k
, l
keys and not left, right, up, down arrow keys, you will want to switch back and forth from Normal mode to Insert mode.
To switch back from Insert mode to Normal mode, press the Esc
key.
Switch from Normal mode to Insert mode
When you want to select some text for manipulation, Vim has a Visual mode. To activate Visual mode, you need to be in Normal mode initially.
Pressing the v
key when you are in Normal mode, Vim will switch to Visual mode. Instead, if you press Ctrl + v
key combination from Normal mode, you will be in Visual Block mode.
After you are done manipulating text and want to switch back to Normal mode, press the Esc
key.
Conclusion
This article discusses all the modes that are available in Vim and how you can switch from one mode to another.
If you are interested in learning more than just the Vim Basics, I highly recommend using this program by Jovica Ilic.