Duplicate tip

This tip is very similar to the following:

These tips need to be merged – see the merge guidelines.

Tip 472 Printable Monobook Previous Next

created May 12, 2003 · complexity basic · author robin · version 6.0

Here's a little function I put together to make some of my mappings easier to read, understand and change.

function ToggleFlag(option,flag)
  exec ('let lopt = &' . a:option)
  if lopt =~ (".*" . a:flag . ".*")
    exec ('set ' . a:option . '-=' . a:flag)
    exec ('set ' . a:option . '+=' . a:flag)

Examples of use:

map <silent> <F8> :call ToggleFlag("guioptions","m")<CR>
map <silent> <F9> :call ToggleFlag("guioptions","T")<CR>

Comments[edit | edit source]

The following might be more flexible (I think it should work for any flag-style option).

" my function to cycle a numeric option
function CycleNum(option,min,inc,max)
  exec ('let tz_value = (((&'.a:option.'-'.a:min.')+'.a:inc.')%(('.a:max.'-'.a:min.')+'.a:inc.'))+'.a:min)
  if (tz_value < a:min) " in case inc<0
    let tz_value = tz_value+a:max
  exec ('setlocal '.a:option.'='.tz_value)

" my function to toggle an option flag
function ToggleFlag(option,flag)
  exec ('let tf_o = &'.a:option)
  exec ('setlocal '.a:option.'-='.a:flag)
  exec ('let tf_t = &'.a:option)
  if (tf_o == tf_t)
    exec ('setlocal '.a:option.'+='.a:flag)

" Toggle folding column
noremap <silent> <F7> :call CycleNum("foldcolumn",0,2,6)<BAR>set foldcolumn?<CR>
imap <F7> <C-O><F7>

" Toggle window appearance
noremap <silent> <F8> :call ToggleFlag("guioptions","m")<BAR>set guioptions?<CR>
imap <F8> <C-O><F8>
noremap <silent> <F9> :call ToggleFlag("guioptions","T")<BAR>set guioptions?<CR>
imap <F9> <C-O><F9>

" Cycle tabstop
noremap <silent> <M-t>s :call CycleNum("tabstop",4,4,8)<BAR>set tabstop?<CR>
" Cycle shiftwidth
noremap <silent> <M-t>w :call CycleNum("shiftwidth",4,4,8)<BAR>set shiftwidth?<CR>

Community content is available under CC-BY-SA unless otherwise noted.