Wikia

Vim Tips Wiki

Changes: Handy option flag toggler

Edit

Back to page

(Move categories to tip template)
(Remove html character entities)
 
Line 17: Line 17:
 
<pre>
 
<pre>
 
function ToggleFlag(option,flag)
 
function ToggleFlag(option,flag)
exec ('let lopt = &amp;' . a:option)
+
exec ('let lopt = &' . a:option)
 
if lopt =~ (".*" . a:flag . ".*")
 
if lopt =~ (".*" . a:flag . ".*")
 
exec ('set ' . a:option . '-=' . a:flag)
 
exec ('set ' . a:option . '-=' . a:flag)
Line 28: Line 28:
 
Examples of use:
 
Examples of use:
 
<pre>
 
<pre>
map &lt;silent&gt; &lt;F8&gt; :call ToggleFlag("guioptions","m")&lt;CR&gt;
+
map <silent> <F8> :call ToggleFlag("guioptions","m")<CR>
map &lt;silent&gt; &lt;F9&gt; :call ToggleFlag("guioptions","T")&lt;CR&gt;
+
map <silent> <F9> :call ToggleFlag("guioptions","T")<CR>
 
</pre>
 
</pre>
   
Line 38: Line 38:
 
" my function to cycle a numeric option
 
" my function to cycle a numeric option
 
function CycleNum(option,min,inc,max)
 
function CycleNum(option,min,inc,max)
exec ('let tz_value = (((&amp;'.a:option.'-'.a:min.')+'.a:inc.')%(('.a:max.'-'.a:min.')+'.a:inc.'))+'.a:min)
+
exec ('let tz_value = (((&'.a:option.'-'.a:min.')+'.a:inc.')%(('.a:max.'-'.a:min.')+'.a:inc.'))+'.a:min)
if (tz_value &lt; a:min) " in case inc&lt;0
+
if (tz_value < a:min) " in case inc<0
 
let tz_value = tz_value+a:max
 
let tz_value = tz_value+a:max
 
endif
 
endif
Line 47: Line 47:
 
" my function to toggle an option flag
 
" my function to toggle an option flag
 
function ToggleFlag(option,flag)
 
function ToggleFlag(option,flag)
exec ('let tf_o = &amp;'.a:option)
+
exec ('let tf_o = &'.a:option)
 
exec ('setlocal '.a:option.'-='.a:flag)
 
exec ('setlocal '.a:option.'-='.a:flag)
exec ('let tf_t = &amp;'.a:option)
+
exec ('let tf_t = &'.a:option)
 
if (tf_o == tf_t)
 
if (tf_o == tf_t)
 
exec ('setlocal '.a:option.'+='.a:flag)
 
exec ('setlocal '.a:option.'+='.a:flag)
Line 56: Line 56:
   
 
" 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>
 
</pre>
   

Latest revision as of 08:46, September 29, 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>

CommentsEdit

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>

Around Wikia's network

Random Wiki