How to set/unset options in Vim

Vim is a very powerful text editor and as such it has lots of options which allows you to configure it “just right” for you! In order to configure it you can set up values to its various options either in the current window you are working on or in configuration file so that every new instance launched will have common settings. This file is usually called vimrc and is located under your home catalog.

But what happens if you wish to reset the value of an option you have configured in this configuration file for just this window that you are currently working in? I found that to be more difficult than expected so, here is how you can:

  1. Start a clean instance of vim with all its default options that ignores all the settings in all the user defined configuration files
  2. Get the currently set options and their values
  3. Disable/reset options that I will call switches
  4. Reset any option to each default value (whatever that might be)

Start a clean instance of vim

In order to start a fresh, clean, default vim instance you have to start your editor with the option -u set to NONE. From the vim man page you can see what this will do:

-u {vimrc}  Use  the  commands in the file {vimrc} for initializations.  All the other initializations are skipped.  Use this to edit a special kind of files.  It can also be used to skip all initializations by giving the name “NONE”.  See “:help initialization” within vim for more details.

So, let’s navigate to our tmp directory and start editing a new test.txt file with our vim editor without loading any of the predefined options in our configuration files:

/$ cd /tmp
/tmp$ vi -u NONE test.txt

Get the currently set options

Vim set command output

Now in order to see all the options that are currently set in our vim instance just type in the set command and hit Enter. In my local install the output of this command looks like this:

Disable/reset “switch” options

Next we will set an option that is either On or Off (which belongs to the category I call switches). The specific option enumerates the lines present in the file that you are currently editing and is called numbers. In order to enable any such option you simply have to execute the command set again followed by the name of the option which you wish to enable. In this case we would execute the following:

:set number

In you now wish to disable the use of numbers you need to specify that by issuing the following:

:set nonumber

In general any option that is a switch can be disabled by using the no word just before the name of the option (Do not use spaces between no and the option otherwise you will receive an error saying “E518: Unknown option: no”). Now the lines should no longer be there!

Reset any option to each default value

Finally, we will use the colorcolumn option to demonstrate how to reset any option to its default value. The colorcolumn option outputs a different color at the specified column in your vim editor. I find this setting particularly interesting while writing code for example that you do not wish to expand more than the 80 column limit. You can the color of the 81st column by issuing the following:

:set colorcolumn=81

Which will now make your editor look something like this:

VIm editor with the colorcolumn option set

Now how can we disable this setting? This setting is not a switch, it has a value assigned to it so typing in set nocolorcolumn will simply throw an error because vim knows nothing about an option called nocolorcolumn. The way to reset this option to its original default value is to type in ONE of the following three:

:set colorcolumn&
:set colorcolumn&vi
:set colorcolumn&vim

Which one you choose depends on what compatibility settings you have. If by default you prefer vim for example then using the first line will reset it to whatever vim considers to be the default value.

Leave a Reply