VIM Basics

VIM Basics

This document is designed to illustrate Vim for your usual work. Vim is a very powerful editor to fulfill your goal efficiently. However, you must learn by use. Do not expect that you can learn Vim well by just sitting and reading any kind of documentation. Therefore there is no complete tutorial here. Only tips and reference of commands are provided to help your learning.

Convention

The notation C- denotes a Ctrl key combination. C-w represents the combination of Ctrl and w.

Start up VIM

To start up Vim, simply type

  vim pentium.v

to enter the file pentium.v or

  gvim pentium.v

to use the GUI interface of Vim. You can edit multiple files and open multiple sub-windows initially

  vim -o *.v

and Vim will open all of the files you want and display and split them horizontally. Or you can use

  vim -O *.v

to get similar result with vertical sub-windows.

Text Editing

Basically there are two operation modes in Vim: command mode and insert mode. Initially Vim starts at the command mode and any input will be treated as a part of the command. To append/insert the characters into the file you must enter the insert mode first.

Moving the Cursor

To moving the cursor, you can use the keys h, j, k and l.

  h: left
  j: down
  k: up
  l: right

These four keys is very convenient to use for your right hand, but in Vim you can forget them because many people doesn't like them. You can simply use the arrow key to move the cursor. When the file size is getting bigger, you can use the hot key to scroll the page

  C-f: move forward a page
  C-b: move backward a page

Usually we edit a code in C, Perl, Verilog or some kind of language. The compilers often report that what number of the code is wrong, e.g., line 25 has a syntax error. You can jump to line 25 by using 25G at the command mode. If only command G is used, the cursor will go to the end of the file (last line).

  1G: go to the first line
  nG: go to the n-th line
  G: go to the last line

Similarly you can jump within a line

  0: jump to the beginning of the line
  $: jump to the end of the line

Additionally, you can jump between the words by using w and e to go forward and backward, respectively.

Exiting VIM

To exit Vim, just use

  :q

or you can save then quit

  :wq

If you modified the file, you have to force Vim to leaving

  :q!

without saving the changes. The command :wq is identical to :x. You can also use :w! to force a write. Usually ! can use to force an action.

Undo/Redo Your Changes

To undo a change simply use u. Vim allows multiple undoes, therefore you can go back as many times as you want. Use C-r can redo an undo. Also you can do multiple redoes.

Copy and Paste

In Vim, the copy and paste commands is called as Yank and Put. The usage of yanking is very similar with deletion.

  yw: yank a word
  yy: yank a line

also 12yy will copy 12 lines into the buffer. The next step is to put the content to where you like. Move the cursor and use p to put the content of the buffer. Moving a line or a word or multiple lines is simply deleting them then putting at the new location.

A powerful function of Vim is the visual blocking (or Visual mode). Using V, you will see the current line is highlighting. Moving the cursor you can mark more lines. A d or y can delete or copy these highlighting lines. There are three different visual mode.

  V: linewise visual mode
  v: characterwise visual mode
  C-v: blockwise visual mode

Try them can you will find them very useful. The visual can be broken by ESC.

In visual mode there are many available operations, among them here is some common and useful ones:

  d     delete
  y     yank
  U     uppercase all characters
  u     lowercase all characters
  ~     toggle case
  <     unindent
  >     indent

Search and Replace

To search a pattern, use

  /pattern

You can use n to find the next match, or N for previous match.

  ?pattern

can do the same thing, with opposite searching direction.

The method of replacement is discussed above. However sometimes we want to change a keyword to another. For example, if you want to replace every Intel with AMD, use

  :%s/Intel/AMD/g

The syntax format is

  :%s/oldpattern/newpattern/gc

  %: the range of whole file
  s: substitution
  oldpattern: old pattern to match
  newpattern: new pattern for replacement
  g: global
  c: confirm for every replacement

The first part indicates the range, % means the whole file, you can define the range of part of the file, as

  :2,46/oldpattern/newpattern/gc

to apply the substitution from line 2 to line 46. The second part indicates the command, s means substitution. The characters / separates two patterns for matching and replacement. The last field of the syntax is the option. Global option g is meant to match every pattern in a line, instead of the first matched pattern of a line.

Visual mode can be used for substitution, e.g., you can mark multiple lines using V, then type :, a line will appear as

  :'<,'>

then append the substitution command s/oldpattern/newpattern/g.

Location and File Status

Type C-g and Vim will report the filename, the status (modified or readonly), total number of lines and the cursor position.

More Tips

Recovering When Panic

When you edit an important file and suddenly there is a power outage. All system are shutdown before you can save your source. Don't be sad because Vim provide a recovering mechanism. Checking a file called .pentium.v.swp if you are editing a file called pentium.v. Recover with Vim

  vim -r .pentium.v.swp

and see if it contains the source you deal with. Save it if everything is OK, then remove the swap file .filename.swp.

Executing Shell Commands

You can execute a command with the shell, for example, you can compile you C code when editing it, :!gcc kernel.c, or :!gcc % to compile the current editing file. Executing other commands is the same.

Keyword Matching

If you are lazy as me, you will find this function wonderful :) In insert mode, you can type a few characters of a word, e.g., if there is a variable called alu_pipeline_register, you may just type alu then C-p. Vim will find out the last word in the file started with characters alu, if it is not you want, you can re-type C-p to match the further previous ones. Similarly, C-n can do that for finding the next matches. Therefore you do not worry about the mistyping of the long variables, or uncommon-used words.

Jumping Between Multiple Windows

To split the window you can use C-w s, or start Vim initially

  vim -o *.c

Jumping between the sub-windows, use C-w plus arrow key to switch the cursor.

Syntax Highlighting

In our environment, the option of syntax highlighting is set by default. Therefore you can use it without any change. The function of Vim is very convenient for coding. You can switch on and off the function by

  :syn on
  :syn off

Getting On-Line Help

To access Vim online help,

  :help

you will learn the usage of the documentation. The online help contains almost everything you need so be patient to read the instructions to use it efficiently.

Editing Multiple Files

Editing two or more files at once is our daily work. You can copy or move a block from one file to the other as you wish. There are many differnet ways to achieve the goal.

Braces Matching

If the cursor is on an opening parenthesis { [ (, the command % will move the cursor to the matching closing } ] ) in the normal mode, and vice versa.

Insert/Delete an indent

C-T/C-D insert/delete an indent in the current line, no matter what column the cursor is in. There are handy hot keys when you are editing language with ambiguous block boundary such as Python, which you have to delete the indent yourself when necessary.

Settings for .vimrc

Some useful setting and mapping for .vimrc are listed as follows:

  set nocp      " nocompatible
  set sw=4      " shiftwidth
  set et        " expandtab
  set wm=8      " wrapmargin
  set bs=2      " backspace
  set ru        " ruler
  set ic        " ignorecase
  set is        " incsearch
  set scs       " smartcase: override the 'ic' when searching
                "     if search pattern contains uppercase char
  set vb t_vb=  " set visual bell and disable screen flash
  set backup

Use :help option-list for detail description of these options.

  map Q gq
  map ,, <C-p>  " Map the auto-complete command to
                " a handy ,, sequence
Last Modified @ Mon Jan 29 16:02:15 2001 by cthuang
Email: cthuang@larc.ee.nthu.edu.tw
URL: http://larc.ee.nthu.edu.tw/~cthuang/