Neovim - Setting options with Lua

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay so this will be the first video uh where i actually go over some of the code for the neovim from scratch series uh you're gonna want to head over to github.com lunarvim slash neovim from scratch if you want to follow along um we're gonna be going over the options branch so i have them numbered you can you know feel free to skip ahead if you want to but we're going to be going over options in this video i'm also going to be going over how to structure your config just the basic structure to kind of get you started so you can understand uh like what what are what is the lewis directory what is in the low and so on so forth uh to follow along if you want to follow along directly with the code that you you know we'll see in this video then you're going to want to clone uh this repo into your config envelope you could follow along without doing that but it's probably easier just to follow along with this if you already have an nvim directory in config then you're going to want to move it to something like nvm.old okay so just make sure you've cloned this and or you know you're ready to follow along and we're gonna go into dot config and then okay uh you'll notice that we're on the master branch so the first thing we're gonna do to just you know only get options and only have enough to like only see the options so we're not overwhelmed by all the stuff that's there with the final product we're just going to check out oh one options here and then you'll just see basically the same stuff here but you can ignore the license file and you can ignore the readme.md file you don't have to pay attention to those at all now in a lua is essentially the entry point for neovim so neovim is going to look for this file first and any lua code that it finds in there it's going to run on startup the lua directory here is essentially where you're going to keep all of your other.lua files so you can kind of break up your config nicely so it's essentially like a path where you'll be able to reference other lua files and find other little files but you'll see how that works um as you follow along with the series so if we go into lua you'll notice i have one more directory in here and it's just called user so i just call it user you can name it after yourself so i could call this chris if i wanted to it's arbitrary if you you know you want to have this be some neovim distribution in the future or something like that you can name it after that you can name it whatever you want it's it's it just has to be something here and the reason for that is because if you start putting lua files in here they may collide with some other.lua files that are named the exact same thing so one will end up like overriding overriding the other so that's why we have this here just as like a namespace all right and so we're going to go inside of user and we actually see our options.lua file so let's open that up all right so basically you can just see all these options here i'm not going to go over every single one but i'm just going to explain a few and then i'm going to explain like just kind of what's going on here and kind of where where you can find more options so with this options file you can see that i already have given like a bunch of comments like just basic comments to kind of explain them now they'll be explained in much more detail if you do help so we're going to go into command mode here we're going to help options at the bottom you also can tab down here so if i start typing like h and i go through you know you can see that there's help right there actually what i should do is turn on screen keys so you can see my keys here and so we're going to do help and then we'll just do options and we're just going to tab and you can see that we get options here all right so this is a big file full of options and it's essentially like if we go through it for instance you can see that we have backup as our first one we can look up back up and you can see that this option is described in detail right there the next one is clip board right and you can see this option is described in detail here so i'm not going to go over every single one i'll leave that up to you if you want to read over every single one or if these comments are you know sufficient for you then that should be fine but these are the options that i have set now some of them may already be default options in neovim right um sometimes they change them sometimes uh you know an option will be updated in the future and sometimes i just forget or i don't know or whatever and then i just have them set anyway because even sometimes you'll show up here and even if it is set and you already have it set to something like like say smart case was already true then maybe i want to come in here and set it to false every now and then so i'll just leave the option open so i can come in here and easily reference it right so all right so let's let's we already talked about um where you can get more help um also let's talk a little bit about lua and like what's happening here anyway so like these are this is how you comment in lua um lua is probably new to a lot of people so it's like a whole programming language that you kind of have to learn a little you'll have to learn a little bit uh to actually set up your config but you'll notice that it's really expressive and really simple so two dashes are is just a comment um you know you just have basic stuff the weirdest thing that you'll probably see here is this uh curly braces kind of thing here right it looks like a list of strings this is what's known as a table but we'll we'll kind of go into tables more in depth later on in the series but you can just think of it as a list and then also a kind of way to you can put key value pairs in there too but for now we're just going to think of it as a list and that's what this particular option takes all right so these are just some options so like for instance for mouse we have a that's for all it just allows the mouse to be used in all modes um i don't know what else right the split below make sure like it makes sure that all of my splits that are horizontal always split below same with if i do a vertical split it forces it to split to the right and there's a bunch of other ones like if you want um like if you tab for instance and you only want it to tap two spaces because i think neovim does eight by default and vim has always done eight by default um i don't know why that's a default but like i think a more scene option is like you know two or four or maybe even three if you're you know but yeah i would recommend setting it to two or four or something like that and there's a bunch of other things like you see this line under uh where i'm moving here that's the cursor line you can see the numbers i have numbers set to true if we set that equal to false it wouldn't be there and so and so forth um the other thing that i'll note is down here at the bottom you can see that i have this other thing and for all options again it should be obvious but you just do vim dot opt dot whatever the option is and then you just give it a value whatever value you find in help options so let's also talk about um this vim.cmd down here which is a little bit different so vim.cmd what we're able to do with this is we're actually able to run vim script inside of a string so what we can like we can pass vimscript as a string and then neovim will interpret it as actual vim script and we'll run it so for instance i have an option here this is an old kind of way of setting options right and so set is keyword and i do plus equals um uh like a dash right there right now i'm pretty sure i could just append it right but i figured i would just leave this stuff down here so you can see that you can actually write if you're used to vim script or something like that and you know how to do something in vim script but you don't know how to do it in lua you can use something like vim.cmd just to pass a string of vim script that will be interpreted as actual event script later on so for instance what that thing does if i do asdf asdf it'll treat it as one word so if i do like dw on top of that it'll delete it if i didn't have that then it would have only deleted this first asdf and left the rest there so that's what that is keyword thing does if you were if you were interested anyway so that's pretty much all this so but let's talk about how i actually know about this options file like how how does neovim actually find this options file and set all of my options so we're going to do just quickly pwd so you can see where we're at at this point we're in config enviem lua user right so let's go back a couple directories here all right and so then again you can ignore readme you can ignore license we know it's in lua now let's take a look at init.lua so init.lua the only thing it has in here and this is this is you're just going to see this this is all i'm ever going to have in uh in a.lua is just a ton of required statements and what what this does is you can notice that first of all i don't have lua.user.options and i also don't have options.lua right what i have in here is i just have require user.options the lua is implied and also.lua is implied so you don't have to care about that i'm just going to introduce a quick command here and we're in command mode i'm just going to do lex blur that's going to open up something called net rw over to the side here so you can kind of go through um you know your files and stuff and so you can see okay here's edit.lua we go into lua we go into user we go into options and that's where it is right so that's that's kind of how you can see that so we're just going to close that so again we can just do user dot options you don't have to put the lua you don't have to put that all right and so that's all you had to do to get it to require your options now notice that if i comment this out really fast and i restart neovim all of my numbers are gone i don't have the cursor line anymore if i try to do like asdf dash asdf here and i do dw now that isn't treated as a full keyword there so you can see that you know kind of what happened um none of my options are set so we're just going to put that back to normal like this whoops okay we'll save that reopen it and all of my options are set again yeah so that's basically just kind of a simple introduction to options now um i did options as simply as i possibly could in this video uh you'll probably some people will be like oh well you don't have to you know repeat vim.opt over and over and over and over again um what you could do is you could just set vim dot opt um or you could do o is whoops o is equal to vim.opt right and then instead of vim.opt all over the place here you could just put o right so for every single one of these things you could just put o and that would be the exact same thing essentially as as vim.opt all over the place now even you know if you want to take that a step further and because this is lua it's not vimscript i feel like vimscript people um they never really treated vimscript as like a real programming language like some people obviously did but most people i feel like didn't but i feel like lua is a little bit more um i see i've noticed more people actually like using for loops and like all these other kinds of things so if you check out the next branch and you go and look at how i'm setting options in the future and this is going to be just a little bit more advanced lua just so um you can kind of get a feel for you know what you're able to do here and you don't have to set options like this but this is another way to do it is instead of setting the options with vim.opt or whatever i'm actually just putting all of the options in a big table here and remember i said a table is a list but also inside of this list you can put um you know you can put key value pairs so we have backup equals false clipboard equals on name plus uh command height equals two so on and so forth right and you can even you know nest this kind of stuff right so we have a list in here so you really have to start thinking about like a table as as you could have just a regular list like this or you could have like a list of key value pairs or so on and so forth right um you also notice i put local if you don't put local then by default all of your variables are going to be global so you want to put local and that way it'll be just scoped to this particular this particular file here uh then the next thing you'll notice is if you remember vim.op down here right so what i'm doing is pairs this is essentially like pairs is just key values right so what i'm saying is for key and value in pairs of options where we have options up here at the top right and all these key values in here we say that vim.opt of key where this is you know a particular key is equal to a value and then these are all the values on the other side of the key right so this is another way to do it it's just kind of like for looping through all of them i figured i would introduce this as well just so you could like see a more advanced way of kind of doing something simple just so you could like get a better feel of lua um and to show you that you don't actually have to do it this way either uh you could just do it the you know the easy simple way as well but i figured i would i would you know put this in just so that uh i would appease people who wanted to learn a little bit more about lua all right and so in the next video we're actually going to be checking out this branch here o2 key maps and we're going to actually go over some key maps and how to set key maps for different modes things like normal mode or visual mode so and so forth all right so that's pretty much it for this video that's basic options in neovim and how to set them and just kind of an introduction to uh your your um your kind of config layout here and a little small lua introduction as well and you're going to learn a lot of lua as you do this you don't need to know lua yet you will learn lua as you go through this um this series and you also just go through like learning about how to how to set up this neo-vim config you'll you'll learn a decent amount of lua just doing that all right so i'm just going to mention really quick if you want to support the channel you can do so over on patreon or you can do that with github sponsors over on my github page here that's really appreciated if you decide to do that but yeah that's pretty much it for this video stay tuned for the next one we're going to be doing key maps and pretty much you know everything else here at some point will end up being a video as well so i'll see you guys in the next one make sure to like and subscribe and that's it
Info
Channel: ChrisAtMachine
Views: 688
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: hY5-Q6NxQgY
Channel Id: undefined
Length: 14min 47sec (887 seconds)
Published: Sun Dec 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.