Skip to main content

Vim Advanced

Getting Comfortable with Visual mode in Vim

There are three modes in Vim and the least popular and yet quite interesting is the Visual mode. Learn more about it.

You know Vim. It is your favorite text editor. Using Vim makes you feel at home. But, like in Home Alone, there is the basement that you have not conquered yet.

You already know about different Vim modes, right? I am going to talk specifically about the visual mode.

What is Visual mode in Vim?

In Vim, the Visual mode is used for selecting text. This is the Vim equivalent of selecting text with a mouse click-and-drag-to-select-text action in a GUI text editor. Here is a representation of selecting text using Vim's Visual mode.

Demonstration of selecting text in Vim

Extra modes under Visual mode

The Visual mode in Vim allows you to select the text, but a different subset of Visual mode will be useful depending on how you want to select the text.

There are 3 subsets of Visual mode in Vim.

  • Visual [Character] mode: Used for selecting individual characters
  • Visual Line mode: Used for selecting a whole line at once
  • Visual Block mode: Used to select text in a block-style rectangular shape

Switching to Visual mode

You obviously need to tell Vim that you intend to switch to Visual mode to use it.

We have a complete guide here on Linux Handbook that gives you a brief overview of modes in Vim and how to switch between each mode.

If you want to switch to Visual mode, you need to be in Normal mode first.

  • Visual Mode: v (lowercase 'v')
  • Visual Line mode: V (uppercase 'V')
  • Visual Block mode: Ctrl + v

Using Visual mode

When you switch to Visual mode, Vim will give you some details. It will show which mode you are in and the amount of text that you have selected.

Once you are in Visual mode, selection starts from your cursor's point, and you can move it using the navigational keys h/j/k/l.

When you are satisfied the selection, you can perform operations like delete, yank and replace.

Visual [Character] mode

As mentioned above, this is used for moving the cursor over individual characters. But it is not limited to selecting single characters. Just like Normal mode, you can jump over words with w/e/b keys and go up/down with k/j keys, respectively.

I will demonstrate selecting an individual character, selecting a few words, selecting an entire line, and then delete the selection using Visual [Character] mode.

Selecting multiple characters, words and lines in Visual mode

If you look closely, the moment I enter Visual mode, the bottom line indicates a few things to me. It says -- VISUAL -- to the left-most side to indicate that the user is in Visual [Character] mode. And, to the right side, it shows how many characters are selected.

Visual Line mode

This mode is used to select a whole line. You can not select an individual character or a word in Visual Line mode. Only complete lines are selected. The' newline' character is also selected if the line is not the bottom-most line.

Here, I will demonstrate selecting multiple lines and deleting them simultaneously. Look closely; the line that is selected last does not become an empty line (because the 'newline' character is also selected along).

Selecting multiple lines with Visual Line mode

As you can see, moving the cursor back and forth on the same line does not select text or undo the selection of text. The whole line will remain selected as long as the cursor stays on that line.

Similar to Visual [Character] mode, the bottom right corner will say -- VISUAL LINE --. But, instead of showing the number of characters, the number of selected lines will be shown in Visual Line mode.

Visual Block mode

In my opinion, this is the most interesting subset of the Visual mode. You can make a rectangular selection of rows x columns.

To show how to use Visual Block mode, let's try replacing all the addition symbols with multiplication symbols to make sense of given math equations. This should also demonstrate replacing (more like over-writing, but you get the idea) selected text with some other text (we will use the clipboard to paste text).

Selecting (and replacing) a block of text using Visual Block mode

Here, the bottom right corner will indicate that you are in Visual Block mode by displaying the text -- VISUAL BLOCK --. And instead of showing the number of characters or lines selected, it will show the dimensions of selected block using number of rows selected x number of columns selected.

Conclusion

This article covers the basics of Visual mode and it's subsets in Vim. How you can navigate and use each subset for peak efficiency.

If you are interested in learning more than just the Vim Basics, I highly recommend using this program by Jovica Ilic.

Mastering Vim Quickly - Jovica Ilic
Exiting Mastering Vim Quickly From WTF to OMG in no time
Pratham Patel
Gujarat, India