In the
first part of my little series "I'm learning Vim again and in more detail" I explained the basics of Vim and the simple way of entering and editing text. In principle, that is completely sufficient for the first steps in Vim.
If you want to edit text on a larger scale and not just delete individual characters, VISUAL mode comes into play.
VISUAL mode is therefore purely for modifying text.
Starting from NORMAL mode (which we always get to using "ESC"), there are three ways to switch to VISUAL mode:
v - starts VISUAL in character mode
V - starts VISUAL in line mode
CTRL + V - starts VISUAL in block mode
VISUAL CHARACTER MODE (VCM)
The VCM can highlight either a sentence in a paragraph or a phrase in a sentence. The highlighted text can then be deleted, copied, changed or otherwise modified using any Vim editing command.
Highlight and edit text
In some cases (e.g. when writing an article for Nerdbude) a whole sentence needs to be moved to another place in the document.
To do this, navigate the cursor to the first letter in the sentence that needs to be moved and activate VISUAL mode with "v".
We then have the following options for text highlighting:
H/L - select text character by character
w - selects up to the beginning of the next word (word)
$ - selects up to the end of the line
Now that the text we want to modify has been marked, we can continue with the modification:
d - delete
p - paste text from the clipboard
y - copy what is marked (yank)
u - undo the last action
Change phrase
If you want to change an entire phrase in a text, you can do it just as easily in VISUAL mode.
Again, move the cursor to the beginning of the phrase you want to change and press "v" to go into VISUAL mode. Then, as described above, mark the phrase you want to change and replace it with the following keys:
The marked text then disappears and you are taken directly to INSERT mode and can change the text.
VISUAL LINE MODE (VLM)
In the VCM you can work "character-accurately". So what if we are sure that the individual words and characters are correct, but the line is wrong (happens a lot when coding)? This is where the VISUAL LINE MODE comes in.
With the VLM you can mark and modify several lines and fortunately with the same commands as in the VCM.
So we navigate back to the beginning of the lines that are to be marked. Then we switch to the VLM with "SHIFT + V".
After that there are the usual commands like:
d - deletes the lines (delete)
p - pastes text from the clipboard (paste)
y - copies the selected (yank)
u - undoes the last action (undo)
When writing code, we naturally want to have indented text. So navigate back to the beginning of the text, switch to the VLM with "SHIFT + v" and mark the code block. Then the whole paragraph can be indented:
> - moves the selected item to the right
< - moves the marked one to the left
VISUAL BLOCK MODE (VBM)
The VISUAL BLOCK MODE is structured a little differently. Here, operations are not carried out at character or line level, but in columns. This can be very helpful with CSV files or YAML files.
This allows columns to be deleted or indented without having to go through line by line. This also works almost identically to VCM or VLM.
We navigate back to the beginning of the block to be edited, switch to VISUAL BLOCK MODE with "CTRL + v" and mark the block. Here, the marking is automatically downwards, i.e. column by column, not horizontally in the line.
The modifications that we can now make are known:
d - deletes the lines (delete)
u - undoes the last action (undo)
> - moves the selected item to the right
< - moves the selected item to the left
Tadaa! The VISUAL MODE. Practical and easier to use than you think.
I'll spare you the practical part this time, as it would simply be the same as everything I've written here ;)