Tech Tip: Some Useful Vim Options


Vim is one the more popular choices of text editors for Linux. For vim, there are a few non-default options that may be useful whether editing config files or writing scripts in your favorite language.

Global options for vim are enabled in the vimrc file, which may be located in /etc or /etc/vim, depending on the distribution you are using. Note that the vimrc file uses a double quotation mark (") to start a comment.

You may find some or all of the following options useful, simply copy and paste them into your vimrc file:

syntax on            " Turn on syntax highlighting
                     " (makes code and config files more readable)
set background=dark  " If using a dark background, instead of the
                     " usual white background in Terminal
                     " (makes darker colors brighter)
set incsearch        " Used for incremental searching
                     " (useful when searching large text files)
set hlsearch         " Turns on highlighting for matched search patterns
                     " (use :nohlsearch inside vim to turn off highlights
                     " after a search)
set tabstop=4        " Sets the tab size to 4
                     " (tabs are usually 8 spaces)
set expandtab        " Tab key inserts spaces instead of tabs
set shiftwidth=4     " Sets spaces used for (auto)indent
set shiftround       " Indent to nearest tabstop
set autoindent       " Carries over previous indent to the next line

Feel free to leave a comment with your own useful vim options and macros.



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Quick :nohlsearch

Anonymous's picture

Quick way to turn off the highlights when you're done with them, just search for something you know is not there.

/a;lsdkj [enter]

It LIKELY won't find anything and turns off highlights.
Much quicker than :nohlsearch and gets the job done.

write as root

stroyan's picture

This mapping allows writing a file that requires root permissions.
If you get into a file as a non-root user and then decide to make edits, it
can be annoying to have to save to a temporary file and leave vi to use sudo.
Going beyond the normal ":w" and ":w!" commands, ":w!!" will write as root.
(It will prompt for a password if you haven't used sudo recently.)

cmap w!! set bt=nowrite :%!sudo tee "%"

Do NOT change tabstop. Leave

diarrhea's picture

Do NOT change tabstop. Leave it at 8. Otherwise a bear
to share files with someone who uses a different value.

Use shiftwidth to change your indenting, but not tabstop.


@electronjunkie (Python and tabs)

andydj's picture

Python is OK with *either* tabs *or* spaces for indents, but *not* a *mixture*. You can use any number of them to indent, as you see fit.


This is because python counts indents by character, not by print width (tabs have no "official" width associated with them.

Bad Example: (\t=tab,_=space)


for n in range(10:
\tprint n
________print num[n]

# This will fail with an indentation error, but you probably won't see it in vim. In these cases, cat -A is your friend ;-)

This is why I hate Python

The Monster's picture

Having whitespace characters define blocks is bad, m'kay?

The very fact that a tab looks like one or more spaces, but isn't treated as equivalent (other than in quoted strings) by the language interpreter/compiler, is craziness.

Are braces really that bad?

Complement to "set hlsearch "

Roxo's picture

A hint I read in a message (bad me, didn't kept the reference), I only kept the comment and the hint itself in my '~/.vimrc' :

" This is great !!
" So when I'm done, searching, I just hit CR and all highlighting turns
" off. I can still use n/N to resume searching and it will highlight
" again!
" nnoremap ^M :noh^M
nnoremap :noh

Other ViM Files

Shawn H Corey's picture

ViM has other places where its configuration files can be placed.

  • ~/.vimrc -- your personal ViM configuration file.
  • ~/.gvimrc -- your personal configuration file that only applies to GUI ViM

Using the config hierarchy.

Roxo's picture

There is an hierarchy of startup file search. The GUI (gvim) search for '~/.gvimrc', if not fount it looks for a '~/.vimrc' and if not found I think it will look for a '~/.exrc' like the classic 'vi'. The Vim will look for a '~/.vimrc' and if not found will look for a '~/.exrc'.

I use to work in a complex environment mixing Linux and Unices and some of them does not have Vim installed. Some have Vim but no GVim. My $HOME is shared using NFS, so in order to make as similar as possible my options I have a '~/.gvimrc' with few options, but the first macro is just :

:so ~/.vimrc

This way I can put the common configuration in '~/.vimrc' and only those restricted to GVim in the '~/.gvimrc'.

Likewise, my '~/.vimrc' starts with a :

:source ~/.exrc

And I can have a number of common macros shared by classical VI, the Vim and GVim.

This hierachy makes all environment very similar for me.



Lucas De Marchi's picture

I'd like to know how to enable some options based on file type. For example, I'd like to have a shiftwidth of 8 for C files and of 4 for cpp/python files.

By File Type

Mitch Frazier's picture

Add something like this to you ~/.vimrc:

:au BufRead    *.c     so ~/.vim/c.vim
:au BufNewFile *.c     so ~/.vim/c.vim
:au BufRead    *.py    so ~/.vim/py.vim
:au BufNewFile *.py    so ~/.vim/py.vim

Then put this in ~/.vim/c.vim:

set shiftwidth=8

And this in ~/.vim/py.vim:

set shiftwidth=8

That'll cause vim to source the specified file when the corresponding file type is read (or when a new file of the corresponding type is created).

Mitch Frazier is an Associate Editor for Linux Journal.

within vim....

lefty.crupps's picture

Once you're inside Vim a lot of these can be turned on or off:

:set nonu
Turn off line numbering, or
:set nu
Turn numbers on

:set noai
Turn off auto-indent
:set ai
or turn it on.

I would bet most of the above can be done the same way.

set number

Raja's picture

set number

Line numbers in vim. :)

set mouse=a rarely use it,

Anonymous's picture

set mouse=a

rarely use it, but on occasion I accidentally try to scroll using the scroll wheel and this makes that work.

Do NOT change tabstop. Leave

Anonymous's picture

Do NOT change tabstop. Leave it at 8. Otherwise a bear
to share files with someone who uses a different value.

Use shiftwidth to change your indenting, but not tabstop.

expand tab

electronjunkie's picture

Be careful w/ expand tab. It will screw up a Makefile and probably python too.

just add this to the rc

Anonymous's picture

just add this to the rc file:

au Filetype make set noexpandtab

nice one

Amit Agarwal's picture

really nice collection and information.

set expandtab set

Anonymous's picture

set expandtab
set softtabstop=4
set ai
"Map tabs like firefox
:nmap :tabprevious
:nmap :tabnext
:map :tabprevious
:map :tabnext
:imap :tabpreviousi
:imap :tabnexti
:nmap :tabnew
:imap :tabnew

Geek Guide
The DevOps Toolbox

Tools and Technologies for Scale and Reliability
by Linux Journal Editor Bill Childers

Get your free copy today

Sponsored by IBM

8 Signs You're Beyond Cron

Scheduling Crontabs With an Enterprise Scheduler
On Demand
Moderated by Linux Journal Contributor Mike Diehl

Sign up now

Sponsored by Skybot