VIM Fundamentals

Files & Navigation - NetRW

ThePrimeagen

ThePrimeagen

Netflix
VIM Fundamentals

Check out a free preview of the full VIM Fundamentals course

The "Files & Navigation - NetRW" Lesson is part of the full, VIM Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

ThePrimeagen demonstrates how to navigate vim's default file system plugin NetRW. A student's question regarding how to navigate between the splits is also covered in this segment. Ctrl + w will access window mode and allow window specific commands to be executed.

Preview
Close

Transcript from the "Files & Navigation - NetRW" Lesson

[00:00:00]
>> So let's go to Files and Navigation. So now we're gonna really start getting into a bit more Vim, all right? So now I'm going to assume that you kinda feel fairly comfortable with at least J and K. Try to get comfortable with those, don't use the arrow keys, avoid it where you can, just get comfortable with J and K.

[00:00:18]
I think this is where it is on Qwerty keyboards, I use Dvorak, by the way. And I forget where all the keys are on your hands, either way. So here is the repo that everyone should clone down, hopefully everyone has it. We're gonna move forward now. I also have the HTTPS version, you can get it from the website.

[00:00:37]
So let's navigate to the playground. And instead of opening up Vim, either to a specific file or just typing in vim, this time we're gonna type in vim dot. Hopefully at this point, you have enough knowledge about an operating system or at least a Unix operating system. I don't know if this is true on Windows, but dot simply refers to itself.

[00:00:58]
It's actually an inode that refers to itself, if I'm not mistaken, and that, it's just a circular reference. So when you open this up, you are opening up a directory at your current location. So let's do that now. This is what it looks like for me, it probably looks very similar for you.

[00:01:15]
And just maybe there's part of you that feels a little bit more comforted cuz now you see a file tree. You're like, okay, okay. This feels more normal editor-esque, at least there's a file tree. There's a whole bunch of stuff up here, I'm not even gonna read it, it looks like a bunch of words.

[00:01:32]
But in the end, it looks like I could do something here. This is okay, okay, things are better. So we can definitely move around and use this. And I'm gonna show you how to use this now. So the thing about NetRW is that it's a default plugin in Vim.

[00:01:49]
It's also in Neovim, Space Vim, I'm sure it's in all of them. All it is is just a file tree plugin. Is it the best? Absolutely not, but at the same time, I'm not convinced file trees are fantastic to be relying on. The only time I ever use a file tree is whenever I'm trying to create or delete or find a file in which I know its folder, but I can't remember its name, right?

[00:02:11]
And then often every now and then if I'm in a JavaScript project, people love to use index. And so you have 400 index files. And so fuzzy finders are effectively just destroyed, right? You press Ctrl+P in VS Code, you can't find your index file, so you end up having to kind of click around to get there.

[00:02:27]
So that's usually my only use cases for a file tree, and Vim has one. So let's try to open up a file via this. So how do you think we move around inside NetRW? Hopefully, if you've been remotely paying attention and my numerous amounts of clues, J moves you up, K moves you down.

[00:02:49]
You can move around just like you'd move around any other piece of code. Because in reality, this what you're seeing is just a buffer, just like the file you were in was just a buffer. It is simply a representation of something underneath. And you can interact with this buffer and it will do things.

[00:03:05]
So when I hover over source, I can press Enter. Everyone go to source, press Enter, get to it. Let's go all the way down to twitch.c, this is a fake file. And as you can see, it's been highlighted, it looks like this. This is obviously a fake include path, that's not real, you can't do that.

[00:03:25]
You see how this works, is now we're starting to see, all right, we were able to take our file tree and navigate there, awesome. But how do we get our file tree back, right? We just got done opening it up, it's gone now. I guess we're gonna have to quit Vim, reopen it up and navigate to the next file.

[00:03:39]
Honestly, I did that for a little bit. I didn't know how to do it, that's how I just thought you had to do it, I didn't know. So instead what you can do is you can type in another command. You can type in Ex, explore. And when you do that, it will replace your current buffer with an explore view.

[00:03:56]
The explore view will open up at the file you're in. So notice that I was on twitch.c and it navigated to the same folder as that, awesome. So I can reopen this back up. But let's say I wanna open it up, but I don't want it to replace my buffer.

[00:04:10]
I want it to have it to the side, feel a little bit more comfortable. You can do vertical explore, right? My goodness, look at that. And then you can close it. If you like it horizontal, this is the only one that doesn't really follow the expected conventions unless if you know some Vim-isms.

[00:04:25]
It's actually just Sex, and the reason why is that it's called a split, right? And so it's a split explore, and somehow that's just how it ended up being. And so you have vertical split explore, split explore, and explore. And so now you kind of know how to open these things up.

[00:04:42]
And it also matches a bunch of commands. So later on, if we get into it, you can open up windows by using s, right? So by using s, I've opened up a second split of the same item right here. And it's a horizontal split, or I can do it vertically, right?

[00:04:56]
So it kinda feels that way throughout the program. So it's gonna use a lot of the same key commands. And hopefully you're starting to see that Vim always redoes itself over and over again. Whatever one convention uses, it's fairly consistent to use throughout. All right, quick question? So the question was, how do you navigate between those tabs or between the splits?

[00:05:14]
I was actually gonna go over this in the quick fix section. But just to satiate that desire, I'll tell you how to do it now. So I'm gonna give you a little quick tip or a quick hint. If I press Ctrl+W, it goes into window mode, right? You have a set of commands based off Ctrl+W.

[00:05:33]
I bet you can guess, how do you move this way? Anyone wanna guess? L, the same way you would move if you're moving on a line. If you did Ctrl+W and you went H, you would hop back that way. If I were to split it, I could do Ctrl+W J to jump down, Ctrl+W K to jump up.

[00:05:54]
As you can see, Vim is constantly reacting like itself in various situations. This is why I was hard so much learning how to do these movements, cuz they constantly keep repeating themselves. Making it easier and easier as you get more and more advanced. And if you're wondering how I opened and did all the splitting, I just did Ctrl+W, and then either S or V right?

[00:06:19]
S to do horizontal, Ctrl+W V to do vertical. Remember, vertical and horizontal is based on the direction of the split, not how they're oriented. And then one last fun one. Since we're already here, I'm gonna show you my favorite command of all time. I know we're derailing and it's gonna ruin my presentation later.

[00:06:36]
If you have a bunch of splits open, do Ctrl and then O. My goodness is what that stands for, it closes everything but your current buffer. It's my favorite thing in the universe, cuz often I'll have open one of these. Perhaps I'll open one of these, and I just want everything else gone.

[00:06:52]
So I can just go bam, bam. You can do Ctrl+W O, Ctrl+W Ctrl+O, right? You can do all these different Ctrl+W Ctrl+O, Ctrl+W Ctrl+Shift+O, it doesn't really matter. As long as you hit Ctrl+W and O, it will work, it's beautiful. I love it, makes me feel super happy on the inside.

[00:07:09]
So hopefully everyone feels the exact same way, cuz [SOUND]. Just okay, maybe I'm the only one that gets that excited about a command, but it just is an incredible feeling.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now