2

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.

New contributor
Harv is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
4

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
  else
    exec "set formatoptions+=".a:option
  endif
endfunction

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.

Your Answer

Harv is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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