In my $VIMRC I have:

nnoremap <leader>A :set formatoptions+=a<cr>
nnoremap <leader>a :set formatoptions-=a<cr>

What I'm wondering is if there's something more like:

nnoremap <leader>A :set formatoptions!=a<cr>

that takes the value of a (the formatoptions flag) and inverts it (if set, unset it, otherwise set it).

This would be like

nnoremap <leader>s :set spell!<cr>

but for options where it takes flags rather than it being a true/false boolean. I'd like to accomplish this without getting into vimscript and if/else loops.

1 Answer 1


There is no built-in way to do this in vimscript that I'm aware of. You could put it in a function:

function ToggleOption(option)
  if &formatoptions =~# a:option
    exec "set formatoptions-=".a:option
    exec "set formatoptions+=".a:option

nnoremap <leader>A :<C-u>call ToggleOption('a')<cr>

But if you really want this as a one liner, you'd need something like this:

nnoremap <expr> <leader>a ":\<C-u>set fo".'+-'[&fo =~# 'a']."=a\<CR>"

In my opinion, this is kinda ugly and the function is much preferable.

  • Special +1 for the oneliner. I love oneliners.
    – anishsane
    May 9, 2023 at 7:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.