Learn Docker With My Newest Course

Dive into Docker takes you from "What is Docker?" to confidently applying Docker to your own projects. It's packed with best practices and examples. Start Learning Docker →

Toggle Boolean Vim Settings with Inv or an Exclamation Point

toggle-boolean-vim-settings-with-inv-or-an-exclamation-point.jpg

This can be really handy to set up a few mappings to toggle spell check, relative line numbers, white space characters and more.

Quick Jump:

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.

Never Miss a Tip, Trick or Tutorial

Like you, I'm super protective of my inbox, so don't worry about getting spammed. You can expect a few emails per year (at most), and you can 1-click unsubscribe at any time. See what else you'll get too.



Comments