Visual Mode – Part 1
Difficulty
Intermediate
Duration
29m
Students
154
Ratings
5/5
Description

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!

Intended Audience

This course is ideal for anyone who needs to edit text files in a command-line environment.

Prerequisites

If you want to follow along with the exercises in this course, you should have the Vim text editor installed on your computer.

Resources

If you want to follow along with the exercise(s) in this course, you can find the necessary resources here.

Transcript

If you're using a text editor in a graphical user environment, then you can do things like highlight texts with your mouse and then do something with that highlighted text, like delete it or copy it. If you're using VIM in a non-graphical environment then you simply don't have the option of using a mouse. However, VIM has a visual mode that behaves in a very similar way. And in many cases, it's actually more powerful than simply using a graphical application that has mouse support.

For example, you can select and operate on a vertical block of texts that spans multiple lines allowing you to make edits in the middle of many lines at once. Even if you don't plan to take advantage of some of the more advanced features, using VIM's visual mode can be a good way to select irregular text or at the very least see what portion of a document your change will affect, before actually making that change.

If you're using a graphical version of VIM, it doesn't mean you have to use the mouse. If you're going for pure efficiency, you could argue that using visual mode commands is quicker than moving your hand back and forth on the mouse and keyboard. Given that this lesson is on visual mode let's open up a file and start doing some demonstrations.

So you can see visual mode in action. First off, I have my terminal open here on my system and I'm going to navigate where I extracted the contents of the course download archive. And for me, that was in the Downloads folder. Then I'll go to the VIM class folder and now I'll open up visual.txt As always, you can follow along with me now or wait for the practice exercise that will follow.

There are three versions of visual mode. They are character-wise visual mode line-wise visual mode and block-wise visual mode. The command to start character-wise visual mode is lowercase v. Let's move over a couple of words on the first line and start visual mode. You can use two + w to move over a couple of words and then type v to enter visual mode. You'll notice the word visual in the status line at the bottom of your screen. Whatever is under the cursor, when you enter visual mode becomes highlighted.

Additionally, when you perform a motion it gets highlighted, too. So if you were to press l, now two characters are highlighted. Keep pressing l and more characters are highlighted. If you want to move by word, then just use w and the highlights follow along. After you've selected your desired texts you can perform an operation on that text, for example to delete the highlighted text, type d. Everything that was visually highlighted is now gone. Let's undo that change with u.

Let's say you want to delete everything from your current cursor position to the next occurrence of capital C in the file. To do that, start visual mode with v. Now, position your cursor on the next capital C. One way to do that is by searching, so you can use /C and press Enter. Now you can press D and the text is deleted. Let's undo that with u.

Let's start visual mode again with lowercase v. If you start to move to the right with l or any other motion for that matter but you decide you want to go the other direction. You just move to the other side of your cursor, here I'll just use h and now that gets highlighted. It doesn't keep the motion to the right. By the way, to get out of visual mode without performing any operations, press Esc, which I'll do here now.

Let's say you want to highlight on both sides of your cursor position. If you know this going in, you can position your cursor at one end or the other of the highlight. But if you don't know that going in or you change your mind, here's how to handle that. So, let's move in a few words with four + w. Now let's start visual mode with lower case v. Now move to the right, a couple of words with two + w. The o command, lowercase o moves your cursor to the opposite end of the highlight. So I'll press lowercase o now, and you can see the cursor moves back to see where we started visual mode.

Now you can move in the other direction in your highlight expanse. For example, if we use B I'll hit B here, the word before the cursor is added to the highlighted section. Want to go to the other end? No problem. Press o, and you're back at the other end. Let's say you want to expand the highlight to the right. So press L a couple of times. Now you can perform an operation on the highlighted area. Let's say you just want to save this area into the unnamed register so you just type Y. The text is yanked and you return to normal mode.

In addition to using motions to control the visually highlighted areas of texts you can use text objects. Let's position the cursor in the middle of the word character-wise by using f + e. Now let's start visual mode with lowercase v. If you want to select the inner word, use I + W. Let's escape out of that selection. Let's say you want to highlight the paragraph so you can use v to enter visual mode, A for all or A + p for paragraph. Now, let's say you want to yank this paragraph. So you just hit Y, and that paragraph is yanked.

I think it's worth mentioning here that you could have done the exact same thing with Y + a + P versus V + a + P + Y. The only advantage in this particular situation is being able to see the texture operating on before you do it. If that makes you feel comfortable, then go ahead and use visual mode to use these types of commands. If you want to shave off that one key stroke then skip visual mode in this case.

Okay. Let's move on to the next visual mode which is line-wise visual mode. First, let's position our cursor in the middle of the second line. I'll go down with j and let's do a forward search, let's say, to the l character. To start line-wise visual mode use capital V or shift + V, I'll do that now, shift + V, and now you'll notice that even though your cursor was in the middle of the line, the entire line was selected.

So the smallest selection you can make is one entire line in line-wise, visual mode. Again, you can use motions to change the highlighted area. The simplest motion would be j to move down to the next line, which makes that entire line become highlighted. Again, no matter where your cursor ends up on a line that entire line will be selected in line-wise visual mode. So if you were to use a motion, like a forward search let's do /ex, for example, and hit Enter. You'll see that the entire line would be selected.

Press n to search again, and the next line is highlighted, because that's where the cursor ends up. You can also use the o command to position your cursor to the opposite end of the visual area. I'll do that now by pressing o. Now you can expand up, by pressing k for example. I've only demonstrated the d and y commands, but you can use many others like capital U to change everything to uppercase. I'll do that now, shift + U and everything is an uppercase.

Okay, I'm going to hit lowercase u to undo that operation. By the way, you can use g + v to start visual mode with the same area selected, as the last time you were in visual mode and it will also put you in the same type of visual mode. So if you were to type g + v now you'll see the exact same lines get highlighted again and you're in line-wise, visual mode. If you want to make this entire selection lowercase, use the lowercase u command.

Now that we're back into normal mode we can undo this with lowercase u as well. Just going to move down the file a bit here, and press Z + Enter to shift the text up in our view on the screen, Here, you'll see a list of some of the commands you can use with visual mode. You can switch case, change, delete, replace, insert, append, join and more. I'm going to use this list to demonstrate visual block mode.

Let's put our cursor at the beginning of the line, that starts with a tilde. Just move down one line for me here and to start block-wise visual mode, we type control + v. At the bottom of your screen you'll see visual block indicating the mode. Some people like to think of this as vertical visual mode, and you'll soon see why. Move your cursor to the right, just before the word switch.

So I'll just move over a couple of characters here. Now let's move our cursor down with j. You'll see a block selected. If you were to make that motion in the character-wise visual mode, all texts on the first line would have been selected and all the texts up to and including the cursor position on the next line would have been selected as well. But with visual block mode you can make vertical selections.

Let's say you want to select this same width for this entire chunk of text. There are a few ways to do this. Let's use a forward search to the next capital S, so we can use /S. That gets you most of the way there. Now let's move back one space with h and down one line with j and now we've got the entire desired area selected. If you want to remove that block, type d and you can see everything slide over. Now let's undo that with u.

Let's re-select that same bit of texts with g + v. If you use the o command the cursor is positioned at the other end of the highlight. So if we do that a few times just hit o here back and forth and back and forth. You'll see that the cursor jumps back and forth. In visual block mode you can use capital O, which moves the cursor to the other corner on the same line. So if we type capital O here a few times, it just moves the cursor from one end of the highlighted line to the other. Press lowercase o, and you're positioned at the opposite vertical end of the highlighted area.

Now you can use capital O to switch back and forth on the same line. Just like in the other modes, you can use this method to expand the selected area. So if you want it to extend the selection to the right just move to the right. So I'll go here and then hit l a couple of times to move to the right. We're not going to do anything with this selected area, so we'll just hit escape to exit out of visual mode and return to normal mode. 

I'm going to move to the next block of text here. I'm going to do a forward search to USA, z + Enter to move the display up on our screen here. Now let's do something very similar with this group of text. Let's say you want to change lowercase usa to uppercase USA. Move your cursor to the first usa, which I have done here and start block-wise visual mode with control + v. Now, move to the right, and then I'll just move down until I have the entire block selected.

Now I'll just use the tilde command which is the switch case command. And that leaves us with all upper case U S A's on each line. Now let's move to the next section. I'll just do /line, press Enter + z + n + Enter. Let's say you want to append some texts to every line. Start out by entering block-wise visual mode with control + v.

Let's go to the bottom line here, with a forward search to nine. If you start moving to the right with l eventually you'll hit the end of the current line but the text on the longer lines isn't selected. If you move up with k and then go back to the end of this line with l you get close, but it's still not there. Now let's move back down to the bottom line with j and to select all the way to the end of every line, Use the dollar sign motion. So I'll hit Shift + four on my keyboard. Now you can do something to this entire block.

To append, type capital A. I'll do that now, Shift + A. By the way, lowercase a doesn't work in visual mode, only capital A. After you type Shift + A, you're placed into insert mode, as you can see at the bottom of your screen. Now you just type the desired text, you want to append to every line, let's type end, for example. You only see the text appear on that one line you're on, but when you hit Esc like I'm going to do now, that same bit of text is appended to all the highlighted lines.

Now let's move down to the next block of text here. By the way, you can insert something on all the lines somewhere in the middle, too. Let's say you want to insert the @ symbol, before each of the variables in the next section of text. First, you would position your cursor at the beginning of the first variable with two /nick, press Enter. Notice how I use a count on the forward search. So now you can start block-wise visual mode with Control + v and then select the other lines with three + j, for example.

Now type capital I to start insert mode. I'll do that here, Shift + I. Lowercase i doesn't work in visual mode. You have to use uppercase I. Now let's type the @ symbol, which is Shift + two. And then when you press Esc, the @ symbol is inserted on all the lines that were highlighted. Let's keep working our way down through this file.

Let's say you want to change the word John on each line to read Billy. One way you can do this is go into visual block mode with control + v, select all the Johns, let's go forward search in here, and then three + j. Now type c to change the text. You'll see that all the text is deleted and you're placed into insert mode. Like you've done before, just type your desired text here. You're going to use Billy and then press Esc. The replacement is made on all the previously selected lines.

About the Author
Students
21178
Courses
61
Learning Paths
18

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.