Vim Tips Wiki
Register
m (Code reformated + some comments have been taken into account)
(Change <tt> to <code>, perhaps also minor tweak.)
(8 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=1456
 
|id=1456
  +
|previous=1455
|title=Cache user-preferred option values for later reset
 
  +
|next=1458
|created=January 5, 2007 5:42
+
|created=2007
 
|complexity=basic
 
|complexity=basic
|author=Thomas Bader
+
|author=Thomas
 
|version=n/a
 
|version=n/a
 
|rating=1/1
 
|rating=1/1
  +
|category1=Options
|text=
 
  +
|category2=Plugin <!-- because this tip is a workaround for plugins that mess with user preferred options -->
Some plugins inadvertently(?) set global options. I have the following code at the top of my vimrc file and set all options to my preferred values using the SetOption command. Whenever I want to reset options I do :ResetOptions which resets all user-defined options previously set by SetOption.
 
 
}}
 
}}
 
Some plugins inadvertently set global options. I have the following code at the top of my vimrc, and set all options to my preferred values using the <code>SetOption</code> command. Whenever I want to reset options I do <code>:ResetOptions</code> which resets all user-defined options previously set by <code>SetOption</code>.
let s:option_preferences = []
 
 
fun! ResetOption(options)
 
if empty(a:options)
 
let options = s:option_preferences
 
else
 
let options = a:options
 
endif
 
for name in options
 
let name0 = 'g:'. name .'_default'
 
if exists(name0)
 
exec 'let &amp;'. name .' = '. name0
 
endif
 
endfor
 
endf
 
 
command! -nargs=* ResetOption :call ResetOption([&lt;f-args&gt;])
 
command! -nargs=+ SetOption let s:tmlargs=[&lt;f-args&gt;]
 
\ | for arg in s:tmlargs[1:-1]
 
\ | if arg =~ '^[+-]\?='
 
\ | exec 'set '.s:tmlargs[0] . arg
 
\ | else
 
\ | exec 'let &amp;'.s:tmlargs[0] .'='. arg
 
\ | endif
 
\ | endfor
 
\ | call add(s:option_preferences, s:tmlargs[0])
 
\ | exec 'let g:'. s:tmlargs[0] .'_default = &amp;'. s:tmlargs[0]
 
\ | unlet s:tmlargs
 
   
  +
<pre>
 
let s:option_preferences = []
 
function! ResetOption(options)
 
if empty(a:options)
 
let options = s:option_preferences
 
else
 
let options = a:options
  +
endif
 
for name in options
 
let name0 = 'g:'. name .'_default'
 
if exists(name0)
 
exec 'let &'. name .' = '. name0
 
endif
 
endfor
  +
endfunction
   
 
command! -nargs=* ResetOption :call ResetOption([<f-args>])
==Examples: ==
 
 
command! -nargs=+ SetOption let s:tmlargs=[<f-args>]
 
\ | for arg in s:tmlargs[1:-1]
 
\ | if arg =~ '^[+-]\?='
 
\ | exec 'set '.s:tmlargs[0] . arg
 
\ | else
 
\ | exec 'let &'.s:tmlargs[0] .'='. arg
 
\ | endif
 
\ | endfor
 
\ | call add(s:option_preferences, s:tmlargs[0])
 
\ | exec 'let g:'. s:tmlargs[0] .'_default = &'. s:tmlargs[0]
 
\ | unlet s:tmlargs
  +
</pre>
   
 
===Examples===
=====Add and remove specific options: =====
+
'''Add and remove specific options'''
:SetOption cpo +=my -=M
 
  +
<pre>
 
:SetOption cpo +=my -=M
  +
</pre>
   
=====Set the value: =====
+
'''Set the value'''
  +
<pre>
:SetOption ts 4
 
:SetOption ts =4
+
:SetOption ts 4
 
:SetOption ts =4
  +
</pre>
   
=====Just cache the predefined value so that it can be restored later on (in this example a later reset would be the same as :set tw&amp;): =====
+
'''Just cache the predefined value so that it can be restored later'''<br>
  +
In this example a later reset would be the same as <code>:set tw&</code>
:SetOption tw
 
  +
<pre>
 
:SetOption tw
  +
</pre>
   
=====Reset specific options: =====
+
'''Reset specific options'''
  +
<pre>
:ResetOption ts tw
+
:ResetOption ts tw
  +
</pre>
   
=====Reset all user-set options: =====
+
'''Reset all user-set options'''
  +
<pre>
:ResetOption
+
:ResetOption
  +
</pre>
   
  +
In order to monitor the options setting, I display changed values in <code>&statusline</code>.
   
  +
<pre>
In order to monitor the options setting, I display changed values in &amp;statusline. (The following code is based on some tip by somebody else or a vimrc I found years ago on the web. Don't know who originally came up with this.)
 
set statusline=%1*[%{winnr()}:%02n]%*\ %2t\ %(%M%R%H%W%k%)\ %=%{TmlStatusline()}\ %3*&lt;%l,%c%V,%p%%&gt;%*
+
set statusline=%1*[%{winnr()}:%02n]%*\ %2t\ %(%M%R%H%W%k%)\ %=%{TmlStatusline()}\ %3*<%l,%c%V,%p%%>%*
 
fu! TmlStatusline()
 
let opt = "&lt;". &amp;syntax ."/". &amp;fileformat ."&gt;"
 
if !&amp;backup | let opt=opt." no-bak" |endif
 
if !&amp;et | let opt=opt." no-et" |endif
 
if &amp;list | let opt=opt." list" |endif
 
if &amp;paste | let opt=opt." paste" | endif
 
if !&amp;expandtab | let opt=opt." tab" | endif
 
 
if &amp;ts != g:ts_default | let opt=opt.' ts='.&amp;ts | endif
 
if &amp;sw != g:sw_default | let opt=opt.' sw='.&amp;sw | endif
 
if &amp;tw != g:tw_default | let opt=opt.' tw='.&amp;tw | endif
 
if &amp;wm != g:wm_default | let opt=opt.' wm='.&amp;wm | endif
 
if &amp;enc != g:enc_default | let opt=opt.' enc='.&amp;enc | endif
 
if &amp;ve != g:ve_default | let opt=opt.' ve='. &amp;ve | endif
 
if &amp;fo != g:fo_default | let opt=opt.' fo='. &amp;fo | endif
 
if &amp;cpo != g:cpo_default | let opt=opt.' cpo='. &amp;cpo | endif
 
if &amp;bin | let opt=opt.' [bin]' | endif
 
if &amp;foldlevel != s:foldlevel | let opt=opt.' F'.&amp;foldlevel | endif
 
 
let opt=opt." | ".strftime("%d-%b-%Y %H:%M")
 
 
return opt
 
endf
 
   
 
function! TmlStatusline()
It could have been this one by Thomas Bader: http://www.trash.net/~thomasb/files/vim/dotvimrc.html
 
 
let opt = "<". &syntax ."/". &fileformat .">"
 
if !&backup | let opt=opt." no-bak" |endif
 
if !&et | let opt=opt." no-et" |endif
 
if &list | let opt=opt." list" |endif
 
if &paste | let opt=opt." paste" | endif
 
if !&expandtab | let opt=opt." tab" | endif
 
if &ts != g:ts_default | let opt=opt.' ts='.&ts | endif
 
if &sw != g:sw_default | let opt=opt.' sw='.&sw | endif
 
if &tw != g:tw_default | let opt=opt.' tw='.&tw | endif
 
if &wm != g:wm_default | let opt=opt.' wm='.&wm | endif
 
if &enc != g:enc_default | let opt=opt.' enc='.&enc | endif
 
if &ve != g:ve_default | let opt=opt.' ve='. &ve | endif
 
if &fo != g:fo_default | let opt=opt.' fo='. &fo | endif
 
if &cpo != g:cpo_default | let opt=opt.' cpo='. &cpo | endif
 
if &bin | let opt=opt.' [bin]' | endif
 
if &foldlevel != s:foldlevel | let opt=opt.' F'.&foldlevel | endif
 
let opt=opt." | ".strftime("%d-%b-%Y %H:%M")
 
return opt
  +
endfunction
  +
</pre>
   
 
==Comments==
 
Here is a slightly modified version:
   
  +
<pre>
== Comments ==
 
 
let s:options = {}
Here a slightly modified version:
 
 
function! ResetOption(options)
 
if empty(a:options)
 
let options = keys(s:options)
 
else
  +
let options = a:options
  +
endif
 
for name in options
  +
exec 'let &'. name .' = s:options[name]'
 
endfor
  +
endfunction
   
 
command! -nargs=* ResetOption :call ResetOption([<f-args>])
let s:options = {}
 
 
command! -nargs=+ SetOption let s:tmlargs=[<f-args>]
 
 
\ | for arg in s:tmlargs[1:-1]
fun! ResetOption(options)
 
 
\ | if arg =~ '^[+-]\?='
if empty(a:options)
 
let options = keys(s:options)
+
\ | exec 'set '.s:tmlargs[0] . arg
else
+
\ | else
 
\ | exec 'let &'.s:tmlargs[0] .'='. arg
let options = a:options
 
endif
+
\ | endif
 
\ | endfor
for name in options
 
exec 'let &amp;'. name .' = s:options[name]'
+
\ | exec 'let s:options[s:tmlargs[0]] = &'. s:tmlargs[0]
 
\ | unlet s:tmlargs
endfor
 
endf
 
 
command! -nargs=* ResetOption :call ResetOption([&lt;f-args&gt;])
 
command! -nargs=+ SetOption let s:tmlargs=[&lt;f-args&gt;]
 
\ | for arg in s:tmlargs[1:-1]
 
\ | if arg =~ '^[+-]\?='
 
\ | exec 'set '.s:tmlargs[0] . arg
 
\ | else
 
\ | exec 'let &amp;'.s:tmlargs[0] .'='. arg
 
\ | endif
 
\ | endfor
 
\ | exec 'let s:options[s:tmlargs[0]] = &amp;'. s:tmlargs[0]
 
\ | unlet s:tmlargs
 
 
let s:option_labels = {'fdl': 'F'}
 
fun! TmlStatusline()
 
let opt = "&lt;". &amp;syntax ."/". &amp;fileformat ."&gt;"
 
 
if !&amp;backup | let opt=opt." no-bak" |endif
 
if !&amp;et | let opt=opt." no-et" |endif
 
if &amp;list | let opt=opt." list" |endif
 
if &amp;paste | let opt=opt." paste" | endif
 
if !&amp;expandtab | let opt=opt." tab" | endif
 
 
for [o, v] in items(s:options)
 
exec 'let oo = &amp;'.o
 
if oo != v
 
let opt .= ' '. (has_key(s:option_labels, o) ? s:option_labels[o] : o.'=') . oo
 
endif
 
endfor
 
 
if &amp;bin | let opt=opt.' [bin]' | endif
 
if exists('b:compressed') | let opt=opt.' ['.b:compressed.']' | endif
 
let opt=opt." | ".strftime("%d-%b-%Y %H:%M")
 
 
return opt
 
endf
 
   
 
let s:option_labels = {'fdl': 'F'}
 
function! TmlStatusline()
 
let opt = "<". &syntax ."/". &fileformat .">"
 
if !&backup | let opt=opt." no-bak" |endif
 
if !&et | let opt=opt." no-et" |endif
 
if &list | let opt=opt." list" |endif
 
if &paste | let opt=opt." paste" | endif
 
if !&expandtab | let opt=opt." tab" | endif
 
for [o, v] in items(s:options)
 
exec 'let oo = &'.o
 
if oo != v
 
let opt .= ' '. (has_key(s:option_labels, o) ? s:option_labels[o] : o.'=') . oo
 
endif
 
endfor
 
if &bin | let opt=opt.' [bin]' | endif
 
if exists('b:compressed') | let opt=opt.' ['.b:compressed.']' | endif
 
let opt=opt." | ".strftime("%d-%b-%Y %H:%M")
 
return opt
  +
endfunction
  +
</pre>
   
'''Anonymous'''
 
, January 5, 2007 6:39
 
 
----
 
----
<!-- parsed by vimtips.py in 0.506412 seconds-->
 

Revision as of 06:26, 13 July 2012

Tip 1456 Printable Monobook Previous Next

created 2007 · complexity basic · author Thomas · version n/a


Some plugins inadvertently set global options. I have the following code at the top of my vimrc, and set all options to my preferred values using the SetOption command. Whenever I want to reset options I do :ResetOptions which resets all user-defined options previously set by SetOption.

let s:option_preferences = []
function! ResetOption(options)
  if empty(a:options)
    let options = s:option_preferences
  else
    let options = a:options
  endif
  for name in options
    let name0 = 'g:'. name .'_default'
    if exists(name0)
      exec 'let &'. name .' = '. name0
    endif
  endfor
endfunction

command! -nargs=* ResetOption :call ResetOption([<f-args>])
command! -nargs=+ SetOption let s:tmlargs=[<f-args>]
 \ | for arg in s:tmlargs[1:-1]
 \ |   if arg =~ '^[+-]\?='
 \ |     exec 'set '.s:tmlargs[0] . arg
 \ |   else
 \ |     exec 'let &'.s:tmlargs[0] .'='. arg
 \ |   endif
 \ | endfor
 \ | call add(s:option_preferences, s:tmlargs[0])
 \ | exec 'let g:'. s:tmlargs[0] .'_default = &'. s:tmlargs[0]
 \ | unlet s:tmlargs

Examples

Add and remove specific options

:SetOption cpo +=my -=M

Set the value

:SetOption ts 4
:SetOption ts =4

Just cache the predefined value so that it can be restored later
In this example a later reset would be the same as :set tw&

:SetOption tw

Reset specific options

:ResetOption ts tw

Reset all user-set options

:ResetOption

In order to monitor the options setting, I display changed values in &statusline.

set statusline=%1*[%{winnr()}:%02n]%*\ %2t\ %(%M%R%H%W%k%)\ %=%{TmlStatusline()}\ %3*<%l,%c%V,%p%%>%*

function! TmlStatusline()
  let opt = "<". &syntax ."/". &fileformat .">"
  if !&backup | let opt=opt." no-bak" |endif
  if !&et | let opt=opt." no-et" |endif
  if &list | let opt=opt." list" |endif
  if &paste | let opt=opt." paste" | endif
  if !&expandtab | let opt=opt." tab" | endif
  if &ts != g:ts_default | let opt=opt.' ts='.&ts | endif
  if &sw != g:sw_default | let opt=opt.' sw='.&sw | endif
  if &tw != g:tw_default | let opt=opt.' tw='.&tw | endif
  if &wm != g:wm_default | let opt=opt.' wm='.&wm | endif
  if &enc != g:enc_default | let opt=opt.' enc='.&enc | endif
  if &ve != g:ve_default | let opt=opt.' ve='. &ve | endif
  if &fo != g:fo_default | let opt=opt.' fo='. &fo | endif
  if &cpo != g:cpo_default | let opt=opt.' cpo='. &cpo | endif
  if &bin | let opt=opt.' [bin]' | endif
  if &foldlevel != s:foldlevel | let opt=opt.' F'.&foldlevel | endif
  let opt=opt." | ".strftime("%d-%b-%Y %H:%M")
  return opt
endfunction

Comments

Here is a slightly modified version:

let s:options = {}
function! ResetOption(options)
  if empty(a:options)
    let options = keys(s:options)
  else
    let options = a:options
  endif
  for name in options
    exec 'let &'. name .' = s:options[name]'
  endfor
endfunction

command! -nargs=* ResetOption :call ResetOption([<f-args>])
command! -nargs=+ SetOption let s:tmlargs=[<f-args>]
 \ | for arg in s:tmlargs[1:-1]
 \ |   if arg =~ '^[+-]\?='
 \ |     exec 'set '.s:tmlargs[0] . arg
 \ |   else
 \ |     exec 'let &'.s:tmlargs[0] .'='. arg
 \ |   endif
 \ | endfor
 \ | exec 'let s:options[s:tmlargs[0]] = &'. s:tmlargs[0]
 \ | unlet s:tmlargs

let s:option_labels = {'fdl': 'F'}
function! TmlStatusline()
  let opt = "<". &syntax ."/". &fileformat .">"
  if !&backup | let opt=opt." no-bak" |endif
  if !&et | let opt=opt." no-et" |endif
  if &list | let opt=opt." list" |endif
  if &paste | let opt=opt." paste" | endif
  if !&expandtab | let opt=opt." tab" | endif
  for [o, v] in items(s:options)
    exec 'let oo = &'.o
    if oo != v
      let opt .= ' '. (has_key(s:option_labels, o) ? s:option_labels[o] : o.'=') . oo
    endif
  endfor
  if &bin | let opt=opt.' [bin]' | endif
  if exists('b:compressed') | let opt=opt.' ['.b:compressed.']' | endif
  let opt=opt." | ".strftime("%d-%b-%Y %H:%M")
  return opt
endfunction