Intro to the ViM Editor

Posted on Wed 02 May 2007 in Technology

What is Vim?

VIM (Vi IMproved) is a powerful text editor available for *nix and many other systems, including Windows. It emulates the behavior of the classic vi editor written by Bill Joy but adds many features.

Ubuntu Linux

Unfortunately, vim does not come installed on Ubuntu Linux. What seems to be vim is actual a package called vim-tiny, which is missing some key features of vim, like syntax highlighting.

To install vim you can use the Synaptics package and search for vim, or open a terminal and run

sudo apt-get install vim

Microsoft Windows 95/98/ME/NT/200/XP

For Windows users who would like to use vim, but would prefer a Graphical User Interface, there is a Windows version of vim which includes Gvim. Download the Windows version here

References, Guides, and Cheatsheets

Sample .vimrc

When vim is run by default it will look for a vimrc file that contains configuration info and scripts that it can use while it is running. The file can also be found here.

"
" .vimrc
"
" Vim configuration resource file.  Specifies desired
" behavior for the vim editor.
"
:set showmode          " Tell you if you're in insert mode
:set tabstop=4         " Set the tabstop to 4 spaces
:set shiftwidth=4      " Shiftwidth should match tabstop
:set expandtab         " Convert tabs to <tabstop> number of spaces
:set nowrap            " Do not wrap lines longer than the window
:set wrapscan          " Wrap to the top of the file while searching
:set ruler             " Show the cursor position all the time
:set showmatch         " Show matching [] () {} etc...
:set smartindent       " Let vim help you with your code indention
:set formatoptions+=ro " Automatically insert the comment character when
                       " you hit <enter> (r) or o/O (o) in a block comment.
:set backspace=2       " makes backspace work like you expect
:set nohlsearch        " Don't highlight strings you're searching for
:set incsearch         " Do highlight as you type your search
:set ignorecase        " makes searches case-insensitive

" Disable paren highlighting
"let loaded_matchparen = 1

" Switch syntax highlighting on, when the terminal can support colors
" if # of terminal colors > 2 OR you're using Gvim
if &t_Co > 2 || has("gui_running")
    :syntax on
    " Change the highlight color for Comment and Special
    " to Cyan.  Blue is too dark for a black background.
    :highlight Comment  term=bold ctermfg=cyan guifg=cyan
    :highlight Special  term=bold ctermfg=cyan guifg=cyan
    :highlight Constant term=bold ctermfg=red  guifg=cyan
endif

" Make vim turn *off* expandtab for files named Makefile or makefile
" We need the tab literal
:autocmd BufNewFile,BufRead [Mm]akefile* set noexpandtab

" Make vim recognize a file ending in ".template" be a C++ source file
:autocmd BufNewFile,BufRead *.template set ft=cpp

" Make vim tab 2 spaces for HTML files
:autocmd BufNewFile,BufRead *.htm* set tabstop=2
:autocmd BufNewFile,BufRead *.htm* set shiftwidth=2
" Make vin tab 2 spaces for PHP files
:autocmd BufNewFile,BufRead *.php set tabstop=2
:autocmd BufNewFile,BufRead *.php set shiftwidth=2

" Make vim recognize Y86 assembly files
:autocmd BufNewFile,BufRead *.ys set ft=asm
:autocmd BufNewFile,BufRead *.ys set nosmartindent

" Make vim recognize R files
:autocmd BufNewFile,BufRead *.[Rr] set ft=r

" Make vim recognize Prolog files
:autocmd BufNewFile,BufRead *.pl set ft=prolog
" Turn off paren matching for Prolog files
:autocmd BufNewFile,BufRead *.pl NoMatchParen

" Adds main program heading from Program Style Guidelines
function FileHeading() 
let s:line=line(".") 
call setline( s:line,"// Program:     ") 
call append(  s:line,"// Author:      Derrick ") 
call append(s:line+1,"// Date:        ".strftime("%b %d %Y")) 
call append(s:line+2,"// Assignment:  ") 
call append(s:line+3,"// Purpose:     ")
call append(s:line+4,"//  ")
call append(s:line+5,"// Input:       ")
call append(s:line+6,"// Output:      ")
call append(s:line+7,"// Related")
call append(s:line+8,"// Files:       ")
call append(s:line+9,"// Functions:   ")
call append(s:line+10,"//              ")
call append(s:line+11,"") 
unlet s:line 
endfunction 

" Adds class heading from Program Style Guidelines
function ClassHeading()
let s:line=line(".")
call setline( s:line,"// Program Name:     ")
call append(  s:line,"// Author:           Derrick ")
call append(s:line+1,"// Date:             ".strftime("%b %d %Y"))
call append(s:line+2,"// Assignment:       ")
call append(s:line+3,"// Purpose:          ")
call append(s:line+4,"// ")
call append(s:line+5,"// Public class variables")
call append(s:line+6,"//            ")
call append(s:line+7,"// Public functions:")
call append(s:line+8,"//            ")
call append(s:line+9,"// Related files:")
call append(s:line+10,"//            ")
call append(s:line+11,"")
unlet s:line
endfunction

" Adds function heading from Program Style Guidelines
function FunctionHeading()
let s:line=line(".")
call setline( s:line,"//****************************************************************************")
call append(  s:line,"//  Function name:  ")
call append(s:line+1,"//  Author:    Derrick ")
call append(s:line+2,"//  Date:      ".strftime("%b %d %Y"))
call append(s:line+3,"//  Purpose:   ")
call append(s:line+4,"//  Params:    ")
call append(s:line+5,"//             ")
call append(s:line+6,"//  Returns:   ")
call append(s:line+7,"//****************************************************************************")
unlet s:line
endfunction

" Adds HTML skeleton to file
function HTMLFrame()
let s:line=line(".")
call setline( s:line,"<html>")
call append(  s:line,"  <head>")
call append(s:line+1,"    <title></title>")
call append(s:line+2,"  </head>")
call append(s:line+3,"  <body>")
call append(s:line+4,"    ")
call append(s:line+5,"  </body>")
call append(s:line+6,"</html>")
unlet s:line
endfunction

" Adds Unix shebang line to beginning of 
" any new Ruby script
function RubyFile()
let s:line=line(".")
call setline( s:line,"#!/usr/bin/env ruby")
call append(  s:line,"")
unlet s:line
endfunction

" If opening new file with these file extentions
" then execute corresponding functions
:autocmd BufNewFile *.C,*.c execute FileHeading()
:autocmd BufNewFile *.h execute ClassHeading()
:autocmd BufNewFile *.htm* execute HTMLFrame()
:autocmd BufNewFile *.php execute HTMLFrame()
:autocmd BufNewFile *.rb execute RubyFile()

" Map keys to perform series of key strokes and functions
" Ex, If F2 is pressed then execute FileHeading function,
" and move cursor to first line of comments
imap <F2>  <esc>mz:execute FileHeading()<enter>`zA
imap <F3>  <esc>mz:execute FunctionHeading()<enter>'zjA
imap <F4>  <esc>mz:execute ClassHeading()<enter>'zA

Unix/Linux

On *nix systems this file is called .vimrc and found in your home directory, ~/.vimrc. Depending on how your system is set up you may not see files starting with a “.” when you list the contents of a directory. To list everything in a directory run the command ls -a

If you would like to use the .vimrc supplied here you can run the following command

wget -O ~/.vimrc http://www.control-d.com/files/vimrc

This will get the text file from the server and save it as .vimrc in your user’s home directory.
NOTE: if you currently have a .vimrc file in your home directory it will be overwritten with this command. If this is the case then either your system administrator has provided you with a default vimrc or you have an idea of what you’re doing. From here you can just open it in your favorite editor ;-) and edit as you like.

Windows 95/98/ME/2000/XP

On Windows systems, after installing gVim the configuration file is called _vimrc and by default installed to the C:/Program Files/vim directory.

If you would like to use the .vimrc supplied here then save the vimrc file from your browser (File->Save As…) into the Vim directory as vimrc.vim. Now edit the _vimrc file and replace the line that says

source $VIMRUNTIME/vimrc_example.vim

with

source $VIMRUNTIME/vimrc.vim

This will tell vim to also use this file for configuration info.

Vim and R

R, a statistics language and environment, can be integrated with vim.

IRC

irc.freenode.net #vim

For more info visit http://www.vim.org


There is also a version of this page available on ASU’s Association of Computing Machinery wiki.