Suggestions for entries in a sysadmins .vimrc

Our CTO has a pretty feature-filled Vim configuration on GitHub.

Highlights:

  • Syntax highlighting, 2 space tabstop, expanded tabs.
  • NERDtree, a file-tree view similar to TextMate's project drawer.
  • FuzzyFileFinder, plugin to do TextMate's cmd-T functionality.
  • Lots of color themes with a nice one (twilight) default.

I find it great for Ruby coding, as our system administration tools are written in Ruby.


First, if you have more than a couple machines you work with, consider putting your ~/.vim/, ~/.vimrc and other useful config files (screen, your shell, etc.) in a revision control system. I prefer using darcs - it's cheap on Debian systems (no need to install Haskell compiler, just install the package directly), distributed, and has great interactive modes. If you know/like git, you can also stick with that.

My current config files are available in my darcs repository, http://repo.harnir.net/. Some specific examples from my ~/.vimrc follow. Sorry, no links for scripts, etc. because I'm a new user, but all those you can find on http://vim.org/.

Essential ViM scripts for sysadmins

  • a backup script is a must, when you edit remote files. I use cbackup.vim script, which keeps last 10 copies of each edited file in a central directory and automatically removes old ones - just put in your ~/.vim/plugins/ directory and forget about it :-)

  • TaskList lets you quickly search for FIXME, XXX and TODO tags in current file, display them and go to the tag with just one keypress. If you use these tags, consider installing this one

  • dbext is essential for all DBA, also very useful for programmers working with databases. Let's you run queries, check structure of the databases and whatnot. It is essentially a front end for tools like mysql, sqlite3, sqlplus and others. If you work with databases, it's a must!

Syntax files

I haven't seen many useful syntax in the wild, most files sysadmins work with are configs in /etc and they are usually pretty well highlighted. In my ~/.vim/syntax/ I currently have syntax for CSS colors, which is awesome (also good for web developers), but the rest isn't very well written, I'll perhaps update them in the spare time. Additional syntax files worth having are for: dhcpd.conf, /etc/network/interfaces, nginx configuration files, and SpamAssassin's local.cf.

Useful tip with syntax: if a configuration file isn't recognized (has a weird name, like /etc/apache2/sites-enabled/some.domain.com, you can either append correct extension to it's name (in this case .conf) or add a ViM modeline at the end of it:

# vim:filetype=apache

ViM configuration options

Most of the configuraion options is very popular, like set nocompatible ruler etc. Very good options to have are:

  • set gdefault: inverses the g mode in s///g - with that option /g will be used by default, so you don't need to add it every time - just add it to turn it off

  • set incsearch ignorecase smartcase hlsearch: essential for searching through files - incremental, shows matches in real time, search ignores case unless you use upper case letters

  • set pastetoggle=<F6>: or other key which you prefer, toggles between paste and nopaste modes, a must if you copy-paste something from a web pages like ServerFault :-)

  • set noerrorbells visualbell t_vb=: get rid of the annoying bell or visual bell every time you do something ;)

Functions, keyboard mappings

  • paste your user name and current date into a file, useful if you work in a team and add comments in files about who changed something when. Just write xxsig and press space, it will be changed to something like harnir 20090531. For this to work correctly with root (ie. write your user name instead of root), you need to log in using sudo.

    iabbrev xxsig <Esc>:r ![ -n "$SUDO_USER" ] && echo "$SUDO_USER `date '+\%Y\%m\%d'`" \|\| echo "$USER `date '+\%Y\%m\%d'`"<CR>I<BS><Esc>A
    
  • Insert comment "lines", like # -- Some title -------------------{{{1, length 78 characters - press \com# in normal mode and there it is. You will be automatically in Replace mode so you can add the title:

    nmap \com# O# <Esc>72A-<Esc>3A{<Esc>A1<Esc><Home>4<Right>R<Space>
    

You can easily modify it for other comment systems (", //, etc.), or just check my config file.

Ending touch

If you have your configuration files in RCS system, you might want to sometimes include configuration options specific to a certain machine. For that, it's good to have ~/.vimrc.local file, included automatically at the end of the configuration (if it's present, of course).

if filereadable(expand("~/.vimrc.local"))
        source ~/.vimrc.local
endif