User Tools

Site Tools


vi reference


By Line

nG to line n
0, $first, last position on line
^ or _ first non-whitespace char on line
+, - first character on next, prev line

By Screen

^F, ^B scroll foward, back one full screen
^D, ^U scroll forward, back half a screen
^E, ^Y show one more line at bottom, top
Lgo to the bottom of the screen
z <return>position line with cursor at top
z. position line with cursor at middle
z- position line with cursor at

Marking Position on Screen

mp mark current position as p (a..z)
`p move to mark position p
'p move to first non-whitespace on line w/mark p

Miscellaneous Movement

fm forward to character m
Fm backward to character m
tm forward to character before m
Tm backward to character after m
w move to next word (stops at puncuation)
W move to next word (skips punctuation)
b move to previous word (stops at punctuation)
B move to previous word (skips punctuation)
e end of word (puncuation not part of word)
E end of word (punctuation part of word)
), ( next, previous sentence
]], [[ next, previous section
}, { next, previous paragraph
% goto matching parenthesis () {} []

Editing Text

Entering Text

a append after cursor
A or $a append at end of line
i insert before cursor
I or _i insert at beginning of line
o open line below cursor
O open line above cursor
cm change text (m is movement)

Cut, Copy, Paste (Working w/Buffers)

dm delete (m is movement)
dd delete line
D or d$ delete to end of line
x delete char under cursor
X delete char before cursor
ym yank to buffer (m is movement)
yy or Y yank to buffer current line
p paste from buffer after cursor
P paste from buffer before cursor
“bdd cut line into named buffer b (a..z)
“bp paste from named buffer b

Searching and Replacing

/w search forward for w
?w search backward for w
/w/+n search forward for w and move down n lines
n repeat search (forward)
N repeat search (backward)
:s/old/new replace next occurence of old with new
:s/old/new/g replace all occurences on the line
:x,ys/old/new/g replace all ocurrences from line x to y
:%s/old/new/g replace all occurrences in file
:%s/old/new/gc same as above, with confirmation


n>m indent n lines (m is movement)
n<m un-indent left n lines (m is movement)
. repeat last command
U undo changes on current line
u undo last command
J join end of line with next line (at <cr>)
:rf insert text from external file f
^G show status

Hello vim

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.

Here is the vim tutorial, you can download it and open it in vim, it will take through the rest. Or 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.

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.
ESC start command mode ( it starts in command mode )
i change to insert mode
:q quit
:wq write and quit
:w write the file
:# goto line #
/str search down for “str”
?str search up for “str”
dd delete line
x delete character
dw delete word ( cut )
yy yank line ( copy )
yw yank word
p paste
u undo ( only limit is the last write )
. repeat last change
cw change word

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.

I insert at begining of current line
a change to append mode
A append to end of current line
o insert mode in new line, next line
» shift line right one shiftwidth
« shift line left one shiftwidth
ZZ save and quit
:x save and quit
G goto last line of the file
#G goto the # line of the file
cntrl-r redo ( reverses undo )
v start / stop visual mode ( see below )
:help help files on everything for VIM ( you give a topic )
q exit recording mode ( read :help recording )
= re-indent
:set et expand tabs to spaces
gq reformat paragraph

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.


The following commands set the indenting mode.

:set autoindent
:set smartindent
:set cindent

I personally like cindent the best, only autoindent in available on plain vi.

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


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 :


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 :


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.

On vulcan my .vimrc ( ~jclark/.vimrc ) is world readable so that you can copy and/or read it.

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:-
" 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
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

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 ) :


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.

vi under Red Hat 5

Install vim and use as default:

yum install vim-enhanced
alias vi='vim'

Load default settings with .vimrc

If you want to configure individually for your user account, create a file name .vimrc in your home directory. Below an example .vimrc file with some defaults:

set tabstop=2
set smarttab
set shiftwidth=2
set autoindent
set expandtab
set backspace=start,indent
set number
set hlsearch
syntax on
autocmd FileType make  set noexpandtab

Substitute command Example

/srv/ · Last modified: 2009/08/15 12:14 (external edit)