A Guide to Vim Visual Mode

Vim visual mode is a feature in Vim that makes it easier to target a specific group of code and make changes to them. Learn how to navigate it.

Written by Igor Irianto
Published on Jul. 17, 2024
Developer coding on text editor/Vim
Image: Shutterstock / Built In
Brand Studio Logo

Vim visual mode allows you to target a group of code that doesn’t follow a distinguishable pattern and edit them, making it easier than normal mode to make small changes in code.

This is important because if you’re like me and many others, your first experience with Vim is probably when you accidentally opened it and couldn't get out. You probably told yourself to never use it again.

Vim Visual Mode Explained

Vim visual mode is a version of Vim that allows you to target specific groups of text that don’t follow a pattern and make edits to them. There are three types of visual mode: line-wise, character-wise and blockwise that make it easier to make changes to code.  

Yet some people swear by Vim. You might wonder why people use it.

Well, I am one of those crazy people. The reason why it’s great — modality. Vim has seven basic modes: normal, visual, select, insert, command-line, ex and terminal-job, and seven additional modes, which I will not go over. It may seem crazy why anyone would need more than one, but this is Vim’s core feature.

To master Vim, you must master all of its modes. Let’s start with Vim visual mode.

Table Of Contents

  • What is vim visual mode?
  • How to enter visual mode
  • Vim visual mode motions
  • How to exit Vim visual mode
  • How to use Vim visual mode
  • Vim visual mode operators
  • Vim visual mode and ex commands
  • Repeating in Vim visual mode
  • Inserting multiple texts in Vim visual mode
  • Selecting the last Vim visual mode selection
  • Entering Vim visual mode from insert mode 
  • What is select mode in Vim?

 

What Is Vim Visual Mode?

If you used a different text editor/IDE, you can probably highlight a text block and apply changes to it. Vim’s visual mode works like that: You can select a text block and apply changes to it.

You can apply changes to a text object with Vim’s normal mode, but there are times when visual mode is better for the job. Suppose you have these:

const one = "ONE";
const TWO = "Two";

You want to lowercase the section starting at the “N” in "One"; and stopping before “T” in "Two";.

const one = "One";
const two = "Two";

You can’t do this easily with normal mode, because you are changing only part of line one and two, not the entire two lines. With visual mode, you can visually select “n” up until before “T”, and apply visual-mode lowercase operator (u).

Visual mode can be useful if you want to target a specific group of text that doesn't follow a distinguishable pattern.

In this case, you can in normal mode, with your cursor on “N”, do gu/Two: “apply lowercase from here until the next instance of phrase ‘Two’”. I also find it more intuitive doing this in visual mode.

More on VimNeovim Configurations for Beginners

 

How to Enter Vim Visual Mode

There are three different visual modes in Vim.

v          " character-wise visual mode
V          " line-wise visual mode
Ctrl-v     " block-wise visual mode

Character-wise visual mode is used to micro-select individual characters.

Line-wise visual mode selects lines. It is common in programming to apply changes line-wise. In the early programming days, many editors were line-based, like ed.

Blockwise visual mode allows you to select columns and rows. It gives you more freedom to move around in this mode than the first two. There are also certain things you can do only in blockwise visual mode.

On the bottom left of your Vim window, you will see either -- VISUAL --, -- VISUAL LINE -- or -- VISUAL BLOCK -- to indicate which visual mode you are in.

While you are inside a visual mode, you can switch to another visual mode by pressing either v, V, or Ctrl-v. For example, if you’re in line-wise visual mode and you want to switch to blockwise visual mode, just type Ctrl-v. Try it!

There is one more way to enter visual mode:

gv

This will start visual mode on the same selected area as the last visual mode. Suppose you just applied uppercase operation on a text block and forgot to apply another operation to the same text block. Instead of going back, reselecting, and applying operation, you can skip the first two steps if you used gv. It will make the same selection area as the last one, so you can just apply the operation you need.

Pretty neat!

 

Vim Visual Mode Motions

Once you’re in a visual mode, you can expand your selection with Vim motions, like hjkl. For more on motions, check out :h motion.txt.

While you are in visual selection, you will see your cursor on one end of the selection. It could either be on the top-left or bottom-right. You can expand your selection unidirectionally wherever your cursor is. So, if your cursor is on the bottom end, pressing j will expand selection downward, but pressing k will not expand selection upward, but contract selection upward. What if you need to expand your selection upward and downward?

You need to change our cursor location to the direction you're expanding.

You can change your cursor location with o or O while in visual selection. So, if your cursor is on the bottom of the selection and you want to expand upward, change the cursor location with o and go up with k.

 

How to Exit Vim Visual Mode

There are three ways to exit visual mode:

  • esc
  • Ctrl-c
  • Same key as your current visual mode

The first two make sense. Here’s what the last one means: If you're currently in line-wise visual mode (V), you can exit line-wise visual mode if you press V again. If you’re in character-wise visual mode, pressing v will exit visual mode. If you’re in blockwise visual mode, pressing ctrl-v will exit visual mode.

 

How to Use Vim Visual Mode

Vim Visual Mode Operators

Vim is a modal editor. It means that the same key may work differently depending on the mode. Luckily, many keys in visual mode overlap functionalities with normal mode keys. If you know your normal mode operators, you’re good. If not, don’t worry. You just need to remember the important ones; they are mnemonics-friendly.

To use visual mode operators, first visually select an area of text (v/V/Ctrl-v + motion), then press a visual-mode operator key. That’s it.

Here are some most common ones:

u        " lowercase
U        " uppercase
d        " delete
c        " change
y        " yank
>        " indent
<        " dedent

For more operators, check out :h visual-operators.

Vim Visual Mode and Ex Commands

You can apply ex commands on visually selected areas. Suppose you have this text:

const one = "one";
const two = "two";

And you want to substitute const with let only on these two lines. You can highlight both lines with line-wise visual mode (V), and perform substitution with:

:s/const/let/g

Any ex command will work with visual mode.

Repeating Vim Visual Mode

Vim's dot command (.) repeats the last change. When you repeat a visual mode operation, the same operation will be applied to the same text block.

Suppose you deleted these two lines with line-wise visual mode (Vjd).

const one = "one";
const two = "two";

The next time you use the dot command, it will also delete the next two lines.

Inserting Multiple Texts in Vim Visual Mode

You can insert multiple texts with blockwise visual mode.

Suppose you have these and you want to add a semicolon at the end of each line.

const one = "one"
const two = "two"
const three = "three"

Here’s how:

  • Start with our cursor on first column, first line (the “c” in const one).
  • Select blockwise visual mode, go down two more lines (Ctrl-v 2j). It will select all the c’s.
  • Horizontally select to the end ($).
  • Append (A).
  • Type ;.
  • Exit visual mode (esc).

Now, you’ll see ; appended to all lines. In blockwise visual mode, you can append or insert texts on multiple lines with A or I. You can also replace multiple characters with r{char}.

A way to insert characters on multiple lines using any visual mode is to use the :normal! command (:h normal). This command allows us to execute normal mode command.

Here’s how you can insert ; with character-wise visual mode:

  • select all three lines (v2j)
  • type :normal! A;

If you’re not in blockwise visual mode and want to append (A) or insert (I) texts, remember you can switch to blockwise visual mode with Ctrl-v from whatever other visual mode you are in.

Incrementing Numbers in Vim Visual Mode

You can increment columns of numbers with Ctrl-a/Ctrl-x in Vim. Here's how you can apply it to multiple lines.

Suppose you have a text:

<div id="app-1"></div>
<div id="app-1"></div>
<div id="app-1"></div>
<div id="app-1"></div>
<div id="app-1"></div>

It’s not a good practice to have multiple ids with the same name. Let’s increment them. Put your cursor on the second line, on number “1” on “app-1” text:

  • Start blockwise visual mode, go down three more lines (Ctrl-v 3j). You should be selecting all the remaining “1”’s.
  • Type g Ctrl-a.

That’s it! Now everything is incremented:

<div id="app-1"></div>
<div id="app-2"></div>
<div id="app-3"></div>
<div id="app-4"></div>
<div id="app-5"></div>

Extra: Ctrl-x/Ctrl-a can increment letters, too, with:

:set nrformats+=alpha

So if you have:

<div id="app-a"></div>
<div id="app-a"></div>
<div id="app-a"></div>
<div id="app-a"></div>
<div id="app-a"></div>

Using the same technique as above (Ctrl-v 3j g Ctrl-a) to increment the “id”’s:

<div id="app-a"></div>
<div id="app-b"></div>
<div id="app-c"></div>
<div id="app-d"></div>
<div id="app-e"></div>

Selecting the Last Vim Visual Mode Area

You learned that gv can select the last visual mode selection. But did you know that you can go to the location of the start and the end cursor of the last visual mode?

`<        " go to the last place of last visual mode selection
`>        " go to the first place of last visual mode selection

Entering Vim Visual Mode from Insert Mode

Normal mode is not the only mode you can enter visual mode from. You can enter visual mode from insert mode.

While you’re in insert mode, enter:

Ctrl-o v
  • In insert mode, you can execute a normal mode command with Ctrl-o.
  • While you are in this normal-mode-ready, run visual mode key v.
  • If you notice, on the bottom left, it will say --(insert) VISUAL--.

This works with any visual mode key: v/V/Ctrl-v.

 

What Is Select Mode in Vim?

In addition to visual mode, Vim has a select mode. Like visual mode, select mode has three modes:

gh        " character-wise select mode
gH        " line-wise select mode
gCtrl-h   " block-wise select mode

Select mode emulates regular editor’s text highlighting behavior closer than visual mode. In a regular editor, after you highlight a block of text, if you type letter “a”, it will delete and replace the highlighted block of text with letter “a”. Select mode does that. After you’ve made your selection in select mode, if you type letter “a”, it will immediately replace your selection with letter “a”.

I never use it, but it's good to know it exists.

A tutorial on Vim visual mode. | Video: Mental Outlaw

More on Software EngineeringGit Merge Theirs Explained

 

Understanding Vim Visual Mode

We’ve made it to the end. We’ve covered a lot of grounds.

We’ve learned how to enter and exit visual mode. We learned that there are three types of visual modes in Vim: character-wise, line-wise, and blockwise. We saw that visual mode shares many operators with normal mode. We learned that visual mode operation is repeatable with dot command (.). We learned to increment multiple rows of numbers/letters. We learned how to enter visual mode not only from normal mode, but also from insert mode. Lastly, we learned about select mode.

Frequently Asked Questions

There are three different ways to enter Vim visual mode depending on the task. This include:

  1. v: Entering v allows you to initiate character-wise visual mode, which is used to micro-select individual characters.
  2. V: Entering V initiates line-wise visual mode, which allows you to make line-wise changes. 
  3. Ctrl-v: This command initiates blockwise visual mode, which allows you to select “columns” and “rows” to make changes.

If you want to exit Vim visual mode, there are three commands you can use:

  1. esc
  2. Ctrl-c
  3. Entering the same key as your current visual mode.
Explore Job Matches.