Toggling in Vim: Efficiently Switching Settings

Have you ever been in a pair programming session where the developer showing you their code is using relative line numbers? You try to point out a specific line, but every time you look, the numbers keep shifting!

Relative line numbers can feel confusing at first, but they are incredibly powerful. Instead of thinking in absolute line positions, you can quickly navigate using motions like 5j (jump 5 lines down) or 3k (jump 3 lines up). Since these movements are relative to your cursor, they make navigation much faster and more intuitive. In this article, we’ll explore how to toggle line numbers and other useful Vim settings dynamically.

Toggling in Vim: Efficiently Switching Settings

Vim is highly customizable, and toggling settings on and off can be a huge productivity boost. Whether you want to switch between relative and absolute line numbers or enable/disable specific features, mappings and functions make this easy.

Toggling Booleans

A simple way to toggle boolean options in Vim is by appending ! to them. Here’s how you can toggle the line number setting:

nnoremap <leader>N :setlocal number!<cr>

Pressing <leader>N will switch between enabling and disabling line numbers.

Toggling Options Dynamically

For options that require conditional logic, you can create functions to check their values before toggling. Here’s an example with foldcolumn:

nnoremap <leader>f :call FoldColumnToggle()<cr>

function! FoldColumnToggle()
    if &foldcolumn
        setlocal foldcolumn=0
    else
        setlocal foldcolumn=4
    endif
endfunction

This function checks if foldcolumn is set and toggles between 0 (off) and 4 (visible fold column).

Switching Between Line Number Modes

This function cycles between different line number modes:

function! ChangeLineNumbering()
if &number == 0 && &relativenumber == 0
    setlocal number!
    echo "nu:1/rnu:0"
elif &number == 1 && &relativenumber == 0
    setlocal relativenumber!
    echo "nu:1/rnu:1"
elif &number == 1 && &relativenumber == 1
    setlocal number!
    echo "nu:0/rnu:1"
else
    setlocal relativenumber!
    echo "nu:0/rnu:0"
endif
endfunction

This lets you cycle through:

  1. Absolute line numbers (number enabled, relativenumber disabled)
  2. Relative line numbers (number and relativenumber both enabled)
  3. Hybrid mode (relativenumber enabled, number disabled)
  4. No line numbers (number and relativenumber both disabled)

Toggling Vimwiki Table Mappings

If you use Vimwiki, you might want to toggle table mappings, which interfere with other plugins like coc-snippets. Here’s how you can do it:

let g:vimwiki_table_mappings = 1

function! VimwikiTableMappingsToggle()
      if g:vimwiki_table_mappings
            let g:vimwiki_table_mappings = 0
      else
            let g:vimwiki_table_mappings = 1
      endif
endfunction

This function toggles the vimwiki_table_mappings global variable, allowing you to switch between normal Vim bindings and Vimwiki’s table navigation.

Conclusion

Toggling settings efficiently can greatly improve your workflow in Vim. Whether it’s switching line numbers, fold columns, or plugin settings, using mappings and functions allows you to customize Vim behavior dynamically. Experiment with these techniques to fit your workflow!