Report a Problem:
Phone: 503-725-5420
Location: FAB 82-01
Home Students Fac/Staff CS Tutors Guidelines Windows Linux Mac Mail Network Software Web TheCAT Sitemap Contact
arrowHome arrow CS Tutors arrow Tips & Tricks arrow Vi Tips Archive Wednesday, 29 March 2017  
Tips & Tricks
Vi Tips Archive Print
Tuesday, 25 April 2006


Archived Tip Title
Submitted by
Vi Macros - A quick look
Sean Brown
Color Syntax Highlighting with VIM
Ki Yung Ahn
Some VI Dotfile Settings
Larry Leach


Vi Macros - A quick look

Submitted by Sean Brown 

vi macros can be very useful for coding. They can be used to automatically print out a skeleton for classes, int main() in C/C++ and whatever else you want with just a few keystrokes. Just open up the .exrc file or create your own file and use the map command. Here is an example of how to output an int main in C/C++ when you type in !qq during command mode:

map !qq ^[iint main(int argc, char* argv[])^M{^M}

This mess needs an explanation and, believe it or not, this is a lot
easier than it looks. I'll skip the map !qq portion because they were
mentioned above. The ^[iint portion is actually three things at once. ^[
is the esc character, the i is just an i (used to enter text-enter mode)
and int is the return type for main. This is equivalent to manually typing in
esc, i and int into vi. If you don't want to remember the ^[ then just
remember ctrl-v. For those who don't know, ctrl-v and esc will print ^[
and vi knows that ^[ is the esc key.

Let's continue. The main(int argc, char* argv[]) is just C/C++ syntax. ^M
is a carriage return. The curly brackets are just C/C++ open and closed curly

If you didn't put your macros in the vi resource file then you can load
your macros into vi by going to the text buffer and typing the source
command followed by the file name that contains all your macros. For example,
if the file macros.txt held all my map commands then the syntax would look like
the following: source macros.txt

Remember to start of the map command with the ^[i. This tells vi that the
macro wants to enter text-enter mode. If you don't type this then vi
interprets everything as a command and then who know's what your output will

ctrl-v is your friend and can be used to print out the string that
corresponds to any non-alphanumeric keys.

A good thing to do is start off your command with a symbol such as !, @,
or something else that vi doesn't already use in command mode. This is
good if you want a command that is more than one keystroke long. Also,
unused alphabetic commands are rare in vi.

Color Syntax Highlighting With Vim

Submitted by: Ki Yung Ahn 

Vim can display colors to highlight your code when your terminal is properly set. Many terminal programs can show colors.

In csh shell or tcsh shell, try this:

$> set term=xterm-color

$> vim ~/.vimrc

In bash shell, try this:

$> export term=xterm-color

$> vim ~/.vimrc

You may see syntax highlighting with colors. Vim automatically highlights syntax by filename suffixes. Vim distrution has syntax highlighting support for many languages including c, c++, java, html, and many others. This helps you to view your source code more easily.

If syntax highlight doesn't start you can use the command:

:syntax on

if you want to turn of the syntax highlighting use the following command:

:syntax off 

You can also save terminal settings in the shell configureation file. Ask the tutors for help with this if you want to make your terminal show colors by default. 


Some Vi Dotfile Settings

Submitted by Larry Leach 

Vi is the visual frontend for the old ex editor. So the config file is.exrc

I set it up with the following lines...

set number
set noerrorbells
set showmode

Set number gives me line numbers on one side of the screen

Set noerrorbells
shuts up the bell

Set showmode puts up a little "insert' tag at the bottom of the screenwhen I'm in VI's insert mode.


Last Updated ( Wednesday, 25 July 2012 )

©1999 - 2017 TheCAT