In this course, you will learn how to use Vim's Visual Mode in order to highlight text and then perform actions on it such as delete or copy, as you would when using a mouse, even when using Vim in a non-graphical environment. You will learn a range of commands for doing so, and then we will move on to an exercise in which you can try out your newly learned skills for yourself!
This course is ideal for anyone who needs to edit text files in a command-line environment.
If you want to follow along with the exercises in this course, you should have the Vim text editor installed on your computer.
If you want to follow along with the exercise(s) in this course, you can find the necessary resources here.
The goal of this exercise is to practice using Vim's Visual mode. First, start a command line session on your local machine. Next, use Vim to open the visual-practice.txt file that came in the course downloads. To do that, navigate to the location of that file. Remember, this could be different for you depending on where you extracted the contents of the file. This example assumes the course download was saved into your downloads folder, and that you extracted it from there. So go to the Downloads folder, the vimclass folder, and now open the file.
First let's use characterwise Visual mode to delete the text that reads DELETE ME, DELETE ME, YES!!! And how we can do that, is to position our cursor one space before the word delete. And we can do that with the till command, which is T uppercase D. Now we can start characterwise visual mode by typing lowercase V. Now let's visually highlight the text up until the less-than sign, and we can do this by pressing L, and using the W motion or using a forward search. I'll just use the W motion here. Be sure not to include the less-than character.
So I need to back up one space here, and then let's remove this highlighted text with the D command to delete it into the unnamed register. And let's do not include the space after the sentence in the register. So let's position our cursor anywhere in that sentence. I'm just gonna hit J a couple of times, and then I'll just hit zero to go to the very beginning of the line, which is also the beginning of that sentence.
Next we can yank it with YIS, which stands for yank inner sentence. Now we can examine the contents of the unnamed register with :reg ". These next several lines here, I wanna join them all together so that they're all on the very same line. And how we can do this is by using Linewise Visual mode, so let's start that now by typing Shift + V, which is capital V, and let's select the entire paragraph with a text object, which is IP for inner paragraph.
Now we can join all the lines by typing Shift + J, that's the capital J or join command. Now all that text is on the same line. And if you wanna verify that's on one line, you can use something like :set nu to turn on line numbering, and you can see that line five is wrapped. So I'm gonna turn numbering off real quick here with :set nu!, and the exclamation mark to toggle that option.
Now let's change the text in between all these hash symbols by centering them and making them uppercase. So let's start Linewise Visual mode with Shift + V, that's capital V, and hitting J to select the next line as well. And now we can change everything to uppercase by typing Shift + U, uppercase U. Now we can quickly reselect that text by typing GV, then colon, center, and then pressing enter.
Let's move to the next block of text here. And what we want to do is change these leading numbers that are really in string format, into just numbers like 1, 2, 3, 4, 5, 6, 7 without the leading zeros and without the quotes. So, one way we can do this is with blockwise Visual mode. So we can start that now by typing Control + V, and move over two columns by typing L twice.
Next we'll move down to the last line in this block. So I'll just hit J six times here. Now we can delete the highlighted text by either typing D or X. I'm going to use D. That text was deleted, and now we're back into normal mode. So we can just move our cursor to the right one column by pressing L, start blockwise Visual mode again by typing Control + V, move down to the bottom line here with J, and now we can do the same thing here and hit D to delete those quotation marks.
Let's use blockwise Visual mode again on this next block of text. What we wanna do here is place a pound sign followed by a space at the beginning of each of the following lines. So what we can do here is type Control + V to get into blockwise Visual mode. We'll just go down here a few lines, and then what we wanna do is enter insert mode with Shift + I, that's capital I, and we'll type the pound sign and a space and hit escape.
Let's say, what we wanna do to this next block of text, is just to append a pound sign followed by EOL, which stands for end of line, To do that, we can position our cursor on the very first line here in the column. Start blockwise Visual mode with Control + V, and we'll just hit J a few times here to get to the very bottom. Now, what we need to do is type the dollar sign command to select the text all the way to the end of the line.
Finally, just type capital A to append and we'll put a space, a pound sign, another space, EOL, and press escape. Now we've successfully appended those characters to each of those lines. If you'd like to abandon your changes and try this practice exercise again at some point in the future, you can just use :q! and press enter.
Jason is the founder of the Linux Training Academy as well as the author of "Linux for Beginners" and "Command Line Kung Fu." He has over 20 years of professional Linux experience, having worked for industry leaders such as Hewlett-Packard, Xerox, UPS, FireEye, and Amazon.com. Nothing gives him more satisfaction than knowing he has helped thousands of IT professionals level up their careers through his many books and courses.