(Remove html character entities)
 
(3 intermediate revisions by one other user not shown)
Line 1: Line 1:
  +
{{duplicate|920}}
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=472
 
|id=472
  +
|previous=471
|title=Handy option flag toggler
 
  +
|next=473
|created=May 12, 2003 9:52
+
|created=May 12, 2003
 
|complexity=basic
 
|complexity=basic
|author=robin at isometry dot net
+
|author=robin
 
|version=6.0
 
|version=6.0
 
|rating=14/5
 
|rating=14/5
  +
|category1=Options
|text=
 
  +
|category2=
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)
 
 
else
 
 
exec ('set ' . a:option . '+=' . a:flag)
 
 
endif
 
 
endfunction
 
 
 
 
Examples of use:
 
 
 
 
map <silent> <F8> :call ToggleFlag("guioptions","m")<CR>
 
 
map <silent> <F9> :call ToggleFlag("guioptions","T")<CR>
 
 
 
 
Can anyone see anyway to improve it?
 
 
e.g. remove the leading exec... "if &{a:option}..." doesn't work.
 
 
e.g. a regex match doesn't seem the cleanest of checks, though I prefer it to setting a variable for each possible flag.
 
 
}}
 
}}
 
Here's a little function I put together to make some of my mappings easier to read, understand and change.
   
  +
<pre>
== Comments ==
 
 
function ToggleFlag(option,flag)
Hmmm. The following might be more flexible (I think it should work for any flag-style option.
 
 
exec ('let lopt = &' . a:option)
 
if lopt =~ (".*" . a:flag . ".*")
 
exec ('set ' . a:option . '-=' . a:flag)
  +
else
 
exec ('set ' . a:option . '+=' . a:flag)
  +
endif
 
endfunction
  +
</pre>
   
 
Examples of use:
function ToggleFlag(option,flag)
 
  +
<pre>
exec ('let tf_old = &amp;' . a:option)
 
 
map <silent> <F8> :call ToggleFlag("guioptions","m")<CR>
exec ('set ' . a:option . '-=' . a:flag)
 
 
map <silent> <F9> :call ToggleFlag("guioptions","T")<CR>
exec ('let tf_new = &amp;' . a:option)
 
  +
</pre>
if (tf_o == tf_t)
 
exec ('set ' . a:option . '+=' . a:flag)
 
endif
 
endfunction
 
   
 
==Comments==
And here's another for valued rather than flag options, such as foldcolumn:
 
 
The following might be more flexible (I think it should work for any flag-style option).
   
  +
<pre>
function CycleNum(option,min,inc,max)
 
 
" my function to cycle a numeric option
exec ('let tz_value = ((&amp;'.a:option.'+'.a:inc.'-'.a:min.')%('.a:max.'+'.a:inc.'))+'.a:min)
 
 
function CycleNum(option,min,inc,max)
if (tz_value &lt; a:min) " in case inc&lt;0
 
 
exec ('let tz_value = (((&'.a:option.'-'.a:min.')+'.a:inc.')%(('.a:max.'-'.a:min.')+'.a:inc.'))+'.a:min)
let tz_value = tz_value+a:max
 
 
if (tz_value < a:min) " in case inc<0
endif
 
exec ('set '.a:option.'='.tz_value)
+
let tz_value = tz_value+a:max
  +
endif
endfunction
 
 
exec ('setlocal '.a:option.'='.tz_value)
 
 
endfunction
e.g.
 
map &lt;silent&gt; &lt;F6&gt; :call CycleNum("foldcolumn",0,2,4)&lt;CR&gt;
 
 
robin at isometry dot net
 
, May 12, 2003 17:53
 
----
 
Here are my revised functions, and mappings:
 
   
" my function to cycle a numeric option
+
" my function to toggle an option flag
function CycleNum(option,min,inc,max)
+
function ToggleFlag(option,flag)
exec ('let tz_value = (((&amp;'.a:option.'-'.a:min.')+'.a:inc.')%(('.a:max.'-'.a:min.')+'.a:inc.'))+'.a:min)
+
exec ('let tf_o = &'.a:option)
 
exec ('setlocal '.a:option.'-='.a:flag)
if (tz_value &lt; a:min) " in case inc&lt;0
 
let tz_value = tz_value+a:max
+
exec ('let tf_t = &'.a:option)
 
if (tf_o == tf_t)
endif
 
exec ('setlocal '.a:option.'='.tz_value)
+
exec ('setlocal '.a:option.'+='.a:flag)
  +
endif
endfunction
 
 
endfunction
" my function to toggle an option flag
 
function ToggleFlag(option,flag)
 
exec ('let tf_o = &amp;'.a:option)
 
exec ('setlocal '.a:option.'-='.a:flag)
 
exec ('let tf_t = &amp;'.a:option)
 
if (tf_o == tf_t)
 
exec ('setlocal '.a:option.'+='.a:flag)
 
endif
 
endfunction
 
   
" Toggle folding column
+
" Toggle folding column
noremap &lt;silent&gt; &lt;F7&gt; :call CycleNum("foldcolumn",0,2,6)&lt;BAR&gt;set foldcolumn?&lt;CR&gt;
+
noremap <silent> <F7> :call CycleNum("foldcolumn",0,2,6)<BAR>set foldcolumn?<CR>
imap &lt;F7&gt; &lt;C-O&gt;&lt;F7&gt;
+
imap <F7> <C-O><F7>
   
" Toggle window appearance
+
" Toggle window appearance
noremap &lt;silent&gt; &lt;F8&gt; :call ToggleFlag("guioptions","m")&lt;BAR&gt;set guioptions?&lt;CR&gt;
+
noremap <silent> <F8> :call ToggleFlag("guioptions","m")<BAR>set guioptions?<CR>
imap &lt;F8&gt; &lt;C-O&gt;&lt;F8&gt;
+
imap <F8> <C-O><F8>
noremap &lt;silent&gt; &lt;F9&gt; :call ToggleFlag("guioptions","T")&lt;BAR&gt;set guioptions?&lt;CR&gt;
+
noremap <silent> <F9> :call ToggleFlag("guioptions","T")<BAR>set guioptions?<CR>
imap &lt;F9&gt; &lt;C-O&gt;&lt;F9&gt;
+
imap <F9> <C-O><F9>
   
" Cycle tabstop
+
" Cycle tabstop
noremap &lt;silent&gt; &lt;M-t&gt;s :call CycleNum("tabstop",4,4,8)&lt;BAR&gt;set tabstop?&lt;CR&gt;
+
noremap <silent> <M-t>s :call CycleNum("tabstop",4,4,8)<BAR>set tabstop?<CR>
" Cycle shiftwidth
+
" Cycle shiftwidth
noremap &lt;silent&gt; &lt;M-t&gt;w :call CycleNum("shiftwidth",4,4,8)&lt;BAR&gt;set shiftwidth?&lt;CR&gt;
+
noremap <silent> <M-t>w :call CycleNum("shiftwidth",4,4,8)<BAR>set shiftwidth?<CR>
  +
</pre>
   
robin at isometry dot net
 
, May 16, 2003 1:10
 
 
----
 
----
<!-- parsed by vimtips.py in 0.503925 seconds-->
 

Latest revision as of 08:46, 29 September 2008

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)
  else
    exec ('set ' . a:option . '+=' . a:flag)
  endif
endfunction

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
  endif
  exec ('setlocal '.a:option.'='.tz_value)
endfunction

" 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)
  endif
endfunction

" 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.