VIM Fundamentals VIM Fundamentals

Offline Ordering with getqflist

Check out a free preview of the full VIM Fundamentals course:
The "Offline Ordering with getqflist" 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 searching for and ordering the course lessons using grep and getqflist in lua. The example in this segment demonstrates the ability to create custom plugins to edit quickfix lists.

Get Unlimited Access Now

Transcript from the "Offline Ordering with getqflist" Lesson

[00:00:00]
>> So I was actually on the airplane coming here and I really wanted the practice, the presentation, but I had a problem. I was on the airplane and the site wouldn't load, because I think there's an external dependency on some sorta styling or something, and it just looked all sorts of wonky.

[00:00:13] So I thought, okay, I can just use Vim to walk through my presentation. I can do this, I'm an engineer, I got this right? I got fuzzy finders, I'll just do it, this is not too hard. So let me just show you this really quickly. This is where we're kind of going off the rails.

[00:00:29] I'm gonna try to write something that's a little complex on the fly. You never know it works at least half the time. Every single time. Alright, cd, I think I called it vim-fundamentals, there we go. And when I was in here, I was like, okay, so all I need to do, let's not do it here, let's go to cd course-website, there we go.

[00:00:48] All we are going to do is just walk through my lessons, but then I start sitting and going, gosh, I can't remember the order of these things. What are the order of all these files? I'm gonna have to remember the names for them, because they're not quite named the right thing, right?

[00:01:03] Alphabetical is not correct, because look, this one is border 15A, and color my pencils is 10. My goodness, there's no way I'm going to be able to do this, right? I'm going to have to search through all this really manually. Well then I thought, wait a second, okay.

[00:01:20] I am a scientist, sort of, I've sort of considered myself a scientist, I quit Vim a few times, I could do this. So I thought, alright, I'm gonna do grep and I'm going to grep for order colon. Looks pretty good, right? And I'm going to do it for everything under my lessons directory.

[00:01:37] Start out md, all right, awesome. We got 32 of them. Yes, we're there. So I can start walking them right? I can walk them for okay, hold on. Now they're still all the wrong order. Why are they the wrong order? So I'm gonna go copen, okay, they alphabetized them based on their file path.

[00:01:56] Okay, so I need a better way to do this right? I need a way to do this a lot better. If I were to alphabetize them on the order string, well, there's a problem, right? What's the problem? String ordering of numbers is not the same as natural ordering of numbers, right?

[00:02:13] I'm gonna have one through 1 to 10, to 20 to whatever, right? It's gonna be that annoying ordering that's not quite right. So I thought okay, we can do this, we got this, I have never done this before until I was on an airplane with no internet. So I was like, okay, I bet you there is a way to get the quick fix list.

[00:02:29] I am gonna type in getq and hit tab. Yes, yes. So we knew there is a good quick fix list. I can get the quick fix list. So I hit Enter, and let's just do this up, hold on, I'm doing help. Let's do getqfixlist. What is it called?

[00:02:47] Getqfixlist, yeah, my goodness call getqfixlist am I not knowing how to type, echo. There we go. Echo getqfixlist, there it is. My goodness, there we go. I'm like, so what's in here? I've never looked at this before we can totally do this. Look, there's text with that text order this.

[00:03:10] All right, I can do this, but I can't do it in Vim, because there's no way I can do this in Vim off because I just don't have the superpowers with Vim to be able to do this. But I bet I could do it in Lua. Well, I'm using Neovim so I can do this.

[00:03:23] So then I thought, I'm gonna go Lua. I'm gonna try to zoom this in so you can see it here. Let's get all that off the screen too because it just makes it hard. I think this is as zoom in as I can zoom it in. Alright. So I went, alright, we're gonna go set quick fix list, right?

[00:03:37] But in Lua remember it's Vim function, set, qfixlist. You wouldn't know that right? I said remember, but you've actually never even heard of that. I don't know why you would know that. So we're gonna do vim.fn.sort, and then I want to do vim.fn.getflist. Yes. Okay, so now I need to provide a function.

[00:03:56] I have no idea how the source function works, but I'm just gonna assume that works like every source function I've ever seen in my lifetime. I'm gonna say function a and b. Yeah, that seems about right, I'm gonna hit enter. Okay. It said error. Danm, I have to do it all in one line.

[00:04:11] Okay, we're gonna do this all in one line, here we go. So I'm getting ready getting ready to do this all in one line. And how do we do this? So I thought for a second I went all right. I bet I don't wanna do it this way, so I didn't do it this way.

[00:04:25] Instead, I went here and I opened up a simple website. I have to do the grep thing one more time, grep up here, get all those. Awesome. And so what I did is I actually just deleted one of these files really quickly, and I just typed it in myself.

[00:04:39] So I did vim.fn.setqflist. I was on an airplane It was fun right? I'm now I'm just determined to do it. I'm not practicing anymore, I'm determined to make this thing work, sort, and then vim.fn.getqflist. And then I'm gonna go function a, b, and now I can go local a text equals a.text.

[00:05:05] Yap. And do it again with b. So now I'm just writing Lua. I bet you I'm like, all right, I bet I can do a num and I bet you it's a.text substring starting at the 9th position, because remember Lau has one index and is ordering O R D E R.

[00:05:23] Colon space quote, one indexing, right? Go to the next one, so there we go, all right, awesome we're here. And then I want to get the length of a text and minus 2 because there's an A, and there's a quote at the end. I'm like, alright, we got this.

[00:05:37] We're totally going to get this. So I took that, I take that, I replaced the A's with B's, and I do GC for Do it globally all the way across the line but asked me first. No, yes, yes, yes. Awesome. Excellent. And then I can remove the highlight search with no highlight search, there we go.

[00:05:54] Okay, so this is looking good. I'm gonna go if a_num is greater than b_num, then return one. And is like how you do it in Lua of remember, I'm not in a Lua file, so I'm getting no highlighting. I just need to get this done. I'm freaking out here like how am I ever going to present?

[00:06:12] If I can't even write a little bit of Lua on the fly, and my indenting is wrong, it's three spaces, it should be four. Everything was going wrong on the airplane, right? So I was like return negative one end. This should be it then I need to do two of those awesome I can hop between them.

[00:06:26] That's it and so then I did Lua file, my current file. Okay, what just happened? Copen, ordering one a 10 went all the way through now I must have done something wrong look at it. It's like almost ordered but it's string ordering but, of course, to number. I bet you that's it, two number, bet you that's it.

[00:06:49] Save it, Luafile, did it, copen 1, 2, 3, 4, blah blah blah. Then I practice might, and then all I have to do is just walk through my thing and I just practice it straight through on the airplane. This felt fantastic cuz I solved a problem that was actually kinda hard.

[00:07:04] To do because I didn't want to have to walk through and try to find each one. I probably spent way too much time trying to fix it. But nonetheless, like this is the power that's available to you. You can write plugins to edit and change a quick fix list to make navigation the way you want it to be.

[00:07:20] The reason why I wanna show you this is just that right there should start sparking your ideas. Like if I can get stuff into a quick fix list, I could write it and do things I want with it to be more useful for me, and I can even write a really awesome language like Lua, which is a great embedded language.

[00:07:39] It's not something I'd wanna write every day but it's a great embedded language for the stuff and Holy cow, this is great. This is exactly what I wanted. For me. This was a really cool moment. Because I was it was taking me three minutes each time I wanted to find a file and I ended up solving the problem in five minutes and it felt fantastic.