Check out a free preview of the full Introduction to Bash, VIM & Regex course:
The "Visual Select" Lesson is part of the full, Introduction to Bash, VIM & Regex course featured in this preview video. Here's what you'd learn in this lesson:

James introduces visual mode, which is a flexible and easy mode to select a piece of text for an operator.

Get Unlimited Access Now

Transcript from the "Visual Select" Lesson

[00:00:00]
>> James Halliday: Visual select is a fantastic feature that I use all of the time. It's really useful when you're coding because you can do things like indent and unindent whole blocks of code. So why don't I open a file on some programming?
>> James Halliday: Let me just pick one.
>> James Halliday: Okay, here's some source code.

[00:00:26] So to go into visual mode, there's a few ways of doing it. There's a, if you just hit v from command mode,
>> James Halliday: Then vim prints out VISUAL at the bottom, and you're in character select mode. So you can move around with j and k and l and h and select something.

[00:00:48] When you're in visual mode as well, you can also use all of the ways of skipping around. So 0, $ forward slash to a pattern, so I can skip ahead to function by doing /function, and now I'm skipped ahead to there. You can also do things like if I want to delete everything that is currently matched in visual mode, I can hit x or d, and it all goes away.

[00:01:12] I can undo that. There's also visual line select mode, which I think is even more useful for our coding. So you do that by typing capital V from command mode. So when you're in visual line mode, it prints out VISUAL LINE at the bottom of the screen. Now you can move around with j, and k.

[00:01:34] You can also search for things but you can't move around with h and l because it's only selecting individual lines. So really handy thing when you're in this mode is for example, if I want to indent a whole section of code, I can visually select something. And maybe I'll search for a curly brace at the beginning of the line, which I did by typing slash, caret, close curly brace, and then I can go up a character.

[00:02:06] And there's built-in functionality for doing indents and unindent. So to indent this whole block of code that I've selected, I can do greater than. So if you type greater than, it all gets indented. You can also push dot to indent again and again and again and you can hit u to undo that.

[00:02:28] You can also unindent code by doing less than. So if I select the same thing, or I can actually hit colon and do the up arrow. Or I think, no, I can't do that. But if I do that and I do less than, then it gets unindented. So here's all of that code flush with the left margin.

[00:02:50] You can also if you have line selected something, do a replacement just on this block of code. So suppose I wanna replace, this code has four spaces. Maybe I want it to be two spaces. So I can do a little regular expression now by typing colon s/, and then I can hit, I can either hit the space bar four times.

[00:03:14] Or I can probably do that and then replace it with two-
>> Speaker 2: Those carets there, did you want that there?
>> James Halliday: Yeah, those are put there by vim when you're in line select mode. That's just the way that it's done. All right, so I guess vim doesn't like the quantifiers.

[00:03:36] Probably the engine doesn't support it. So if you hit space bar four times,
>> James Halliday: Just as well. Okay, so now it's indented with two spaces. You could also do the same thing to add or remove semicolons, all kinds of stuff.