The VISUAL ModeIn the first part of my small series "I learn Vim new and more detailed" I had explained the basic features of Vim and the simple way to enter and edit text. This is basically enough for the first steps in Vim.
But if the editing of text should be a bit bigger and not limited to the single deletion of characters, the VISUAL mode comes into play. The VISUAL mode serves thus purely the modification of text. Starting from NORMAL Mode (which we always enter by pressing "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 either deleted, copied, changed, or otherwise modified with any Vim edit command.
Highlight and edit text. In some cases (e.g.: when writing articles for the Nerdbude), an entire sentence needs to be moved to a different location in the document.
To do this, navigate the cursor to the first letter in the sentence that needs to be moved and press "v" to activate VISUAL Mode.
After that we have the following options for text marking:
H/L - mark Characters w - mark until beginning of the next word $ - mark until the lineend
Now that the text we want to modify is highlighted we continue to modify:
d - delete p - paste y - yank / copy u - undo
Change phrasesIf you want to change a whole phrase in a text, it is just as easy in VISUAL mode.
Again with the cursor at the beginning of the phrase to be changed and with "v" in the VISUAL mode. Then again as described above, select the phrase that is to be changed and replace with the following keys:
c - change
After that, the selected text will disappear and you will be taken directly to INSERT mode and you can change the text.
VISUAL LINE MODE (VLM)VCM lets me work "character by character". So if we are sure that the individual words and characters are correct, but the line is wrong (happens especially when coding often)? Here the VISUAL LINE MODE takes effect.
With the VLM can be marked and modified over several lines and that fortunately with the same commands as in the VCM.
So we navigate again to the beginning of the lines that should be marked. Then we switch to the VLM with "SHIFT + V".
After that there are the usual commands like:
d - delete p - paste y - yank u - undo
Especially when writing code, we want to have indented text. So navigate to the beginning of the text again, switch to VLM with "SHIFT + v" and mark the code block. After that the whole paragraph can be indented:
> - moves the selected to the right < - moves the selected to the left
VISUAL BLOCK MODE (VBM)The VISUAL BLOCK MODE is structured somewhat differently. Here it is not operated on character or line level but in columns. This can be quite helpful with CSV files or with YAML files.
So you can delete or indent columns without having to go through line by line. This also works almost identically to VCM or VLM.
We navigate to the beginning of the block to be edited, switch to VISUAL BLOCK MODE with "CTRL + v" and select the block. Here, the block is automatically selected downwards, i.e. column by column, not horizontally in the line.
The modifications we can now make are known:
d - delete u - undo > - moves the selected to the right < - moves the selected to the left
Ta-da! The VISUAL MODE. Practical and easier to use than you think. I'll save the practice this time, because it would simply be the same as everything I've written here ;)