This page was last modified: July 22 2007 17:10:15   
Too Cool for Internet Explorer

My preferred texteditor is Vim. Why? Because it's the only one I've ever used ;-) - There's probably a lot of other good text editors out there, but I was recommended to use Vim, and just stuck to it, since it gets the job done for me. So if your looking for a "this text editor vs. another text editor" -article, you've definitely come to the wrong place...

This document contains:

Installing Vim

cd /usr/ports/editors/vim/
make WITHOUT_X11=yes install clean distclean

Note the WITHOUT_X11=yes part. I'm including it since I'm not using a graphical interface on the server, and therefore I don't need support for a GUI. It would just take up unnecessary diskspace.

Configuring Vim

The configuration file for Vim is named .vimrc (note the dot in the filename) in your home directory. The .vimrc file is sometimes also referred to as the exrc file. They are the same type of file, but "exrc" is what Vi always used, "vimrc" is a Vim specific name.

If you can't see the .vimrc file in your home directory, just create it. It is safe to use Vim at this point .. if there's no user configuration file, the default settings is used instead.

Vim has MANY configuration options and I recommend you take a look at Vim documentation: options if you wan't to know more about the settings. All settings can be set both at runtime (while editing a file) and in .vimrc.

This is my .vimrc:

if &term =~ "vt100"
  set t_Co=8           "set number of colors
  set t_Sf=^[[3%p1%dm  "set foreground color
  set t_Sb=^[[4%p1%dm  "set background color
  set t_kD=^[[3~      "fix delete button
  set t_kh=^[[1~      "fix home button
  set t_@7=^[[4~      "fix end button
endif

set ai nocp digraph ek hid ru sc vb wmnu noeb noet nosol nu
set bs=2 fo=cqrt ls=2 shm=at tw=255 ww=<,>,h,l ts=2
set bg=dark
colorscheme mine
syntax on

If you copy and paste the above, there's a few things you have to be aware of. The red parts has to be inserted in a special way. Just delete them and insert them again like this.:

^[ = press [CTRL]+[V] and then [ESC]

^[[3~ = press [CTRL]+[V] and then [DELETE]
^[[1~ = press [CTRL]+[V] and then [HOME]
^[[4~ = press [CTRL]+[V] and then [END]

To be honest I don't know what half of the stuff in my configuration file means... and I don't care as long as everything works as expected. Of course there was a few things I had to look into, since they didn't behave as I wanted. In my case, that was the delete, home and end keys. It took me a while to figure it out, but finally i found a fix for this and added the above three lines.

This is what I found in the documentation regarding set bg=dark:

Vim guesses the background color that you are using. If it is black (or another dark color) it will use light colors for text. If it is white (or another light color) it will use dark colors for text. If Vim guessed wrong the text will be hard to read. To solve this, set the 'background' option.
This means that if your colors seam to light for the background use:

set background=dark

or

set bg=dark

They both have the same effect

If the opposite is the case, try:

set bg=light

I found an explanation of the minimal settings for the .vimrc file at http://www.frozen-north-linuxonline.com/Howto/vimsetup.html. Use that link if you wan't a quick and understandable explanation of the lines beginning with set ai nocp and set bs=2 fo=cqrt .

Syntax highlighting

One of the things I love most about Vim, is its syntax highlighting and mappings.

If unix and Vim is new to you syntax highlighting can be a real hassle to get working. I did everything right, but it still didn't work. Then someone suggested that I change my term to vt100, and then everything worked fine.... here's what I learned from struggling with the settings.:

You need to add two things to your configuration file (.vimrc): colorscheme mine tells Vim to use a theme called "mine" for syntax highlighting (more about that below), and syntax on is necessary to enable syntax highlighting.

You need a color scheme file - a configuration file for syntax hightlighting. Vim comes with a variaty of examples. Just take a look in /usr/local/share/vim/vim64/colors.

Create a new directory in your home dir for the file:

mkdir ~username/.vim
mkdir ~username/.vim/colors

Next pick a color scheme you like and copy it to .vim/colors/mine.vim.

When the file is in place, open it and change the value of colors_name:

let colors_name = "mine"

Set as default

If you always prefer to use Vim when editing text, you should set it to be your default editor. This is done by changing the EDITOR environment variable, which is located in the shell initialization file, in the users home directory.

The name of the file depends on the shell. In the below example the file is .profile, since this user is using the shell named bash. If the file does not exist, just create it. Consult the man pages for your shell, if you are not sure which file to edit.

cd /usr/home/bill
vim .profile

The file could contain something like this:

# $FreeBSD: src/share/skel/dot.profile,v 1.19.2.2 2002/07/13 16:29:10 mp Exp $
#
# .profile - Bourne Shell startup script for login shells
#
# see also sh(1), environ(7).
#
# remove /usr/games and /usr/X11R6/bin if you want
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/local/bin:
/usr/X11R6/bin:$HOME/bin; export PATH
# Setting TERM is normally done through /etc/ttys. Do only override
# if you're sure that you'll never log in via telnet or xterm or a
# serial line.
# Use cons25l1 for iso-* fonts
# TERM=cons25; export TERM

BLOCKSIZE=K; export BLOCKSIZE
EDITOR=vim; export EDITOR
PAGER=more; export PAGER

# set ENV to a file invoked each time sh is started for interactive use.
ENV=$HOME/.shrc; export ENV

[ -x /usr/games/fortune ] && /usr/games/fortune freebsd-tips

Just locate the EDITOR environment variable (or insert it if it isn't there) and change its value to vim. The change will take effect at the next login.

Tabbing with spaces

You can setup Vim to use spaces when pressing the [TAB] button on your keyboard, instead of inserting a real tab. I always setup Vim to insert 2 spaces whenever I press [TAB]. This is done by inserting this in the .vimrc file:

set tabstop=2
set shiftwidth=2
set expandtab

The expandtab option makes Vim use spaces instead of tab.
The tabstop option controls how many spaces are inserted when [TAB] is pressed.
The shiftwidth option controls how many spaces are inserted for indentation.

If at some point you want to insert a real tab character, just press [CTRL]+[V]+[TAB].