Tech Tip: Some Useful Vim Options
July 27th, 2009 by Giovanni Torres in
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.
__________________________
Special Magazine Offer -- Free Gift with Subscription
Receive a free digital copy of Linux Journal's System Administration Special Edition as well as instant online access to current and past issues. CLICK HERE for offer
Linux Journal: delivering readers the advice and inspiration they need to get the most out of their Linux systems since 1994.
Subscribe now!
The Latest
Newsletter
Tech Tip Videos
- Nov-19-09
- Nov-04-09
Recently Popular
From the Magazine
December 2009, #188
If last month's Infrastrucuture issue was too "big" for you then try on this month's Embedded issue. Find out how to use Player for programming mobile robots, build a humidity controller for your root cellar, find out how to reduce the boot time of your embedded system, and if you're new to embedded systems find out the basics that go into one. You can also read about the Beagle Board, the Mesh Potato and a spate of other interestingly named items. And along with our regular columns don't miss our new monthly column: Economy Size Geek.
Delicious
Digg
StumbleUpon
Reddit
Facebook








Quick :nohlsearch
On August 12th, 2009 Anonymous (not verified) says:
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
On August 3rd, 2009 stroyan (not verified) says:
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
On July 30th, 2009 diarrhea (not verified) says:
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.
Daniel
@electronjunkie (Python and tabs)
On July 29th, 2009 andydj (not verified) says:
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.
HOWEVER: NEVER MIX TABS AND SPACES.
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)
num={0:"zero",1:"one",2:"two",3:"three",4:"four",5:"five",
_____6:"six",7:"seven",8:"eight",9:"nine"}
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
On July 30th, 2009 The Monster (not verified) says:
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 "
On July 28th, 2009 Roxo (not verified) says:
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
On July 28th, 2009 Shawn H Corey (not verified) says:
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 ViMUsing the config hierarchy.
On July 28th, 2009 Roxo (not verified) says:
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.
HTH
filetype?
On July 28th, 2009 Lucas De Marchi (not verified) says:
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
On July 28th, 2009 Mitch Frazier says:
Add something like this to you ~/.vimrc:
Then put this in ~/.vim/c.vim:
And this in ~/.vim/py.vim:
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 and the Web Editor for linuxjournal.com.
within vim....
On July 28th, 2009 lefty.crupps (not verified) says:
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
On July 27th, 2009 Raja (not verified) says:
set number
Line numbers in vim. :)
set mouse=a rarely use it,
On July 27th, 2009 Anonymous (not verified) says:
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
On July 27th, 2009 Anonymous (not verified) says:
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
On July 27th, 2009 electronjunkie (not verified) says:
Be careful w/ expand tab. It will screw up a Makefile and probably python too.
just add this to the rc
On August 1st, 2009 Anonymous (not verified) says:
just add this to the rc file:
nice one
On July 27th, 2009 Amit Agarwal (not verified) says:
really nice collection and information.
set expandtab set
On July 27th, 2009 Anonymous (not verified) says:
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
Post new comment