Vim Unset or Toggle a Boolean Option

There are a bunch of ways to toggle a boolean option in Vim. An example of a boolean option is number that shows line numbers when set or expandtab that inserts spaces instead of the tab character (\t). Setting them is simple:

:set number
:set expandtab

How can we unset or toggle them quickly? Well if you just want to unset/reset or set a boolean option off, then you can use the :se[t] no{option} syntax.

:set nonumber
:set noexpandtab

Another option is to toggle the option by inverting the value. This can be done by appending the set command with a ! or prepending the option name in the same command with inv.

" :se[t] {option}!
:set nonumber!

" :se[t] inv{option}
:set invexpandtab

After toggling or re-setting the option value, you can print the new value as well to check whether the operation went through properly or not:

" Toggle expandtab and then print the new toggled value
:set expandtab! expandtab?

Leave a Reply

Your email address will not be published.