Check out a free preview of the full Introduction to Bash, VIM & Regex course
The "Modes and Moving Around" 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 demonstrates how to move within a document while editing within the Vim editor.
Transcript from the "Modes and Moving Around" Lesson
[00:00:00]
>> James Halliday: Things like insert mode and command mode are known as modes. There are few more modes. There's visual mode. There's more, but they're kind of,
>> James Halliday: Like I've never needed to use any of the other ones. So I don't know. That might be it, but who knows? Vim is one of those really, really deep topics where nobody ever really knows the whole thing.
[00:00:27]
But you can get by with knowing enough of it, I guess,
>> James Halliday: As you're moving around in the text file. So you can use the arrow keys and that happens to work. Although if you're in an old school VI, then arrow keys probably will not work. It'll print like some sequence.
[00:00:59]
So what you have to do instead is, from command mode, you can use j and k to go up and down. So let's write some text out. You can use k to go up, j to go down, l to go right, and h to go left. And if you look at some keywords, that VI was originally written on, they actually had the arrows printed on hjkl, like up, down, left, right.
[00:01:36]
And they didn't have a separate pad where you could have arrow keys. It was actually built into the physical keyboard, this kind of thing.
>> James Halliday: You might notice that in other places, this j to go down, k to go up, is available. If you open up less of some file,
[00:02:05]
>> James Halliday: In less, you can actually also use j to go down and k to go up. Also in Twitter and in Gmail, same thing, because this convention is extremely established among programmers. So they'll often put it into whatever they're working on. So a lot of the time, it's like a neat little Easter egg that you can use these shortcuts kind of all over the place.
[00:02:37]
>> James Halliday: Okay, so here are some more ways of moving around inside of Vim. If you remember, from the regex section, we used the caret that was the anchor to the start of the line. There's a similar thing in VI where the caret means jump to the beginning of the lines.
[00:02:53]
So here, if I'm kind of far off onto the right side, I can hop to the beginning by typing a caret. I can hop to the end by typing a dollar sign. Just like regular expressions. Kinda of neat. You can also hop to the beginning with a zero, which I usually do, because then you don't have to hold the Shift key down.
[00:03:13]
So 0, $, 0, $. So once you're at the end of the line, if you go up and down, it stays at the end of the dollar sign, which is kind of nice, or it stays at the rightmost position.
>> James Halliday: If you want to hop to the beginning of the file, you can do gg in command mode.
[00:03:36]
So gg, beginning of the file. And then G goes to the end of the file.
>> James Halliday: So, gg, G, $, 0, $, ^.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops