- What are Vim Modes? How to Change Them?
- Modes in Vim
- Normal mode
- Insert mode
- Visual mode
- Changing modes in Vim
- From Normal mode to Insert mode, and back
- Switch from Normal mode to Insert mode
- Conclusion
- VIM Modes and how to change the mode
- Vim Modes
- Normal mode
- Insert mode
- Visual mode
- Changing the modes
- Basic commands
- File related commands
- Moving the cursor
- Inserting Text
- Changing text
- Copying pasting
- Deleting Text
- Undo/Redo
- About the author
- Karim Buzdar
What are Vim Modes? How to Change Them?
Vim has three different modes for handling user input and processing them.
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.
You can not switch between Insert and Visual mode without switching to Normal mode, so that is not covered.
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.
VIM Modes and how to change the mode
Vim is the most popular and extremely powerful text editor. It possesses a lot of features that you would not expect to have in a text editor. Most of these features make it much easier for you to do a lot of work in a little time. The Vim editor is a modal text editor; it uses modes for different purposes like inserting text, running commands, and selecting text. Modes basically decide whether pressing any key on the keyboard will insert those characters or move the cursor through the document. Therefore, it is important to know what each mode is and how to change the modes.
This article will describe what Vim modes are and how to change them. Vim is a free and open-source text editor that comes installed by default with most of the operating systems.
Please note that all the commands and processes discussed in this article have been tested on the Ubuntu 20.04 LTS (Focal Fossa).
Vim Modes
In Vim, there are three modes of operation: Normal, Insert, and Visual.
Normal mode
Normal mode is the initial mode of the Vim editor. When you open a new file edit an existing one, it starts in normal mode by default. In normal mode, you cannot insert any character. Normal mode is also known as command mode because all the keystrokes you perform are interpreted as commands. For instance, if you press k, it will move the cursor position up one line instead of inserting the character “k”. Similarly, if you press yy, it will copy the current line instead of inserting “yy”. Also, in normal mode, the uppercase and lowercase letters are treated differently. For instance, pressing o create a new line for the text below the current cursor location, while pressing O creates a new line for text above the current cursor location
To access normal mode from other modes, press Esc key.
Insert mode
Insert mode is where you can insert your text in the file. This mode inserts every character you type at the current cursor location.
Visual mode
Visual mode allows you to select text so that you may perform certain operations (cut, copy, delete) on it.
Changing the modes
As already discussed, when you create or open a file in vim, it first opens in Normal mode.
In order to type any character, you will need to switch to the Insert mode. There are different commands to enter into Insert mode from Normal mode that are i, I, o, O, a, and A. The most commonly used command to enter in to insert mode is “i”. To shift back to normal mode, press Esc.
To switch to the visual mode from Normal mode, different commands are v, V, Shift + v, and Ctrl + v. The most commonly used command to enter in to insert mode is “v”.
To switch to the visual mode from Insert mode, first shift to Normal mode by pressing the Esc, then press v to get into the Visual mode.
Basic commands
Following are some basic commands that can be used for inserting and manipulating text in Vim:
File related commands
:w | write the file to the disk |
:q | quit vi without saving the file |
:wq | write the file to disk and quit vi |
:q! | Ignore the warning and discard the change |
:w filename | Save the file as filename |
Moving the cursor
j | move the cursor down one line |
k | move the cursor position up one line |
l | move the cursor to the bottom of the screen |
0 | move to the beginning of the line |
$ | move to the end of the line |
Inserting Text
I | insert text at the beginning of the line |
i | insert text before the current cursor location |
a | insert text after the current cursor location |
o | Create a new line for the text below the current cursor location |
O | Create a new line for text above the current cursor location |
Changing text
cc | Remove the whole line and start Insert mode. |
s | Remove the character under the cursor and start Insert mode. |
r | Replace the character under the cursor |
Copying pasting
y | Copy the selected text to clipboard |
yy | Copy current line |
P | insert the text “before” the cursor, |
p | Insert the text at the point after the cursor |
Deleting Text
X | delete the character before the current location |
x | delete the character under the current location |
D | Cut to the end of line |
dd | Cut current line |
Undo/Redo
The text editor should be optimized for editing, not just writing, and Vim is one of them. It has separate modes for editing, inserting, and selecting text. In this article, you have learned about vim Normal, Insert, and Visual mode and also how to switch between different modes. I hope you liked the article!
About the author
Karim Buzdar
Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.