ContentsXiaorang Li's Homepage

Getting started

  1. How to start vim

  2. How to exit vim

  3. How to enter text

  4. How to move cursor

  5. How to insert text

  6. How to delete text

  7. Copy, cut and paste

  8. How to search for word

  9. How to substitute text

How to start vim

How to exit vim

Once entering vim, what can I do

If this is one of the first few times you use vim, perhaps you're wondering why you cannot type in anything. This is because you're in command mode. In command mode, your key strokes are interpreted as editing commands. To begin typing new text into the file, you need to switch to insert mode by press i. Then you can see each character you type appears on the screen.

The two basic modes of vim are.

After you start vim, you're in command mode.

Switch between modes

  1. From command mode to insert mode, press
  2. i
  3. from insert mode to command mode, press ESC

How to move around on the screen

In both the command mode and the insert mode you can always use the arrow keys to move the cursor, and with gvim you can click mouse to get to new position. However, these are not the ways of vi guys. If you do that, you're not using vim, you're using notepad. If you want to call yourself a vi guy, forget about the arrow keys, backspace, delete, insert, ... and of course, the mouse. The most effective ways of moving cursor is first go to the command mode by pressing ESC and then use vim's cursor-moving commands to move around. The 4 basic commands are

Remember: these commands work only in command mode. At first you may feel a bit uncomfortable. After you get familiar using these commands you will stick to them and forget the arrow keys.

How to delete

First I will tell you that the DELETE key always works, and the BACKSPACE key works with the newly typed text in the insert mode. However, I suggest you do not use them. Instead, force yourself to use vim's deletion commands.

  1. Make sure you are in command mode by press ESC
  2. move the cursor to the character you want to delete
  3. press x , this character disappears
x is just one of the many powerful deletion commands. Usually you need to move the cursor to the character you want to delete and then press x to remove it. Remember using the cursor motion commands j k h l to locate your target, and don't leave the command mode.

How to insert text

  1. In command mode, move the cursor to the desired location
  2. press i, then you are in insert mode and can type in whatever you like.

How to COPY, CUT and PASTE

If you run the GUI version of vim, gvim, you can use mouse and the pull-down menus to do that---the same fashion with other editors. However, that is not the preferred style. You'll feel better off if you can live without a mouse.

  1. Enter the command mode
  2. by pressing ESC

  3. Move the cursor to the line which you want to make a copy, by pressing j or k
  4. press
  5. yy
    to make a copy of the line, or
    dd
    to cut it and make a copy

  6. now move cursor (by pressing k or j) to the the location where you want to put this copy
  7. press
  8. p
    to put the buffer after current line, or
    P
    to put the buffer before current line
If you want to copy or cut several lines, put a number before the yy or dd command, like
8yy
to copy 8 lines.

How to search for a word

Suppose you want to find all the words apple in your file
  1. Make sure you are in command mode
  2. by hitting ESC

  3. type
  4. /apple
    followed by ENTER to find an occurrence of apple. When you type the slash, it and the following characters will be shown on the bottom of the screen. After you press ENTER, the cursor will go to the first occurrence of apple if found, and the target will be highlighted.

  5. after you got the first apple, you can keep typing
  6. n
    to find other apples

How to substitute a word with another one

First make sure you're in command mode by pressing ESC.

ContentsXiaorang Li's homepage