linuxnewbie.org.gif
Tuesday, 12-Dec-2000 10:32:05 EST
Newbized Help Files articles discussion board bookshelf sensei's log advertising info
Customizing vim
Written By: Danny "Strike" DiPaolo

This article is intended for the following types of Linux users:

  • programmers (from novices to hobbyists to seasoned coders)
  • people who use markup languages like HTML and/or LaTeX
  • anybody who really loves their vim and wants to make it even better

That being said, I'm going to assume you are one of the above people if you are reading this article. However, keep in mind that I use vim for all of the above reasons, so disregard anything that you think may not apply to your needs.

This article will also assume that you are at least fairly familiar with the vim editor. I'm not sure how far back this NHF will apply in terms of the format of the config files, but I am using 5.6.11 right now (5.7 is the most recent, I believe). If you are unsure, just get a new version by navigating their website at http://www.vim.org . But, for example, you ought to know that vim is not a mode-less editor, and you should at least be somewhat familiar with both command mode and input mode as well as entering commands at the vim prompt (the colon [:], like :wq for writing and quitting).

---Organization---

Note - what I'm suggesting here is exactly that, a suggestion, nothing more. There may be better ways of organizing things, but this is the way I like to do it because it fits in well with the rest of my directory structure.

First of all, there is one file whose place is not at all optional. This is your .vimrc file. This MUST be in your home directory. This file will also pretty much be the key to all of our customizations. So, in case I haven't gotten this point across - this file is IMPORTANT. If you want to keep your customizations, I'd suggest backing it up somewhere in case of a power failure which crashes your system or in case you want to move it to another system.

However, there are other configuration files that we will be using. These begin to grow in number pretty quickly, and I don't like having a lot of unsightly dot-files (files beginning with a .) in my home directory - there are already plenty of dot-directories - so what I did was that I created a directory that was designed specifically to hold my other vim configuration files.

I called this directory .vim-files because it's a good descriptive name and it doesn't show up on a simple ls (configuration stuff like that really should not in the first place). Every other file I will talk about will either reside in this directory or a subdirectory of that directory.

To create this directory, issue the following commands:
cd
mkdir .vim-files

The first cd will just put you in your home directory, and the second command actually creates the directory.

Once that is set up, we are ready to start digging into the .vimrc for some serious configuration goodness!

---The ~/.vimrc file---


Before we get too deeply involved, I want to give you an idea of just how configurable vim is. So, fire up a session of vim in a terminal window. Get to the colon prompt and type "set " (with a space) and then hit <TAB>. If you weren't aware of this feature before, vim has tab-autocompletion for all of its commands as well as filenames and such. But, you should get something like "set all" (that's what I get). Hit <TAB> again, and again, and again. Hold down <TAB> for a while. What you see whizzing by are all the different options that you can set for vim. Some of them are usually pretty obvious and have reasonable defaults. Some of them are highly dependent upon what the user is accustomed to. Some of them are highly esoteric and generally aren't ever even worried about. But, sure enough, there are a lot of them.

If you want to take full control of your vim sessions, here is what I suggest you do before proceeding. Do the procedure I outlined above, but more slowly - read each option as it goes by, and if there are ones that you are interested in using for your configuration, jot them down. The same goes for ones you aren't sure of the meaning for (like "what does 'set cscopeprg' mean?"). Once you have these lists, get back to a colon prompt and just type ":h " and then the option that you were curious about. ":h" is the online help in vim. It is VERY extensive, and you are highly encouraged to poke around the help files they provide. Make sure that you keep a list of things you are interested in setting as well as any particular values that you need to set them as. This is essentially what I did to create my own .vimrc, and it has paid off.

Of course, these are just options to be set. We will be doing a little bit more than that in this NHF, because ... well, because we can and because it makes vim a much more powerful tool.

Now, I'm sure that if you are one of the people I described as the type who should be reading this NHF that you have at least had at least a little bit of experience with configuration files like this before. But, one way that this one is a little different is that it can basically be thought of as a script. Honestly, I could delete my .vimrc file from my computer if I was willing to enter all 40+ commands manually at the colon prompt every time I started up a session of vim. But, trust me, that won't happen. However, the fact that vim simply executes all of your .vimrc commands at the startup as if you were entering them manually is helpful to us because of the feature I mentioned earlier - command tab-autocompletion. This, combined with the very extensive online help that vim provides will make you able to take what I show you here and add any other interesting command that you want.

I know what you are thinking - you want to go ahead and dig in already, right? Good, me too. There's just one more thing I have left to mention before I show you all my current ~/.vimrc file, and that is the comment character. The comment character in the ~/.vimrc file is the double quotation mark ("). Every character after that one on a line is considered a comment and will not be part of the script. There are no block comments in ~/.vimrc files.

Okay, enough of that, here is my lovely ~/.vimrc file:

----------------------------

" Formatting
set tabstop=4                   " tabs are 4 spaces
set bs=2                                " backspace over everything in
insert mode
set smartindent                 " does C-style stuff

" Mouse stuff
set mousehide                   " hides mouse after characters are typed
set mousefocus                  " no real reason for this
set mouse=a                             " mouse in all modes
set mousem=popup                " Right mouse button pops up a menu.
Shift-left extends selection      

" Other stuff
set autowrite                   " writes on make and shell commands, etc
set     ruler                           " Turn the ruler on
set nohlsearch                  " Highlighting found search items is
annoying
set nocompatible                " vi compatibility is weak

" set suffixes to ignore in command-line completion
set
suffixes=.bak,~,.o,.h,.info,.swp,.aux,.bbl,.blg,.dvi,.lof,.log,.lot,.ps,.toc

" run ispell on current file
" map #fi :w:!ispell %:e %
" 
" I'm not sure what key that is mapping to, or if it is just the key
" sequence specified ..

" Remove ALL auto-commands.  This avoids having the autocommands twice
" in case the .vimrc file gets sourced more than once.
autocmd!

" LaTeX autocmds
autocmd BufRead                 *.tex   source ~/.vim-files/.vimrc.latex
autocmd BufNewFile              *.tex   source ~/.vim-files/.vimrc.latex
" C autocmds
autocmd BufRead                 *.c             source
~/.vim-files/.vimrc.c
autocmd BufNewFile              *.c             source
~/.vim-files/.vimrc.c
" asm autocmds
autocmd BufRead                 *.s,*.S,*.asm   source
~/.vim-files/.vimrc.asm
autocmd BufNewFile              *.s,*.S,*.asm   0r
~/.vim-files/skeletons/skel.asm      
autocmd BufNewFile              *.s,*.S,*.asm   source
~/.vim-files/.vimrc.asm
" HTML autocmds
autocmd BufRead                 *.htm,*.html    source
~/.vim-files/.vimrc.html
autocmd BufNewFile              *.htm,*.html    0r
~/.vim-files/skeletons/skel.html
autocmd BufNewFile              *.htm,*.html    source
~/.vim-files/.vimrc.html
" Perl autocmds
autocmd BufRead                 *.pl    source  ~/.vim-files/.vimrc.perl
autocmd BufNewFile              *.pl    0r ~/.vim-files/skeletons/skel.pl
autocmd BufNewFile              *.pl    source ~/.vim-files/.vimrc.perl
autocmd BufWrite                *.pl    !chmod +x %
" Java autocmds
autocmd BufRead                 *.java  source ~/.vim-files/.vimrc.java
autocmd BufNewFile              *.java  source ~/.vim-files/.vimrc.java

" Only do this for Vim version 5.0 and later.
if version >= 500

if has("terminfo")
  set t_Co=8
  set t_Sf=[3%p1%dm
  set t_Sb=[4%p1%dm
else
  set t_Co=8
  set t_Sf=[3%dm
  set t_Sb=[4%dm
endif

endif

" And of course, the ever important syntax highlighting
" This has to go last because of the if stuff above
syntax on
-----------------------------------------------------

This is actually just a preliminary version, I know that it will grow to be much larger than it is now simply because I haven't added all of the languages I code in to this one yet (most notably, C++ is missing).

I'm pretty liberal with my comments in here, because some of the options I set were ones that I just looked up once and forgot after that. So, if I'm ever tempted to delete certain lines, most of them have a description that will tell me what I'd be deleting by doing so. It's also a good idea to break it up into logical blocks, setting options that are similar in the same blocks, instead of just all over the place.

A quick look at the options reveals a fair amount about how I like my vim working environment. But, truth be told, setting the options for vim is hardly the most exciting and/or versatile thing we will be doing. So, pardon me if I simply gloss over them. Besides, I explained before of a good way to find new options to set and how to set them.

The real strength in my .vimrc lies in the autocmd section. You may be saying, "What are autocmd's?" Well, a quick ":h autocmd" (remember, if it's in our .vimrc file, it's just a command, and we can get help for commands with the online help with ":h <command>") puts us in the autocmd.txt help file which tells us:

--------
You can specify commands to be executed automatically for when reading or writing a file, when entering or leaving a buffer or window, and when exiting Vim. For example, you can create an autocommand to set the 'cindent' option for files matching *.c. You can also use autocommands to implement advanced features, such as editing compressed files (see |gzip-example|). The usual place to put autocommands is in your .vimrc or .exrc file.

WARNING: Using autocommands is very powerful, and may lead to unexpected side effects. Be careful not to destroy your text.
--------

[- Next page -]

1 2 3



Would you like to have your article published online? Send them in to newfiles@linuxnewbie.org
[-NHF Control Panel-]
The Linux Channel at internet.com
Linux Planet
Linux Today
Linux Central
Linuxnewbie.org
PHPBuilder
Just Linux
Linux Programming
Linux Start
BSD Today
Apache Today
Enterprise Linux Today
BSD Central
All Linux Devices
SITE DESCRIPTIONS
[-What's New-]
Order a Linuxnewbie T-Shirt
Easy Webcam NHF
Directory Navigation NHF
Installing Snort 1.6.3 on SuSE 6.x-7.x
Customizing vim
The SysVinit NHF
Installing ALSA for the VT82C686 integrated sound
USB Creative Video Blaster II for Linux
Configuring the Intellimouse Explorer in XFree86 V4+
The beginnings of a distro NHF
Getting Past Carnivore?
Getting and Installing PGP
Getting your ATI Rage 128 Working
How to create a multiple partition system
Using Fdisk
Introduction to Programming in C/C++ with Vim
Adding a Hard drive in Linux -- In five steps
Installing ALSA for the Yamaha DS-XG Sound Card
Getting your Diamond Rio Mp3 Player to work with Linux
Bash Programming Cheat Sheet
Installing NVIDIA Drivers for Mandrake
Setting up Portsentry
Hard Drive Speed Tweak for Linux
Sensei's Log
Chat room
Join: Linuxnewbie.org SETI Black Belts!
Send in your news
Click the image to add Linuxnewbie.org to your MyNetscape Page
[-LNO Newsletter-]

[-Archive-]
The beginnings of a distro NHF
Connecting to the Internet using KPPP
Getting your SBLive to work
Unreal Tournament NHF
LWE Day 2 Pictures
LWE Day 1 Pictures
The LNO FAQ!
WoW (Words of Wisdom)
Other sites news
What is Linux?
What is Linux? part deux (ups & downs)
Search newsgroups
The List
ALS Report
Feedback Form
jobs.linuxtoday.com.gif
Match: Format: Sort by:
Search:
[-Quick Links-]

Copyright 2000 internet.com Corp. All Rights Reserved. Legal Notices Privacy Policy

internet.com.gif