Loading
Home ›
Tech Tip: Some Useful Vim Options
Jul 27, 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.
______________________
Trending Topics
| You Need A Budget | Feb 10, 2012 |
| The Linux powered LAN Gaming House | Feb 08, 2012 |
| Creating a vDSO: the Colonel's Other Chicken | Feb 06, 2012 |
| Your CMS Is Not Your Web Site | Feb 01, 2012 |
| Casper, the Friendly (and Persistent) Ghost | Jan 31, 2012 |
| Razor-qt 0.4 - Qt based Desktop Environment | Jan 30, 2012 |
- Fun with ethtool
- Linux-Based X Terminals with XDMCP
- 100% disappointed with the decision to go all digital.
- Readers' Choice Awards 2011
- Parallel Programming with NVIDIA CUDA
- You Need A Budget
- Validate an E-Mail Address with PHP, the Right Way
- The Linux powered LAN Gaming House
- The Linux RAID-1, 4, 5 Code
- Python for Android
- Gnome3 is such a POS. No one
2 hours 34 min ago - Gnome 3 is the biggest POS
2 hours 44 min ago - I didn't knew this thing by
8 hours 49 min ago - Author's reply
12 hours 13 min ago - Link to modlys
13 hours 20 min ago - I use YNAB because of the
13 hours 31 min ago - Search
18 hours 34 min ago - Question
18 hours 57 min ago - for the record
19 hours 18 sec ago - That's disappointing. Thanks
21 hours 23 min ago





Comments
Quick :nohlsearch
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
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
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)
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
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 "
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
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.
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?
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
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.
within vim....
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
set number
Line numbers in vim. :)
set mouse=a rarely use it,
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
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
Be careful w/ expand tab. It will screw up a Makefile and probably python too.
just add this to the rc
just add this to the rc file:
nice one
really nice collection and information.
set expandtab set
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