Check out a free preview of the full VIM Fundamentals course

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

ThePrimeagen live codes a scratch buffer vimL plugin. How to create a new buffer, set local options, and passing in the command to the systemlist. The setting nobuflisted will keep the files in memory, but hidden to increase the plugin speed.

Preview
Close

Transcript from the "My First vim Plugin" Lesson

[00:00:00]
>> Let's see, plugin, plugin, plugin, plugin, while pontificating, I actually have a plugin that I was going to pontificate upon. While plugins while giving errors on how the best way to approach them. Interesting, so I don't do a lot of demo, so I can help with some, I'm gonna do a just a very small demo plugin.

[00:00:17]
I may even screw it up, honestly, I don't do a lot of VimL, I use Lua primarily, but you don't have to use Lua, you don't have to use Neovim. Some of the best plugins I've ever used in my life are done with VimL. So we'll get to those.

[00:00:35]
So let's just first do a little small plugin, okay? So let's create a new file. So I'm gonna open up this little quick fix, or the vex window, right? And if you just type in %, it'll say, hey, enter new file name. So I'll go, I don't know what to name is thing, we'll just call it plugin.vim, why not?

[00:00:53]
And once I save it, it is now in there. If I do pv again, you'll see plugin is now right there. I can delete it with capital D, if you want to, you can rename it and move it around with capital R. It's an okay interface, honestly, there's better ones, I think Dirvish has one of the better ones, NERDTree is really fantastic as well.

[00:01:09]
So we have this plugin, and let's just start writing something. So here's something that does happen from time to time. I want to be able to create a scratch buffer. This is actually based off of a, let's see, let's do this, was actually based off of a article that I found not too long ago, it's right here.

[00:01:29]
He goes through this really extensive scratch buffering that he makes. But let's just do a really simple one to show you kinda some of the powers of Vim. Right, so I'm just gonna close down that with my favourite command Ctrl+W,O, love it. What we're gonna do is we're gonna create a function.

[00:01:43]
Now in Vim functions you can do fun!, effectively, this just ensures that the function will always replace itself with the latest, whatever you source. And let's call it something fantastic, let's call it scratch, why not? There we go, and then let's also pass in a command. So what we're gonna do is we're just creating a function that passes in a command.

[00:02:04]
Now here's the thing, when you write a function, you got to end the fun. Don't forget that it, will get you every single time. It gets me every single time. So what is the command gonna do? Well for us, let's just do something pretty simple. Let's create a scratch buffer, and then just execute that command as a system command.

[00:02:21]
All right, so what we're gonna do, is we're gonna first create a new buffer. There you go, new, that's all we have to do. It's the same thing as doing new right here inside of your command. Notice I just created a new list right there. I can rotate it if I want.

[00:02:35]
There's all sorts of things you can do with it if you want to. We're not gonna really do that right now. So we're gonna just do new, I can do vnew for vertical split, we can do all of that. But we're gonna create a new one and then we need to set some options on it.

[00:02:47]
Now, the moment you create it, when it's created, your cursor will be moved there. And now we're executing this line of code, which means I can do something like this. set local, which will actually set things about this buffer only, it's not a set that's for the whole Vim experience, it's for this specific buffer.

[00:03:04]
nobufflisted, all right, so nobufflisted, so what is nobufflisted? Well, if you type in the word buffers, what you'll see is every file that I have opened up, right? So during this Vim experience, we keep the files underneath the hood. They remain in memory despite not being in view.

[00:03:23]
So I can actually have those and they'll load them up super fast. Because if you have a really large file, you don't want to have to read the whole thing from your operating system again just to get it back into memory. So they remain in memory. You can actually set certain commands to make that easier, such as set hidden, which means that you don't even have to save the buffer to put it into the background, it will remain there.

[00:03:42]
When you try to quit Vim, it will say, hey, by the way, have you forgot to save this? Do you want to save this file or not? Right, so there's actually some pretty nice ways you can use this. So set buff no listed means that when we create the scratch buffer, it won't appear in this list, which is good, cuz we don't wanna bunch of no names, right?

[00:04:00]
We can also do a bufftype=nofile, meaning it's just not a file type, right? It's just whatever, we won't receive any specific highlighting or file operations upon it. And also, I believe it's like, buffhidden=delete, meaning that when we leave it from our viewport, when we remove it from the window, instead of being in the background, it will automatically delete itself, fantastic, right?

[00:04:26]
So now we can create a quick variable, let's just call this output, why not? And this equals a system call, systemlist, I believe it's called systemlist, I'm always so bad at this. And now here's where VimL gets really [SOUND]. You have to do A for argument, command, right?

[00:04:43]
So I'm passing in the argument command into the systemlist. And now I have all this in systemlist, what it does, is it executes a system command and then creates it into a nice array. Since I am using a Neovim, I could use some Neovim specific API's, or I can just use a easy Vim one.

[00:05:02]
So I can just append the current buffer. So buffnr will get the current buffer, I think it might be called bufnr, I can never remember these things. call buffnr, and then on top of that, I can just pipe in the output. So we have a local argument, output, let's shout out percent sign, we're still doing this, we're must be calling something a little bit off.

[00:05:24]
Let's call append, there we go, we got it, we got it right. So now if I do the call scratch out, there we go, we got it, look at that first try. We did our very, very, very first try right here, we were actually able to take all these things and create something that will actually open up our own stuff.

[00:05:39]
Now I come in here and I can edit these things do what I want. Effectively, the tutorial that they do online, he wants to be able to edit SQL queries. So he has a way to be able to communicate, get these things so he can bring it into Vim, do a light amount of editing, and then put it back in, put it on a system clipboard, send it back wherever he wants to send it back.

[00:05:58]
And it's really, really nice to work with. And so scratch buffers are pretty cool. And this was really simple to write. I mean, despite the fact that I failed like zero times as I did the first try, it's pretty trivial. It's four lines of code, and it's actually can be pretty powerful.

[00:06:12]
You could imagine what we're piping into there. So there you go, there's like a little simple plugin, and I do wanna do some a little bit of pontificating. To me this is really kinda like the important part, which is, what do I think is an ideal Vim command, or setup, or an ideal vim plugin?

[00:06:29]
What makes Vim one of the more ideal ways to edit text, is once you learn everything, it becomes about thinking about the thing you want to do. And your muscle memory, everything else can actually fill in all the commands to do that. You want to replace all the text in the function.

[00:06:47]
You don't think about, okay, do I need to jump down by four lines, what do I do here? No, you're just di squirly, you get them all out, right, you just know right away. It becomes more and more simple. And so I feel like that's kind of really where Vim shines is plugins that also do the same thing.

[00:07:04]
So this is where I'll probably, and no knocks on anyone that uses Lazygit, but I don't like it because you have to do scrolling, you have to do a lot of Easter egging to get to where you want to be. Let me show you something that's a little bit different, and I'll show you how it works.

[00:07:19]
I'm gonna do this all at the same time. I'm actually gonna edit a plugin and actually make a commit right now to one of the plugins I've been using.

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