Showing posts with label Visual Block Mode. Show all posts
Showing posts with label Visual Block Mode. Show all posts

Tuesday, February 2, 2021

Vim - Blockwise Visual Mode (aka vertical selection)

When learning Vim, it is important to learn the Blockwise Visual Mode where you can work with vertical selections
Photo by Alex Knight on Unsplash

Once you installed Vim, learned the basics of Vim Modes and understood the Visual mode it's time to investigate further the Blockwise Visual Mode, one of the sub-modes of Vim that allows you to perform actions on blocks of texts, including vertical blocks.

Visual Mode Recap

But before getting to the Blockwise Visual Mode, let's quickly recap yet again the Visual mode which should probably be familiar to you at this point. It's the mode in which Vim highlights the selected text so you can run operations on it. It's equivalent to how you select text in other text editors using the mouse or the keyboard. 

There are three visual modes in Vim:

  • Visual Mode per character (v) - sets a starting selection point and keep altering the selection while you move the cursor
  • Visual Mode linewise (V) - selects line by line
  • Blockwise Visual mode (Ctrl+V) - allows vertical selections

Today we will focus on the Blockwise Visual Mode and learn how it can be used to solve problems efficiently.

Blockwise Operators

The Blockwise Visual Mode contains its own blockwise operators:
  • I - inserts at the start of the block on every selected line
  • A - appends to the end of the block on every selected line
  • c - change the selection
  • r - replace each word of the selection by the supplied character

Example 1 - Inserting text

So let's see an example. Imagine that we have this piece of Markdown and we want to make the lines 2 - 6 a markdown list by prefixing it with "- ".
Our list before: no - in the beginning of the line
By pressing Ctrl+V}I- we'd end up with:
Our list after: we added - to the beginning of the line
Where:
  • Ctrl+V - enters visual block mode
  • } - selects until the end of the paragraph
  • I- - inserts "- " at the beginning of the block 

Example 2 - Uppercase item

Let's continue our example by setting making item uppercased. First, we select our column by positioning our cursor in the first i and press Ctrl+V5j to select vertically:
Then we press ~ to change the case of each character in the selection:
Where:
  • Ctrl+V - enters visual block mode
  • 5j - selects the next 5 lines
  • - changes the case (making i -> I)
We could have achieved the same results above using the replace tool. In the end, use whatever makes sense for your use case.

Example 3 - Replace text

Let's continue our example by replacing item by word. First we select item with Ctrl+Ve5j:
Then we replace it with cvim tip, ending up with:
Where:
  • c - changes the selection
  • vim tip - is the text we wanted the target to be

Other Operations

Apart from the previous examples, here are other interesting commands to run on your selections:

  • y - to yank (copy) the text
  • ~ - change case of the selection
  • d - delete the selection
  • U - to uppercase
  • u - to lowercase
  • gg - format lines
  • J - to join all lines
  • : - to run any command on the selection
  • r - to replace any character in the selection by another
  • > - to indent the selection
  • < - to un-indent the selection

Getting to Normal Mode

To get back to Normal mode from Visual mode (or one of Vim's other modes), press <Esc> or <Ctrl-C> or <Ctrl-[>.

Learning More

Ready to learn more about the Visual mode? Open its dedicated manual with:

:h visual-mode

To learn more about blockwise operators, run:

:h blockwise-operators

Mode-specific help

If you want to know more about specific keys, Vim also has an intelligent mechanism to get you to the help quickly. It follows this pattern:

What Prepend Example
Normal mode command :help x
Visual mode command v_ :help v_u
Insert mode command i_ :help i_<Esc>
Command-line command : :help :quit
Command-line editing c_ :help c_<del>
Vim command argument - :help -r
Option ' :help 'textwidth'
Regular expression / :help /[

We hope you get used to the above syntax and use it regularly in you your Vim journey. It will not only help you learn more about Vim but also to memorize the commands better.

Conclusion

On this post we continued in our Vim journey by learning a bit more about Vim's Visual Block mode. If it seems complicated, don't be concerned. It takes years to master Vim but be sure that the more you learn, the more you realize that time is that secret ingredient in getting comfortable, becoming proficient and efficient with Vim.

Learning Vim is like learning a musical instrument. It takes time, effort and discipline but once you master it, the gains are endless. You definitely won't regret.

See Also

Any comment about this page? Please contact us on Twitter

Featured Article

Vim - Ex Mode

When learning Vim, it's important to understand its modes, including the  Ex Mode  where you can continually type your commands u...

Popular Posts