Vim Tip #32: Vim Settings


tags: Vim

Vim Tips

This is not about any particular Vim setting: it's about how settings are controlled and how their status discovered. Once again, I'm ripping off the excellent Practical Vim by Drew Neil - although it's not a straight lift, I do have something to add.

Try typing (in Normal mode) :set - this will show a frankly alarming list of settings, many of which you didn't even know existed. This is by no means all of them - I think it's just the ones that have been modified by you or by your local Vim. If you want to find some more, check out :h option-list.

As an example, let's look at line numbering. To turn on line numbering, type :set number. To turn it off again, type :set nonumber. Vim settings that are toggles (ie. they have only two states, on/off) are controlled by :set <name> and :set no<name>. Vim also supports toggling settings - in two different ways. Whatever state line number display is in, you can switch it to the other state by typing either :set number! or :set invnumber. If you need to know what state the line number display is in (you can see for yourself in this case, but some you can't see and if you're writing VimScript the answer is often useful), use :set number? which replies nonumber or number depending on the state.

Similarly, :set spell responds spell because I currently have spell-checking turned on. The 'spell' setting is subject to the same tests and toggling.

Other settings require a value passed to them. I'm an old-school command line guy, so if I'm working on text documents, I often set 'textwidth'. Many of these values have short forms: in the case of 'textwidth' the short version is 'tw'. To find the current setting, use the same method: :set tw? (:set textwidth? works as well). The answer is textwidth=0 because I don't currently have it turned on - I leave it off when I'm editing RST files. If I want to wrap text at the 75th column (one of my standard settings for regular text files), it can be turned on with set tw=75. It can be turned off again with set tw=0. Settings that take parameters can't be toggled with the inv preface or the trailing ! toggle.

Any setting(?) can be reset to its default by appending an ampersand: :set tw&. The default for textwidth, it turns out, is 0.

Some settings apply to all buffers. Some (like tw and spell) are specific to the current buffer. Some (like colorscheme) apply to all buffers and it's not optional. But some (unfortunately I can't find a good example right now!) can be made to apply only to the current buffer by replacing set with setlocal.

While most settings have short forms, I'd encourage you to use the long form in your ~/.vimrc or ~/.config/nvim/init.vim file for readability. The short forms are great when you're toggling things on the fly (if you can remember them), but the vimrc is hard enough to read without using short forms.