Neovim - Completion Tutorial 100% Lua

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to be talking about completion in neovim all right so if you're following along with the neovim from scratch series you can head over to the repository i'll leave a link in the description and you can check out the o5 completion branch um if you're following along with your own neovim config that's fine too you're just going to have to you know write it all out yourself um we're going to be using nvim cmp as our completion uh plugin here i'm sure there will be different ones in the future or whatever but uh right now i think this is kind of the one that most people use and it's it works really well for me and i think it it adds a lot of nice extensibility as well which i'll show later on in the video so we're going to be using this so the first thing you're going to want to do is wherever you keep your plugins and probably can't really see that but i keep mine in lua user plugins like that and from here what you're going to want to do is install nvim cmp and you're going to notice that there's a bunch of other cmp related um kind of extensions right and so these are all extra completions so for instance buffer completions path completion scene like command line completion so on so forth um the reason that we have these i guess is just so all of that doesn't have to be built into nvimcmp itself and so this way you can create whatever completion extension that you kind of want i'll show you where to find a bunch of them but these are the ones that i think are useful right now in the future we're going to install one for instance for lsp but this video is not really about lsp the next one will be so but we have we want to set this up first so these are basically extensions uh one other thing that it will require is a snippet engine a snippet engine is something like lewis snip or snippy or um v-snip or something like that i use lewisnip i've been using lewisnip for a while so i'm just going to do this with lewisnip last thing that i'll add since we might as well just do this while we're here is just to add a ton of snippets friendly snippets is a repository that um just has a ton of snippets for a ton of different languages so likely your language will be supported out of the box and you'll have a ton of uh community curated snippets that will just work you know okay so those are all the plugins that i recommend installing when you're done you can just order whoops you can do packer install like that if you're using packer and it should install them for you i already have them installed so nothing happened for me and there you go so now let's get started we'll take a look at this and i'm just going to show you how it works and then i'll show you how i have it all configured so first thing is anything in this buffer right now if i start typing like re you should see that we have we can get to require and that's found in the buffer and you can see there's a little message there and or we could do um like a snippet and so req is a snippet so you just put an req and press enter and you get a snippet and we could require you know like some other module right named whatever so that's that um you'll notice too that i have like a super tab kind of functionality here so if i type fu to get a snippet this is the function snippet you'll see that how it moves through this snippet is um like you can kind of see this like pop-up menu here and you can see that this is how it works it goes like okay if you see a dollar sign and then a number you'll see that that's where it'll go first then the dollar sign number is going to go second for the two right and then dollar sign three is where it'll go for the third time so and then whenever you see a colon that's what will automatically be there before you even start typing so we should see name inside of our function like automatically it should say name and then we should see after the colon here we should see a comment for code inside of the snippet when we're done so let's press enter okay and we do see name and we do see code so let's talk about this concept of kind of like super tab and so super tab just means that the tab key is like overloaded so you could see that i could cycle through completions with it earlier but also i can do this so like if i name my function this and then i press tab it'll immediately take me to the next snippet location i'll press as like asdf or whatever again and then i'll press tab again right now it has that open so i guess if i close it with ctrl e and then i press tab again it should take me into the next piece of the snippet and i can just do asdf again and i should be fine and that's pretty much it then it'll take you to the end and then you can just use tab as normal just like that so now we can you know now so tab will work normally just creating tabs it will uh it will let you move through snippets like we just did and then also on top of all that it will also let you move through the menu so that's kind of this concept of super tab it can do a lot of um it can do a lot of things as you would expect it to be able to do and setting that up has never really been super trivial so you'll you'll see how i did that in uh in the setup which i'll i guess i'll go over now so um if you're familiar with um how i uh set up a lot of this stuff then you should be familiar with uh how you know how i'm gonna do how i'm gonna set up this cmp uh plugin so we have a file for each kind of like plug-in or thing that we do and so we have one for cmp as well all right so the first thing we're going to do and i'll make this nice and big so you can see it the first thing that we're going to do is we're just going to require these two um plugins here so one for cmp and one for lewis nip we're just going to run this really fast to lazy load i guess plugins or the ability to load plugins from vs code so vs code like plugins for lewisnip i think that's what it does i'm not 100 sure but i know that let's make sure that all my snippets work um then i have a function here called check backspace you don't really need to be super concerned with this but it's basically just going to help us help our super tab work better it's just going to make it's going to make it work more uh how you would expect it to that's all you really didn't need to know about that at this point uh these kind icons here are all from like a nerd font and i'll go into or some of them are from like an icon from a nerd font right and i'll go into i guess how to set these up for lsp later but they're pretty much all lsp related so a lot of these you won't see until in the next video when we set up the language server protocol stuff um but yeah so these are just all of our icons if you're interested in getting a nerd font i'll probably make a video in the future uh for for how to install a nerdfight and you can find more uh symbols down here so you can if you don't like these symbols you can add other ones that you can find here all right so now let's talk about actually setting up cmp so if you head over to the nvim cmp repository you'll notice um that i didn't just you know come up with all of this and write it on my own i'm basically just copying and pasting this chunk right here but with a few changes like i'm not just requiring cmp i'm doing in a protected call so that it won't like if i don't have it installed it won't like break or whatever uh so you'll see that we have to put a snippet engine so this chunk right here is this chunk right here it's just that instead of choosing vsnip i chose lewisnip as my snippet engine um and then we can go down to mappings so ctrl k and j i just added them so if i do something like if i want to move uh throughout the menu with control j and k i can do that so just like i could do it with uh control n and p just like that uh there's also control b and control f so if we have like i guess there's not really much that i can really show you that i can scroll through but like for instance if one of these uh windows here was much bigger we'd be able to scroll through it with a control b or control f so just keep that in mind i guess uh the next thing would be control space so if you want to pull if you don't want to like type to pull up the uh to pull up the completions you can just do control space and then you can pull them all up that way and this has the added benefit of giving you every single possible completion available uh so you can kind of look through them control y i'm actually not sure what control y did so i guess we're disabling it here um i never really paid attention to what it did maybe it selected the thing or something like that i'm not really sure but yeah that's probably like maybe even a built-in neo-vim kind of concept that i just am not aware of uh and then control e is to exit so you can see in insert mode we abort and in command mode we close so we're going to just do something like uh i don't know like if we just open it up and we're going through it or whatever and you don't want it there anymore you can just do control e and it'll get rid of the completions all right so now let's talk about uh cr here what does cr stand for it stands for carriage return what is a carriage return it's just the enter key that's all it is right so if we go in here and we you know start typing something in we're tabbing through it or whatever and we just want to press enter it'll just press enter it'll choose it for us and it'll make it it'll confirm the selection and make the uh the window disappear okay and now there's this concept of using tab as super tab right and this is how i do that this is um kind of like a custom mapping it's kind of difficult to get this all working and explain like how it works or whatever but basically if cmp the menu is visible it's select next item if i have the ability to expand a lewis snippet then what tab does is it expands the lewis snippet if it's jumpable if it's expandable or jumpable right then i will jump to the next thing then we'll do that check backspace thing that i showed you a second ago and that'll just fall back to regular tab usage otherwise it's just a regular it'll just behave like regular tab there's a very similar thing that happens here with shift tab but it's just it doesn't have to do as much so that's kind of how i'm getting super tab to work and i explained super tab earlier in the video so you should understand kind of what that's doing all right so now let's talk about formatting and what i mean by formatting so formatting is if we are inside of here how does how does this look right like how does this uh completion menu actually look how's it formatted and so how i have it right now is to give me a kind symbol and then give me the abbreviation and then give me the um like a uh what what is this thing like the i guess the menu or something like that um and and you'll see what this you can see this menu thing here so that's what we're calling it um and the menu is just telling you like does it come from buffer does it come from as is it a snippet and then you'll see in the future does it come from lsp does it come from is it path so if we do something like this you can see these are all paths right so yeah so that's what that is uh we can move that around so like if you wanted um for instance if you wanted this little symbol to be all the way over here you could just move what that symbol is called what we're calling those is we're calling those kind so that's like a kind i guess and if you wanted that all the way at the back you could put it all the way at the back okay and so this is a format function here i don't know how much he talks about format functions and i'm sure that's in here somewhere i know that if you go and check out the wiki so if you're on the mvm cmp repository and you click wiki you should see advanced techniques here mappings like all different extra kinds of mappings that you can do i think he goes over super tabs somewhat here and just other things i mean i'm sure you'll find stuff here that i that might be useful to you that i i don't have set up for this um but anyway so this is how this format function works uh you can format a string for your kind so like for instance we can instead of and i'll show you i'll show you just like a different format just for the heck of it so this is one format and this is for dot kind right so the dot kind remember being the first thing that kind of shows up right so in this case it's like a pair of scissors for a snippet um instead of that we could get that and then we could also get the actual kind itself that'll be more useful when we look at lsp and i'll just leave that there and i'm gonna i'll change a few things and then anything that comes from lewisnip right now we just have that say snippet but we could for instance we could put something like lua snip like that we'll just make it capital to be consistent and then buffer i don't know you could change that to like file if you wanted to call it something like that or whatever right and now you'll see those will change and then we'll just return this as a vim item and then this will format things also maybe we'll uh swap kind to be where menu is and then we'll do grab menu and put that over here so then you'll see that it'll just kind of be out of order so let's save that and then go take a look at it again and you can see the difference so now you can see that the kind is afterwards and then also the the new menu thing is in the front there so that's how you can kind of change this around a little bit all right let's go back into cmp and that was formatting so that's just some basic formatting you can play around with this and add stuff that you want i think likely you're just probably going to want to leave it the way that i had it there before um these are just some confirm options now these are sources this this is also very important um in the order that you have this is the order of precedence that will that will like um so so for instance like if you have buffer over top of lua snip right then all of the buffer stuff will be listed before the snippets if you have path first it will be listed before all the other things so how i like to do this is i usually like snippets then the buffer and then what i usually will end up putting over top of this will be my lsp like uh completion stuff and i'll go over that in the next video so this actually is important the way that you set this up um there will be indexing done here so this is the xerox index this is the first actually i think in lua this is the first index uh this is the second and this is the third and so yeah so that's something to be aware of if you're like how come all of my buffer stuff shows first i think that's annoying i want all my lsp stuff for my snippets to show first however you want to do it just make sure that you're ordering this in the right way that documentation thing that shows up every now and then so like if i do something like whatever like that there you go to get this to show up um you can just add like a border or you can set it equal to true and the documentation will show up if you don't want to show up then what you can do is you can just get rid of this and you can set it equal to false and then that won't show up anymore uh there's other things too like ghost text which is experimental at least right now it is and native menu i'm not really going to go over native menu but ghost text looks something like this so if we come out and we do like i guess you can't really see it but if we do like require here you can start to see uh if you look pretty closely there you can see that it uses the comment highlight group and um you can see that it's already starting to complete some of that for you inside of like i guess with virtual text right there so yeah so i guess i don't know if that's like super helpful but if you can see like with the color scheme uh it'll like i guess i i don't know if that's super helpful but it's it's an option you can set it if you want to all right um that's the experimental stuff and yeah i think that's pretty much it i think probably the hardest concept in all of this is understanding that there are extensions and sources i guess they're called sources i've been calling them extensions um if you want to find other sources then what you can do is you can go down to here we go so where can i find more completion sources there's a link you can click here and there's all kinds of completion sources so this is just mvmcmp there's one for org mode that shows up uh this is for i think like sql based stuff i don't really know what that one does this one's actually kind of interesting because this one we're going to pull into the next video and this is going to give us the ability to actually use lsp in our completions this is for rust so if you know anything about like crates and rust and cargo and all that kind of stuff this will help you with that um this is one for git i don't think i've ever used it but it does something this one's for rip grap we're using path right now uh lewis nip we're using now buffer we're using now there's one for tab 9 tree sitter i think another really interesting one is this one right here that i think is pretty useful it won't be useful until we have our lsp set up but you'll get extra vim completion stuff so in inside of your config so vim will actually be something and then after vim you'll get something like you'll get completions for opt or something like that right so that's kind of useful for when you're when you're editing your config to have this cmp and vim lua source right here uh yeah so there's ones for npm so on and so forth there's like tons of them and all you have to do to actually get the ones that you want are you go to wherever you're installing your plugins jump to the bottom here just add it after this so like for instance if i wanted to add uh let's see what's one that i actually could show you um i don't think there's really any that i can like show right off the bat without lsp but how you would do it if we wanted to add for instance the mpm one um and i'm not even sure if this one works i feel like i tried it before and it didn't but you know you can see how this works there all you would do is you would go and grab the repository like that and you would just do use paste that in there oops go like that and for me i just save the file and it'll install it for me um if you don't have that capability then you can just do packer install and that'll install it for you and then after that what you'll want to do is uh yeah whoops what you'll want to do after that is go into user cmp and then just add it as a source so we'll add it as a source the name should be and they'll usually give you uh what it is so the name for them is mpm so you'll just want to grab that there and uh whoops i don't know why that i don't i guess i didn't copy it but mpm like that and now the first thing that should show up is mpm completions when you are in like a package.json file for instance i think that's what this is for yeah for package.json so i'm not really sure if that works uh but you can test it out if you want but i'm just showing you how you can add all different other kinds of extra completion sources and just to kind of explain that they're all broken up into separate repositories so that i guess the scope doesn't have to explode inside of the mvim cmp repository and so yeah that's that's kind of a deeper dive on on uh on extra sources okay so i think that's pretty much it for this video i think um that's that's a decent introduction to to cmp and then other things like super tabs sources um snippets and all that good stuff so the next video we're going to be going over i hope lsp yes so the next video we're going to be going into lsp stuff now that we have the ability to do auto completion uh just because i figured we would get completion out of the way first because um you're going to basically want that before you it's kind of one of the core things in lsp is that you're you have the ability to auto complete certain things like uh you know symbols that are built into uh the language or functions that are built into the language or whatever else right so yeah so i figured i would do completion before i did lsp so yeah that's pretty much it for this video uh if you want to support the project you can support me over on github or patreon make sure to like and subscribe and i'll see you guys in the next one
Info
Channel: ChrisAtMachine
Views: 691
Rating: undefined out of 5
Keywords: chrisatmachine, linux, neovim, crypto, vim, free, rice, arch linux, tech, technology, unix, os, operating system, code, programming, software, open source, tutorial
Id: GuIcGxYqaQQ
Channel Id: undefined
Length: 21min 44sec (1304 seconds)
Published: Mon Dec 20 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.