Editing options.lua defaults0:00
Out of the box, NeoVim comes with many sensible defaults that previously needed to be configured in vi and vim. But there are still a number of default settings that I prefer to change, and you might like to as well. We'll start by opening our .files repo and then jumping into NeoVim. In the previous lesson we created an options.lua file, so let's go ahead and open that in nvim-lua-user-options.lua. I've gone ahead and copied in all the settings I want to talk about, but at the moment they're all commented out, which in Lua is 2-characters. There are a few ways we can set options using Lua, but I prefer to use this vim.opt API,
Indentation and tabs1:06
we can tell it how many spaces to use by setting the shiftWidth. I also configure how wide a tab character should be, just in case we come across any while we're editing files. These are just the default settings I like to use. Later on we'll install a plugin that will respect any .editor-config files you might have in your projects. Next up, I like to enable smartIndent. So if you imagine writing an if statement, and once you've got to the end of the line you type the opening { and then you press enter, smartIndent just makes sure that the next line is indented by 4 extra characters.
Wrapping and line numbers1:31
you type the opening curly brace and then you press enter, smart indent just makes sure that the next line is indented by 4 extra characters. I then like to disable line wrapping by default. So line wrapping is great if you're writing a blog post or something, like some long-form text, but when I'm writing code, I generally prefer my code to not wrap and instead just scroll horizontally as much as it needs to. Now again, this is only a default setting. We can enable line wrapping as we need to by typing :set wrap, and that will turn it on temporarily for that session, and we can turn it off using :set nowrap. Next up, let's talk about line numbers.
Command-line tab completion4:02
But let's say I meant to use the win size command. I've now got to either backspace and retype or just keep hitting tab until I get to the option I want. So now let's enable this different default setting. And there's actually two settings here. So we've got this comma here that separates these settings. So the first setting is how it behaves before we've pushed the tab character, and the second setting controls how it behaves once the wild menu is open. So let's go ahead and source this file, and now we'll type colon wi and press tab. And you'll see now it completed all the way up to win, because that is the common match.
Spell checking defaults6:10
So let's go ahead and save that and you'll actually see the colors change a little bit and later on we'll install a theme to make things look a whole lot nicer. I also like to enable spell checking by default. So let's turn that one on and resource the file and now you can see we've got some things that Vim considers spelling mistakes and that's only because these are comments. So inside a comment, it's not thinking these are code. It's thinking we've just typed something wrong. Again this is only a default setting, so if we're in a file and we want to turn this off, we can always say :set no spell to turn that off temporarily and :set spell to turn it back on again.
Merging fill characters9:16
So let's go back to our settings and let's change our FillCharacter settings. Now one cool thing about the VimOptions API, so far we've just been completely replacing the default setting. So for ListCharacters, we've replaced it with a whole new object, replacing anything that Vim had there by default. For FillCharacters, all of the defaults I'm happy with except for this EOB, which is the end of buffer character. So we can use : append that will basically merge in whatever we pass here into whatever the defaults are. Let's go ahead and save this and we'll resource and let's jump back over to that empty buffer.
Confirm before quitting12:05
Next up, I like this confirm setting. So let's go ahead and we'll add some changes to this file that are not saved. If I was to go :q to quit now, we get this big red scary error, no writes since last change, and the only option we have is to press Enter. So let's go ahead and enable this setting and we'll resource the file and let's just make some more changes to this file. If I say :q now, instead of giving me an error, it now asks me whether I want to save the changes to the file, and I can say yes, no, or cancel. In this case, I'm going to push cancel. Let's delete that line.
