Toggle Boolean Vim Settings with Inv or an Exclamation Point
This can be really handy to set up a few mappings to toggle spell check, relative line numbers, white space characters and more.
Here’s a few examples using !
to toggle settings:
" Toggle spell check.
nnoremap <F5> :setlocal spell!<CR>
" Toggle relative and regular line numbers.
nnoremap <F6> :set relativenumber!<CR>
" Toggle visually showing all white space characters.
nnoremap <F7> :set list!<CR>
The above example does the same thing as what’s below:
" Toggle spell check.
nnoremap <F5> :setlocal invspell<CR>
" Toggle relative and regular line numbers.
nnoremap <F6> :set invrelativenumber<CR>
" Toggle visually showing all whites pace characters.
nnoremap <F7> :set invlist<CR>
Personally I prefer the first version because it keeps the command name the
same. I find it more difficult to mentally parse out the “inv”. Using !
to
invert something in programming is pretty common too, although that’s usually
put before the expression instead of after.
The video below demonstrates these mappings and goes into a bit more detail.
# Demo Video
Timestamps
- 0:11 – Toggling spell check, relative line numbers and white space characters
- 1:46 – Going over each set of custom mappings in my vimrc file
- 2:51 – I prefer using the exclamation point due to it being more readable
Reference Links
Which style of inverting a boolean setting do you prefer? Let me know below.