Photo by
Alex Knight
on
Unsplash |
Once you installed Vim, learned the basics of Vim Modes and understood Normal mode, it's time to learn yet another Vim mode: Visual Mode. This is the mode where you insert/modify text.
On this tutorial we will understand more about it and how to use it effectively.
Visual Mode 101
The Visual mode will probably be familiar to you. 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.
Vim allows any operation on visual blocks including copying, deleting, replacing, changing case, etc.
Visual Mode types
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
Once the visual mode is started with one of the options above, move the cursor to the desired end selection point. You will see the text selection being highlighted. Let's see them in action.
Visual Mode per character
Start the visual mode with character with v then move your cursor around to keep expanding your selection. For example, if from the start of the file you typed v4w, you would have:
Visual selection after pressing v4w from the top of the file |
Where:
- v - enters visual mode per character
- 4w - moves 4 words ahead
Visual Mode linewise
The difference between the visual mode linewise and the previous one is that this selects line by line (but is not limited to only lines). Start the visual mode linewise with V then move your cursor around to keep expanding your selection. For example, if with my cursor located on the top of the file I pressed V2}, I'd have:
Visual selection after pressing V2} from the top of the file |
- V - enters visual mode linewise
- 2} - 2 paragraphs forward
Blockwise Visual Mode
The Blockwise visual mode allows us to perform vertical selections. For example, if you pressed Ctrl+V4j$ from the quae word, you'd have:
Where:
- Ctrl+V - enters blockwise visual mode
- 4j - moves 4 lines down
- $ - moves to the end of the line
Selecting with search
Fine-tuning the selection
- o - go to the other end of the highlighted text
- O - similar to o but in visual block mode, moves to the other corner in the same line
Performing Operations
With the selection made, press <Enter> to set it so you can run interesting commands like:
- 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:
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 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.