Managing set options

From Vim Tips Wiki

Jump to: navigation, search
Tip 37 Printable Monobook Previous Next

created 2001 · complexity basic · author slimzhao · version 6.0


The options that configure Vim are of three types:

Options can be global, or may apply only to a particular buffer or window. :help :setlocal

A quick list of options is at :help option-list, and all the details are at :help options.

[edit] Boolean options

The many ways to set a boolean option are best illustrated by a simple example. In the following, the 'number' option is used. Any boolean option, such as wrap, linebreak, diff, etc can be set in these ways.

:set number Turn line numbers on
:set nonumber Turn line numbers off
:set invnumber
:set number!
Toggle line numbers
:set number& Set option to default value
:set number? Show value of option

Replace number with any boolean option.

For nonboolean options, :set option& also sets the option to its default value, and :set option? will display the current value.

[edit] Toggling an option

If you often need to change an option, you could use a mapping and script as in the following example (which could be placed in your vimrc). In this example, the F12 key is mapped to either enable or disable the mouse.

nnoremap <F12> :call ToggleMouse()<CR>
function! ToggleMouse()
  if &mouse == 'a'
    set mouse=
    echo "Mouse usage disabled"
  else
    set mouse=a
    echo "Mouse usage enabled"
  endif
endfunction

It's easier to toggle a boolean option, for example:

nnoremap <F12> :set number!<CR>

[edit] References

[edit] Comments

Personal tools