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.
In addition to selecting texts with motions you can use text objects too. For example, the next section of text is a snippet from a shell script. I'll move down here. Let's say you want to indent all the lines inside the function so that it's easier to read. First position your cursor somewhere in between the curly braces. For example, you could just jump to the first pound sign with a forward slash pound here, press Enter.
Now let's start line-wise visual mode with Shift + V that's capital V. Now select all the lines within curly braces with a text object like eye closing curly brace or you could have used eye opening curly brace. To shift all the text over to the right, type the greater than symbol, that Shift + period on my keyboard. And now everything is indented.
By the way, if you wanted to shift one line without using visual mode, you can use greater than greater than. Let's do that here greater than greater than and that just moves that one line that you're on over. Now let's undo that with U.
Also, just to be thorough, you could accomplish this same thing without using visual mode at all. For example, you could use greater than followed by a text object to indent that text. So I hit greater than, I and then either an opening or closing curly brace. Let's say you want to shift everything to the left to quickly reselect the last visual area use GV. Now type the less than symbol and everything shifts to the left. We'll do that again, GV and we'll pull it back again to the left.
Before we finished with visual mode, I wanna take just a minute to talk about indentation and tabs. It's not as exciting as visual mode but since we're shifting around texts now's a good time to talk about these fine spacing details. When you perform one of these shift operations it shifts texts by the shift width setting. To view the value of the shift width setting, you can use colon set shift, with, question mark and press Enter.
As you can see here, it's set to eight. So each time you perform a shift operation the text gets shifted by eight characters. Also by default the tab stop setting is set to eight as well. And we can confirm this with set tab stop question mark and pressing Enter. The tab stop setting is the number of spaces that a tab character in a file accounts for. Set another way tab stop is the width of a tab.
Another setting that comes into play when working with tabs is the expand tab setting by default it's disabled. What you can see here with colon set, expand, tab question mark and hit Enter. It displays no expand tab because it is disabled. When it's enabled it inserts the appropriate number of spaces instead of an actual tab character. So given that the shift width is eight and tab stops are eight and expand tab is off, that means when you perform a right shift operation a tab gets inserted. If you wanna make tabs easy to spot turn on list mode with colon set, list, and press Enter.
Let's quickly indent this block of text. So I'll do GV and then greater than sign here to shift everything over. Instead of seeing the space as a tab character represents when you have lists turned on you see the actual tab character which is represented by curet capital I. Also you'll notice dollar signs at the end of each line. This helps you spot any extraneous white space at the end of lines.
Let's toggle list a mode off with set list exclamation mark. Let's quickly push everything back to the left. So we'll re select the previously selected area with GV shifted to the left with the less than symbol. Now let's set the shift width to four with colon set shift width equals four and press Enter. Now let's shift the text over with GV greater than.
If you turn on list mode with set list you'll notice that there are no tabs just spaces at the beginning of the shifted lines. If you perform the shift once again with GV greater than now you'll see the tab character shows up because you shifted over to a multiple of the tab stop width and expand tabs is turned off. If you're like me, you're not a big fan of tabs especially tabs mixed with spaces throughout a file. So now turn on expand tab with colon set expand tab and press Enter.
Now let's perform a couple of shifts with GV greater than. That moves the block over four spaces and another GV greater than also moves the block over another four spaces. So now each line in the block starts with a series of spaces and not tab characters. If you wanna save your settings like turning on the expand tab option and setting shift width to four add those set statements in your VMRC file. Vim settings in the VMRC file are covered in another lesson in this course. So refer there for more detailed information.
So that's how you use visual mode in combination with operators like delete, change, yank, shift, and so on. So now let's look at how you can limit the range of command, mode commands on a selected region of text. First, I'm gonna toggle off the list option with set list, exclamation mark.
Now I'm gonna move to the next section of the file here. Let's say you have a file where you want to make a substitution to only part of that file. In this example, you can see the text, United States of America appears on each line. Let's say you want to change that text to just USA but only on the lines that include States and their capitals. Start out by positioning your cursor on the first line you wanna change. So you can just use J in this example.
Now start line-wise visual mode with capital V and select to the last line. One way to quickly do that for this set of data is to use forward slash capital G and press Enter. Now let's start command mode with a colon. You'll see a bit of text that is automatically inserted, that text is a range which represents the visually selected area. The single quote less than sign is the start of the visual area and the single quote greater than sign is the end of the visual area. Like any other range, the start and end points are separated by commas.
Now you can perform your command on that range. So you can type, for example, the substitute command which is S forward slash United States of America. And we wanna replace that with USA forward slash and hit Enter. At the time of this recording command mode commands affect entire lines. So if you were to use character-wise visual mode commands would affect an entire line. Even if the line was partially visually selected. This may change in future versions of Vim per the note in Vim's own built in documentation. But again, as of now, this is how it works.
Let's work on another quick example of using visual mode with commands. Let's say you want to center the text between the lines of pound signs. First, you would position your cursor on the line that starts with the episode. One way you can do this is just a forward search with forward slash capital EP and press Enter. Now I'm just gonna hit Z Enter so we can easily see this. Now select the lines by starting visual mode. Let's use line-wise visual mode with capital V moved down to the next line with J and select that line as well.
Now let's enter command mode with a colon and you'll see the visual range already put in place for you on the command line. Now you can use the center command I'll just type that here center and press Enter. The center command uses a default of 80 columns to center with. The lines of pound signs are 80 characters long. If you wanted to send her the selection with a width of 40 just supply that after the center command.
So let's do this with GV to reselect the lines colon to go back into command mode, now type center and space 40 and press Enter. Now, the text is centered within 40 columns. By the way the shortened version of the center command is CE. So let's recenter it with GV colon CE Enter. You can also left the line text with a left command. Let's do that with GV colon left and press Enter. The shorthand version of left is LE also you can supply a column to left align too.
So let's say you want to position the text over five spaces so we can just use GV to re-select the area colon for command mode, LE for the left command, five for how far we wanna go over to the left and press Enter. You can use the right command to write a line lines. So type GV colon, right, and press Enter and that makes everything right aligned to the eighth column. You can override that by supplying a column position after the right command. Also, our eye is the shorthand version of the right command. Let's right justify everything to the 60th column with GV colon RI six zero Enter.
Let's say we want to position this text in the middle of the screen, and also start each line with pound signs. Pound signs are commonly used as comment markers for many programming languages. So you can use this technique to create headers in your source code.
First, let's send center the texts with GV colon CE and Enter. Now let's position the cursor all the way to the beginning of the line with zero. Now start block-wise visual mode with Ctrl + V, moved down with J and replace the characters with our pound sign. Now the main text is centered while the line start with a pound sign without repositioning the text in the middle.
Well that's visual mode. Let's quickly recap what you learned. First off, you learned about the three types of visual modes, which are character-wise, line-wise, and block-wise visual mode. You learned how to start character-wise visual mode with lowercase V, line-wise visual mode with uppercase V, and block-wise visual mode with Ctrl + V. You then learned how to use motions in text objects to expand the visual area. You also learned how to expand the visually highlighted area by using lowercase and uppercase O commands.
From there, you performed a various commands on the visually highlighted areas such as changing, deleting, yanking, upending, replacing, and shifting. You then went on to use commands that use the visual area as a range. You performed substitutions with the S command and aligned text with the center right and left commands.
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.