Vi / Vim Editor

vi - Visual Editor & vim - VIsual editor iMproved

Vi is one of the two most popular programmers' editors ( the other is emacs ). Vim is vi on steroids, and it is available on every platform/OS I have ever worked on. In vim all of the commands from vi still work on it, then they just added the rest.

You can type 'vimtutor' while logged in on most unix systems. This is actually a vi tutorial, it does not cover any vim features.

vi / vim basic commands

In this section, I will give some basic vi commands that may be useful. To run vi just type 'vi' or 'vim' followed by the filename.

Important : The first thing to remember is that vi/vim is a modal editor. This means that the editor has separate modes for editing and commands. In order to actually type a document you need to be in insert or append mode. This also means you have to be in command mode to issue commands.

vi / vim some other commands

Here are some other commands that you may find handy. There are hundreds of commands, I am just trying to give a list that most people are familiar with.

using the edit commands

The basic edit commands are yank, delete, and change. These can be applied in many ways. dd deletes a line, while 3dd deletes 3 lines. cw is a change a word, 5cw is change 5 words.

indenting

The following commands set the indenting mode.

:set autoindent
:set smartindent
:set cindent

I personally like cindent the best, but it doesn't work for every language, and only autoindent is in available with plain vi.

If you want to fix the indenting on your source code, and you are in command mode, just type the following command :

1G=G

This command say goto line 1 (1G), reindent (=), until the last line (G). You may also want to remove any tab damage to your code. If expandtab is set ( abbreviation is et ), you can use this command :

:retab

syntax coloring

Vim has syntax coloring, just like the visual editors from MicroSoft. This is very handy for spotting problems in your code.

If you like syntax coloring and it is not on type the following:

:syntax on

quickfix mode

Vim has what is called quickfix mode, this works in two ways. You can compile a program and capture the errors in a file, then start vim with a '-q' and it will go directly to the error/warning, and display the warning with the cursor on the line.

Here is how to do this :

g++ program.cpp >& err.txt
vim -q err.txt

The other way to use this is to use the built in make feature. In order to do this you must have a makefile.

While in vim ( in command mode ) type the following :

:make

visual mode

Visual mode allows several commands to be run in conjuction with it. To use it type 'v' then move the cursor, this will create a highlighted block of text. In the windows version of gvim, you can use the mouse to select blocks of text. I prefer to use 'V' which is line based visual mode.

Here are some commands commonly used with visual mode.

  • > shift the block right one shiftwidth

  • < shift the block left one shiftwidth

  • = reindent the block

  • y yank ( copy ) the block ( paste with a p )

  • d delete the block

  • c change the block

auto commands

Auto commands allow your setting to vary depending on the file name. In the example .vimrc below, I change indenting and formatting for different file types.

saving vim settings

In vi you made a file named .exrc, in vim it is .vimrc, you may have both if you want. If there is no .vimrc, but there is a .exrc vim will use those settings and run in vi compatible mode, which will disable most of the vim enhancements.

Here is an example .vimrc

I left out a map example since they don't display very well in html

" Here are John's " vimrc settings
set wm=8
" set wrapmargin
set nohls
" turn off highlight on search
set et
" turn on expand tab
" colorscheme adjustments :hi lists the symbols and values for this
colo evening
" change the colorscheme
" make the preprocessor stuff a lighter color
hi PreProc ctermfg=yellow
hi Constant cterm=underline,bold ctermfg=9
"turn on comment continuation for C style comments
set fo+=r
" formatoptions r adds new comment line automagically
" only apply on C comments, and mail forwarding "> "
set com=s1:/*,mb:*,ex:*/,:>,fb:-

"
" AUTO-COMMANDS " for Makefiles
" added some special formatting in Makefiles
autocmd BufEnter ?akefile* set noet ts=8 sw=8 nocindent list lcs=tab:>-,trail:x
" for source code
autocmd BufEnter *.cpp,*.h,*.c,*.java,*.pl set et ts=4 sw=4 cindent
" change the filetype, based on the extension
autocmd BufEnter *.pro,*.prolog set et ts=4 sw=4 cindent ft=prolog
" for html
autocmd BufEnter *.html set et ts=4 sw=4 wm=8 nocindent
"
" abbreviations
ab teh the
ab tomarrow tomorrow
" The man plugin does lookup with control-k, or :Man runtime ftplugin/man.vim " in my old age I have gotten weird about statuslines
set statusline=%n\ %<%F\ [%{&ff}]\ %h%m%r%y\ %=\[%b\/0x%02B\]\ %-14.(%l,%c%V%)\ %P
set laststatus=2
hi StatusLine ctermfg=white ctermbg=blue

Key mappings

You can remap keys to do certain things with the map command.

map v ithis shows a mapping<ESC>

So with that mapping if I press v in command mode it will insert "this shows a mapping"

I frequently remap things I don't use, and don't forget that the function keys are typically available ( except F1, which is help ).

string replacement

Sometimes you want to replace one string with another. This is done with the ex command "s". The form of the replacement is :

: line_range s/old/new/g

ex ( replace foo with bar from line 10 to 20 ) :

:10,20s/foo/bar/g

You can use visual mode to select the lines, when you type the colon to start the command it will use symbols for the visual line range.

other features

  • Vim ships with ctags, which allows you to hit cntl-] while on a function call, and it will take you to the function, even if it is in another file, cntl-t returns. It just uses stack to track the locations.

  • Vim has a marks capability, where it can remember where you placed a mark in a file, then you can jump to the mark at any time, even if you log out and log back in ( or power down the machine ).

  • It even does right to left editing, Farsi, and Hangul, and other international font/style settings.