Toggle Vim boolean options
Vim contains numerous boolean options — features that can be turned on or off. You’re probably familiar with some of these from your .vimrc
:
" turn on line numbering
set number
" show vertical cursor highlight
set cursorcolumn
" paste text without reformatting it
set paste
There are many more and you can see all Vim options with :help option-list
.
Calling any of these commands from Vim, such as :set number
, will turn that feature on. To turn it off, you can call :set nonumber
and that feature will be disabled.
But did you know you can also toggle any one of these by adding a bang !
at the end? Calling :set number!
will toggle the line numbers either off or on depending on their current state. You can also check the current state of any option by adding a question mark ?
to the end: :set number?
will return either number
or nonumber
depending on if the option is on or off.
When you’re turning options off, this can be a minor shortcut in keystrokes. But it has several other added advantages: You can enter command mode :
and then press <ctrl-p>
to cycle back through your history, finding the toggle command and invoking it again.
Or, if the toggle command was the last colon command you ran, simply press @:
to repeat the last command, thereby toggling the option back on or off again.
Here’s a quick demo of toggling options in Vim: