programming with vim

This tutorial aims to help the new hacker create a comfortable programming environment for c++ using Vim. It is my opinion that you should use tools that will help you in all circumstances, whether at your own machine or logged into a remote machine with only the command-line available. My choice of tools reflects this belief. In this tutorial you will find enough information so that you can pick up your favorite book on c++ programming and get to work learning in your new environment.

Setting Up Vim

Vim is a highly configurable and efficient programmers text editor. Vim or one of it.s variants is guaranteed to be installed on any *nix system you will encounter. Because of its wide availability it wise to add it to your list of available tools by learning to use it. Since learning to use Vim can be difficult, I recommend using GVIM, with it you get a gui that also allows you to explore the vim commands. This way you will ease into vim without a steep learning curve.

The initial Vim configuration is difficult to use for most new hackers. Fortunately, it is possible to customize vim with a simple edit or creation of .gvimrc in your home directory. Take a look at the different color schemes that ship with gvim and choose which one you like best. Under linux you can look at the color scheme files located in /usr/share/vim/vim62/colors directory (in Windows look at X:\vim\vim62\colors). You can also take a look at the example configurations configs in your vim directory.

Here is an example .gvimrc file (taken from my own machine):

---[ begin sample ]
syntax on
set backspace=2
set nocompatible
set autoindent
set ruler
set cindent
set number
set mousehide
set history=50
set tabstop=4 shiftwidth=4 noexpandtab
colors elflord
set guifont=Courier_New:h10
---[ end sample ]

Your Terminal Emulator (Linux)

Under Linux, much of your time will be spent with the ever powerful the command-line. If you prefer something simple and functional just use the default or xterm. If you are like many new linux users, you may enjoy the look of transparent terminal emulators. To get a transparent emulator use Aterm. Aterm is a lightweight terminal emulator that supports transparency out of the box. To call Aterm with a pseudo transparent blue background use these options: aterm -fg white -tr -sh 75 -tint blue.

An annoyance many people encounter with the command line is the use of ls --color. The color-coded directory and file listing helps is a great feature. To make this the default when you type ls you just need to tweak your .bashrc file (assuming you use bash). To do this append the following to your .bashrc file: alias ls='ls --color'

The Command Line (Windows)

With windows you are going to need to setup your environment to allow you to use your compiler. In this example I will show you how to do this with the compiler that comes with Visual C++. From this example you will be able to setup any other compiler (Borland and Microsoft both offer free versions of their compilers for the command-line).

The cleanest way to setup this environment is to use call the command prompt via a batch file. This allows you to set up the path for that instance. If you want to be able to call other programs from the command line (I often do this with abiword or putty), you will need to set the path to include the EXE of that program. The last line will call the command shell under W2K or WXP.

---[ begin sample ]
@echo off
@echo set the path for your compiler and the SDK
set path=D:\Program Files\Microsoft Platform SDK\Bin;%path%
set path=D:\Program Files\Microsoft Visual C++ Toolkit 2003\bin;%path%

set include=D:\Program Files\Microsoft Platform SDK\Include;%include%
set include=D:\Program Files\Microsoft Visual C++ Toolkit 2003\Include;%include%

set lib=D:\Program Files\Microsoft Platform SDK\Lib;%lib%
set lib=D:\Program Files\Microsoft Visual C++ Toolkit 2003\Lib;%lib%

@echo set the path for VIM
set path=C:\Vim\vim62;%path%

@echo set the path for Extra programs
set path=C:\Program Files\AbiSuite2\AbiWord\bin;%path%
set path=C:\Program Files\PuTTY;%path%

@echo set some keys because we are absent-minded!
doskey ls=dir/w
doskey vim=gvim

@echo compile with cl /EHsc progsrcname.cpp
@echo move to your working directory and call the prompt.
cd c:\
---[ end sample ]

Now to enable tab completion, which is one of the coolest features of many *NIX terminal emulator and is also available in Windows. To enable this feature open regedit, go to HK_CURRENT_USER, Software, Microsoft, and then to Command Processor. Change the value of CompletionChar to 9 (the ASCII value of your tab key).

Vim and Visual Studio

Once you get used to Vim you may want to continue using it even while you are working with Visual Studio directly. To use Vim with Visual Studio you need to enable the plug-in visvim (it comes default with the standard Vim windows install). To enable the plugin under Visual Studio go to Tools, Customize, Add-ins and Macro Files, and then click the Browse button. Browse to X:\vim\bin\visvim.dll and select it. This will enable the Vim Developer Studio Add-in. Use this Add-in to enable the Vim tool-bar in Visual Studio.

Well, i hope this how-to has helped you in preparing a decent programming environment with Vim.