Skip to content

Switch boolean on line and return

Branislav Gerazov edited this page Apr 26, 2021 · 1 revision

This function can be used to switch between booleans anywhere on the line and returning to original cursor position:

  • TrueFalse and FalseTrue,
  • truefalse and falsetrue,
  • 10 and 01.
function! ToggleBool()
   let save_pos = getpos(".")
   normal! 0

   if search('\c\<true\>\|\<false\>', '', line('.')) > 0
       Switch
   elseif search('\<1\>', '', line('.')) > 0
       execute 's/\<1\>/0/'
   elseif search('\<0\>', '', line('.')) > 0
       execute 's/\<0\>/1/'
   endif

   call setpos(".", save_pos)
endfunction

command! -nargs=0 ToggleBool call ToggleBool()

Clone this wiki locally