VIM Fundamentals

Customizing vim with vimrc



VIM Fundamentals

Check out a free preview of the full VIM Fundamentals course

The "Customizing vim with vimrc" Lesson is part of the full, VIM Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

ThePrimeagen walks through creating persisting custom settings by saving commands to a vimrc that will automatically execute when a file is opened in vim. A brief overview of previous motions and how to access a list of available options is also covered in this segment.


Transcript from the "Customizing vim with vimrc" Lesson

>> I just pasted in the file you should be curling down. If you use all my zest, just type in C you press up, and you should be right back to it. The file should be called some, and then you're gonna rename it to exercise.js. Awesome, awesome.

We're gonna go back in there, and I've kept my spot if you haven't, remember the commands we have executed thus far. Where set scroll offset equals 8, you can choose whatever number makes you kinda feel comfortable. That's just how many lines either below or above, it will start auto scrolling for you, as you get near the bottom or the top.

Then we also did set number. I don't know why it's not auto completing. That puts the absolute position line numbers, on the side of your editor. And then, we also did our new or relative number. We did all of them, right? Relative number. And that means, that from your cursor, we're gonna to display the relative offset to help you with faster and more awesome than motions, right?

And so, now we're gonna to start getting after it. I can't quite remember what comes up next. So we're gonna just kinda look at it together, and I think everyone's gonna have a great time? So anyways, so we've done this, but here's the problem, right? We did all these settings, everything is awesome.

Everybody just quit with me for one second quit bang, get out of there and then just simply reopen it back up. You can either re curl it back down, or just simply open up exercise.js. Do that, and what's the problem? Everything that we have put in, gets forgotten, right?

And that is because vim, does not save your settings. You tell them what to do, it does not tell you what to do, well, some of the times. So anyways, so let's close this back down. Now, for those who are using vim, you will be opening up something called your vimrc.

And that will be located in your home directory. I just can do tilde a dot vimrc. If you're using ill vim, remember it's in your config path. I showed you how to do it earlier. I'll do it again really quickly right now. You echo out stdpath config, and that will show you where you have your a nit.vim, all right?

So I'm gonna go here and I am gonna edit my an edit.vim, which is a config, neo.vim and intvim, you edit yours, so everyone should have an empty one. If you've properly set up your environment to have a clean environment, it should be empty, and this should be really a new file.

So what we're gonna do is we're gonna do this set, scroll off equals 8, And then I'll try to slow down there. Remember, you can copy you can paste, so you can do your y your P to get that previous line, do that. Now, we can go over here, and we can set the other options.

So let's dwinumber, then you can hit Escape. You can go over one with l and dwdw. You could have done xx, you could have done some other things. There's some other more faster, and efficient ways to do that. But for now, I'm just kinda giving you the basics.

So let's do it again, let's yy, let's p. Let's the p, but then press word over, press I and then type in relative. Now you'll notice, you have these three options in your vimrc. Now remember your vimrc, is simply the identical thing to your command mode. It reads one line at a time, and applies the settings you have set into your vim experience.

So if I were to save this it'll now say hey, you wrote it, you wrote it to this location, which means I can now quit. And at this point, I can reopen up that curl file here. I'll just curl it back down again, and look what we have.

Our setting has been saved. As I move around, it moves it with me. I no longer have to re type in all those settings. So this is really incredible, right? Because now this is the very beginnings, you know that there's a vimrc, you know it's something as simple as just whatever commands I type into my command.

I can also put into my vimrc, and it'll be automatically executed for me. And that's at the beginning of every vim startup. So, this is just the beginning. Hopefully the vmrc doesn't feel that crazy right now. Hopefully it feels actually pretty normal. You're like, okay, I see it.

I can set some stuff. It just gets sourced for me at some point. It's kind of like a bashrc, right? You don't really control when you do a bashrc at some point in the process, it does it for you, and then you get to have all your shortcuts or whatever.

All right, so we're gonna keep on going. One thing that I really dislike, is how vim looked at that moment, right? Because it just looks ugly. I don't like eight spaces, I don't like tabs. I want four spaces, right? That's what I want. And so if I apply all these Settings, I will get what I want.

And since you kinda have to look these up, and find all the name of the settings, I just kinda provided them here for you. But if you wanna know what they mean, you could always use the help menu. So let's look at the term, set expand tab. What in the world does that mean?

So I can go help expand and I can hit tab, and I'll show me a bunch of different things that have expand in it. And like none of these are really the one I want. So I can Exit out there, and retry expand tab. And when I hit Enter, it'll take me to there.

And you'll see that in insert mode, use the appropriate number of spaces to insert a tab. Spaces are also used when you use the left and right shifting of stuff, right? You're like, okay, so expand tab simply converts a tab character into spaces. All right, I now know what that setting means, right?

Now when you see set expand tab, you know what it means that just doesn't seem like some magic setting, that eventually makes all your system work correctly. So you can do that with everything, everything inside all of your set options, you can just do help that set option, and you will get the editors definition.

If that's not good enough, or if it's written in kind of a goofy way, cause sometimes I read help options, and I don't really understand what they're trying to tell me. You can always Google it and some will take a different stab at trying to make it sound better, cause sometimes they'll use terms that are like editor isms, right?

You have to understand the nomenclature of the editor, to be able to understand what you're reading. And that comes with time and experience. But for the most part, they're fairly comprehedable. So I'm gonna take all these options that I have right here, and I will copy them. I'll put them over here in the chat, just to make it easy.

Is there an online resources that lists those settings? I'll get there in just one second, all right? So I pasted them all in the chat. And let's go back, and go here. And so, I'm gonna quit this file again. And I'm gonna go back to my vimrc. I'm just going to simply press up on my shell, get my previous command history, jump back in here, press Ctrl O, one line below and then I'm gonna Ctrl Shift V for me.

So capital V, if you're on a terminal usually that's how you have to paste in, so there we go. Now, I have all those settings and when I save this, it will now write it and when I reopen up them, all my weird indenting lows will be gone.

As you can see as I go down, this looks better. This looks more normal. This probably looks like what most people are used to seeing, when they see JavaScript, barring these bizarre bright blue, the cyan, yellow and light purple. Like besides those colors, everything else looks pretty much how you would expect it to look.

And so let's save it. Let's go back we have it awesome, this is kinda starting to feel better. Quick recap though. I know I mentioned this right before we went down, we were combining motions and all that. So in case you forgot, I'll do it really quickly or in case it feels a little disjointed since the last time.

Let's just jump back up here, I can walk back up there, or I could do it the new way we've discovered. I can type in a number 9k. I now have jumped up to the top. I can do shift v and do 6j, and I can do d.

Totally works. I can do d6j, right? We can combine these things together to make them work. Also fun little fact, I can do y 6 J, and then now I can paste them all I want. Look at all look of that pasting I just got done doing, right, cause I can now yank over a range.

How cool is that? Let me undo by pressing you all of that. And so you can see that we can take all these motions, and combine them. And here's the deal, is that right now that probably feels a little weird to do, I totally get that. But the more you use these motions, the faster and faster you'll get.

And there's also some pretty cool plugins, specifically for niobium. It's actually just like a core feature. You can highlight on yank meaning, it will flash the region you yank. So you can go, okay, I yanked that region. It actually helps you kinda comprehend your yanking and said going into visual mode first, highlighting what you want.

And then yanking, you can get used to using the yank motions instead of visual motions then yanking, it's pretty cool. All right, so let's exit out of there. Let's go back here. Let's look at this. So a couple things before we move on, is you can always Google any options like if you have problems with, say, your cursor going into a thin cursor versus a fixed cursor.

You can simply go and you could probably Google like, hey, vim options cursor, and you'll probably get something. You could also go into vim, and type in help options. Now this, eventually as you scroll down, will literally have every single option that is available all the way down, and it just keeps on going, keeps on going and eventually some where deep down, look at that, you can just see these things flying by.

I just saw something that said bomb. That actually there you go. I didn't even realize that there was, I don't even know what this means, but I'm sure as you see it, you'll see it. A Boolean is default off, right? You can go through, and look at all these different things that actually exist within vim, and every last one of them.

Now, honestly, this is pretty difficult, cause there is a tonne of options. If I go all the way down to the bottom I say set number, there's 7000 lines in this help document. I doubt you're gonna read this Stephen King novel, it'll be gripping but in the end, that's just way too much context to hold in your head.

That's why honestly, whenever I'm doing options setting or knowing what's available, I usually either jump in here, and do a quick like file search to see if I can find it. If not, I just jumped on Google try to see like, hey, is there some sort of option I'm missing?

It's very, very hard to have all these in your head and honestly, I forget them all the time. Completely normal, right? It happens to everybody. Another cool thing is that, I've showed you this a few times you can tab complete options, but there's also another available option. So if I go like this, help expands tab, remember how we couldn't quite get it.

If you press control D, it'll actually show you a pop up menu. And so you can see everything that's available. So you can actually look through those one at a time. And you see that the fourth option down right here, sorry I'm bad at using a mouse. Say's expand tab, so I can actually see that and then I can go okay, expand, expand tab.

And you can see, all right, I have all these options right here. And so when I press tab, I get the pop up menu. When I press Ctrl D, I get the kind of like the full, this is what we'd have autocompleted, it's kind of a nice way to look at it.

And a fun fact when you're inside of the tab menu, you can use Ctrl N to go forward, or Ctrl P to go backwards. You can also use tab, but I got used to using Ctrl, P and N, because a lot of plugins also use Ctrl, P and N to navigate these pop up menus, especially a lot of the autocompletes, the lSPc, CLC conquer completion.

They all use very similar pop up menu, s and they all require Ctrl P control N. You can use tab but ultimatel, y I just like to stick within kind of like a pretty defined bounds. It just operates vim the same no matter what. And so, I like learning some of the isms, and that's one of them I have gotten used to using.

That was a little bit.

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