Sometimes I find it very useful to toggle the cursorline and cursorcolumn settings in the Vim text editor. If you’re unfamiliar with these settings, I’ll briefly explain. When you set cursorline, as you move the cursor from line to line, Vim will highlight whichever line you are on. Similarly, cursorcolumn will keep the column highlighted. I find this one particularly useful when trying to keep code (or text) lined up. To make it simple to switch toggle settings, I created some mappings in my .vimrc:

 map <silent> <Leader>cl      :set                  cursorline! <CR>
imap <silent> <Leader>cl <Esc>:set                  cursorline! <CR>a
 map <silent> <Leader>cc      :set   cursorcolumn!              <CR>
imap <silent> <Leader>cc <Esc>:set   cursorcolumn!              <CR>a
 map <silent> <Leader>ct      :set   cursorcolumn!  cursorline! <CR>
imap <silent> <Leader>ct <Esc>:set   cursorcolumn!  cursorline! <CR>a
 map <silent> <Leader>co      :set   cursorcolumn   cursorline  <CR>
imap <silent> <Leader>co <Esc>:set   cursorcolumn   cursorline  <CR>a
 map <silent> <Leader>cn      :set nocursorcolumn nocursorline  <CR>
imap <silent> <Leader>cn <Esc>:set nocursorcolumn nocursorline  <CR>a

Now if I want to turn on the cursorcolumn, I can just press \cc and when I want to turn it back off, just hit \cc again.

FYI, some details explained:

  • map – makes the key mapping work in normal mode, while “imap” makes it work in insert mode.
  • <silent> – tells vim not to echo to the statusline what its doing during the execution of the mapping.
  • <Leader> – tells vim to use my personal mapleader key to activate key mappings. I use the default key, which is the backslash (“\”), so when I want to activate one of these mappings, I press backslash, then ‘c’, then one of ‘l’, ‘c’, ‘t’, ‘o’, or ‘n’.
  • ! – an exclamation point at the end of a setting variable tells Vim to toggle the value. In otherwords, if it’s currently off, switch it on; if it’s currently on, switch it off.