vim

Vim Visual Block Mode

Vim is a very pleasing and aesthetic editor found in the Linux system. It can be used to make your text more appealing and graceful using some of its functionalities. Other than that, Vim came up with more than one mode to perform specific actions in it. These modes contain the Insert mode, normal mode, visual mode, and visual block mode. Today, we will be discussing the Visual Block mode of Vim in this article, along with other modes. You have to make sure that your Linux system has already configured the Vim editor in it. If not, make use of the “apt” command to do so. After that, try opening the console application with Ctrl+Alt+T. Let’s begin with the opening of a new text file named new.txt within our Vim editor. So, try using the “vim” command followed by the name of a file to do so. The file “new.txt” will be opened within the Normal mode of Vim editor at first.

$ vim new.txt

Visual Block Mode:

Visual Block Mode of Vim allows us to perform different operations like delete, copy-paste, i.e., yanked and put, etc. To enable the Visual block mode in Vim, you have to try out the “Ctrl+V” command within the normal mode. You can see that the new.txt file has been opened in the Visual Block mode. The file contains only two lines that will be edited further.

Let’s start editing the new.txt file within the Visual Block Mode. So, we will be performing the delete operation first. So, make sure your cursor is at the 1st character of the 1st line. Start pressing the “next” key from the keyboard to highlight the text in the horizontal direction. You can press “j” to move downward and press “k” to move in the upward direction. So, we pressed “j” to move 1 line down to highlight the other line and pressed “d” from the keyboard.

In return, all the text that has been highlighted in the Visual mode would be erased as shown.

Press “u” to undo this process and get the same old text on your Vim file as below.

This was all about the delete command in the Vim editor. Now, we will be seeing how the yank command works in the Vim editor to copy the text data from any file. So, we have added three lines of text using the Insert mode and placing the cursor. Come back to the Visual mode once again and try highlighting or selecting the text you want to copy using the yank command. Thus we have highlighted the first 2 words of each line using the “next” and “j” keys from the keyboard. Now, press “y” to copy the highlighted text.

After being yanked, the Vim editor also shows that the 3 line block of text has been copied or yanked. Now, scroll down to the 4th line of the text file using the Insert mode and place the cursor at its first character. Press the “p” command to put the copied text in it.

The yanked text will be displayed or pasted to the last 3 lines in the file as shown.

Let’s try to delete the text data from somewhere between the files. So, within the Visual mode, we have started to highlight as the cursor was placed at that point using the Insert mode.

The particular highlighted text has been deleted using the “d” command followed by the Enter key. The output for this delete command is shown below.

Let’s add some indentation before each line of text within the Vim new.txt file as shown in the Visual Block mode of Vim. The cursor has been placed at the 3rd line.

Now press the back “arrow” key from the keyboard to go back to the 1st location of line 3. With this, press the key “k” to go in the upward direction. The section will be highlighted. Use the “d” command to delete this extra indentation.

The output would be something like revealed in the image below.

Let’s use the indentation once again. Highlight the indentation found at the start of each line using the “next” and “j” keys from the keyboard up to your need. The indentation area has been highlighted, as shown in the Visual Block. Press the yank command “y” to copy this indentation.

After copying, Vim will show us the action performed to copy the block of 3 text lines.

Now, move towards anywhere in the text file to paste the copied indentation. So, we have chosen the middle place of the text lines. The highlighted area shows that the copied text or anything will be posted here at the highlighted area. Press the “p” command from the keyboard to replace the highlighted text with the indentation.

You will see that the text highlighted in the above image will be replaced with the empty spaces as copied before using the “yank” command.

Within the normal mode, press “v” to open it quickly. You will see the word “Visual” after opening the file in it, as shown below. This mode is also called the Character Visual mode of Vim. You can also perform the yank, delete and put functions here. Within this mode, move the cursor to highlight the text, as shown below.

To get into the Vim Visual Line mode, press “Shift+V” while you are in Vim’s Normal mode.

Conclusion:

This guide explains using the Vim Visual Block mode in the shell of the Ubuntu 20.04 system. We have covered different commands to copy, paste, delete the text, and ways to move upward, backward, downward, and forward within Vim. We have also comprehended some other modes as well. We hope this article will be helpful to every new and expert user of the Vim and Ubuntu 20.04 system.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.