shim.gif Clark University Math and Computer Science Department Worcester MA
  HomeWelcomeFacultyCoursesJoin UsResourcesAnnouncements
shim.gif

Quick Vim Tutorial

Vim Modes
  • One of the most important aspects to remember about VIM is that it is a modal editor. Most of the commands fall into one of three modes.

    1. Normal Mode: This is the mode VIM starts in. In this mode, most keys on the keyboard are defined to be a specific command. At any time, pressing the ESC key returns the user to Normal mode.

    2. EX-Mode: This mode is sometimes referred as the command mode. To reach this mode, one must first be in Normal mode, then press colon (:). The command(s) may be issued when the colon appears at the bottom left corner of the screen. Then press enter to execute the command. One exception to that rule is the search command: a forward slash (/) is pressed instead of the colon.

    3. Insert Mode: To reach this mode, one must first be in Normal mode, then press i. This allows a user to start inputing data into file.
The basics

These are the very basic commands needed to start vim, open a file, edit, and save a file.
  • To start vim type "vim [filename]" This will start vim editing the specified file (or a new file if no file specified).
  • If you wish to search for a word, enter normal mode and press / (forward slash) [expression]
  • If you wish to go to a certain line number, enter EX-mode and input the line number. (ie press : (colon) #, where # is the line number to jump to)
  • To edit text you must enter insert mode. Again, this is done by pressing i while in normal mode
  • To delete a word, enter normal mode and press dw to delete the word from the cursor on
  • To delete a line, enter normal mode and press dd to delete the line the cursor is on
  • To undo, enter normal mode and press u (continue pressing u to undo further)
  • To redo, enter normal mode and press Ctrl-r (continue pressing Ctrl-r to redo further)
  • To save, enter normal mode and press :(colon) w (w for write) or : (colon) w [filename] to write a new file
  • To quit, enter normal mode and press : (colon) q or : (colon) q! to force quit without saving

Some Useful Options for ~/.vimrc
  • :set autoindent - Automatically indent following lines to the indentation
  • :set smartindent - Context-sensitive indentation (great when coding)
  • :set ignorecase - Ignore case during pattern matching.
  • :set number - Display line numbers
  • :set shiftwidth=n - Width for shifting
  • :syntax on - Turn on Syntax Highlighting (also great when coding)

    Mixed File Format:
    Unix, Windows, and MAC all use different end of line (EOL) character. Vim typically figures out the fileformat for you automatically. A file with \r\n linefeeds becomes dos, a file with \r only becomes mac, and a file with \n only becomes unix. However, when a file contains BOTH \r\n and \n linefeeds, Vim declares it as a UNIX file and displays the ^M (\r) characters. These mixed-EOL files come about from stuff edited in different editors: some editors only save the lines actually changed, converting only part of the file to DOS-style or MAC-style line endings. This also happens with files generated by user-written programs if a consistent mechanism for line termination isn't used. Here is the trick to remove all the ^M's.
    • You must be in command mode (From normal mode and press :(colon) to enter command mode)
    • set fileformat=mac
    • Save and restart Vim for the changes to take effect. (simply refreshing does not work. I don't know why)
    References: There are many more commands for using vim. What I have shown above will give a user the basic tools for editing text files. For all the commands and tricks for programmers, you can check out
  • the VIM Tutorial (572 page PDF).
  • the Vim Source Forge website
  • The vimtutor. To execute the tutor, open a command line and type vimtutor
  •  
    Contact the Webmaster at jbreecher at clarku.edu