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
- 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
- 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
- Ctrl+V - enters visual block mode
- 5j - selects the next 5 lines
- ~ - changes the case (making i -> I)
Example 3 - Replace text
- 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:
To learn more about blockwise operators, run:
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
- Switching modes in Vim
- Vim - Normal Mode
- Vim - Insert Mode
- Vim - Visual Mode
- Vim - Blockwise Visual Mode (aka vertical selection)
- Vim - Replace Mode
- Vim - Command-Line Mode
- Vim - Select Mode
- Vim - Ex Mode
- Why use Vim
- How to get started with Vim
- Learn to learn Vim
- Vimtutor - Your first Vim tutorial
- Vim Modes - All you need to know
- How to install Vim