logo for Vim: The editor
Source: vim.org
Basic Functions
  • Open an existing file by entering vim in the shell followed by the name of the file.
  • Create a new file in the same way as opening a file by specifying the new filename. The new file will not be saved unless specified.
  • Save a file that is currently open by entering the :w command.
  • Quit an open file by entering the :q command. If you have made any edits without saving, you will see an error message. If you wish to quit without saving the edits, use :q!.
  • Save and Quit at the same time by combining the commands: :wq.
  • Edit the file by entering insert mode to add and remove text. Entering into normal mode will allow you to easily copy, paste, and delete (as well as other functionality).
  • Cancel a command before completely entering it by hitting Esc twice.
Normal (Command) Mode

Vim starts in normal mode, and returns to normal mode whenever you exit another mode. When in normal mode, there is no text at the bottom of the shell, except the commands you are entering.

Navigation

Navigation in normal mode has a large number of shortcuts and extra features, which we will only cover some of here. Basic movement can be done using the arrow keys or using the letter keys as shown in the table.

Keys for basic movement.
MoveKey
h
j
k
l

The benefits of using the alternate keys is that you do not have to move your hand back-and-forth to the arrow keys while in this mode, and can more effectively enter Vim commands (once you are practiced). Some other examples of navigation shortcuts include:

  • Move to the beginning of the line: 0
  • Move to the end of the line: $
  • Move to the beginning of the next word: w This can also be used with a number to move multiple words at once (i.e. 5w moves 5 words forward).
  • Move to the end of the current word: e This can be used with a number in the same way that w can to move multiple words at once.

These extra navigation shortcuts become powerful when combined with other Vim functions, allowing you to edit text and navigate through the file without changing modes.

Insert Mode

When you first open a document, you will always start in normal mode and have to enter insert mode. To enter insert mode where the cursor is currently located in the file, press the letter i or the Insert key. Additionally, you can press the letter a (for append) if you would like to enter insert mode at the character after the cursor. To exit insert mode, press the Esc key. When in insert mode, -- INSERT -- will be visible at the bottom of the shell. Navigation in insert mode is done with the standard arrow keys.

Editing Features

Here are some important commands to know:

  • Undo the previous command, even the last edit in insert mode, with the command u
  • Redo the previous command (after undo) with Ctrl-R
  • Copy (yank) characters, words, or lines:
    • yl to copy a single character under the cursor
    • yy to copy the current line
    • y#y or #yy where # is replaced with the number of lines you want to copy (i.e. y25y will copy 25 lines).
  • Paste (put) characters, words, or lines:
    • p will paste after the cursor for characters and words, or on the next line (regardless of the cursor location within a line) if you are pasting lines.
    • P will paste before the cursor for characters and words, or on the preceding line (regardless of the cursor location within a line) if you are pasting lines.
  • Delete or Cut characters, words, or lines (that can then be pasted elsewhere):
    • x to delete a single character under the cursor
    • dd to delete the current line
    • d#d or #dd where # is replaced with the number of lines you want to delete (similar to copy).
  • Search for strings throughout a file and optionally replace:
    • A basic search for a word is simply /word followed by Enter. This will jump to the first occurrence of the word after the cursor. Phrases can also be used.
    • Once a search is active, you can use n to jump to the next occurrence and N to jump to the previous occurrence.
    • Search and replace has many options, but one example is to find all occurrences of "foo" in the file and replace (substitute) them with "bar" with the command: :%s/foo/bar/g
  • Split the screen vertically or horizontally to view multiple files at once in the same shell:
    • :sp <filename> will open the specified file above the current active file and split the screen horizontally.
    • :vsp <filename> will open the specified file to the left of the current active file and split the screen vertically
    • Navigate between split-screen files by pressing Ctrl-W followed by navigation keys (i.e. Ctrl-W h or Ctrl-W ← to move to the left file)
    • Also note that you can open several documents at once from the shell using appropriate flags. See man vim for more information.

Any of the editing commands can easily be combined with navigation commands. For example, 5de will delete the next 5 words, or y$ will copy from the current cursor location to the end of the line. There are a large number of combinations and possible commands. Note that copying, pasting, and deleting can also be done efficiently using visual mode.

Visual Mode

From normal mode, press the v key to enter visual mode. This mode enables you to highlight words in sections to perform commands on them, such as copy or delete. Navigation in visual mode is done with the normal mode navigation keys or the standard arrow keys. For example, if you are in normal mode and you want to copy a few words from a single line and paste them on another line:

  1. Navigate to the first character of the first word you want to copy
  2. Enter visual mode by v
  3. Navigate to the last character of the last word you want to copy (this should highlight all the words you want)
  4. Enter y to copy the words
  5. Navigate to where you want to paste the words
  6. Enter p to paste

Note that step 6 will paste after the cursor instead of on the next line even if you have copied several lines. You can also replace that step with P to paste before the cursor.

Vim and the Shell

Working with Vim regularly can mean switching back and forth between it and the shell, but there are two ways to simplify this. From normal mode, you can use the command :! followed by any shell command to execute a single command without closing the file. For example, :!ls will display the contents of the current directory. This will appear to background Vim while executing the command (so you can see the shell and output), and display the following message upon completion:

Press ENTER or type command to continue

Pressing Enter will return you to your open file. Alternatively, you can simply background the file while in normal mode with Ctrl-Z to view the shell and issue commands. When you want to return to the file, use the foreground command fg. In this way, you can actually have a number of files open (with or without splitting the screen) all in the same shell, and easily switch between them. Note that if you background multiple files, the foreground command will bring them up in reverse order (most recent file accessed first).

Customization

Vim uses a .vimrc file for customizations. Essentially, this file is to consist of Vim commands that you would like issued each time you open Vim to customize your experience. One example of a command you will likely want is syntax on, which provides syntax highlighting for programming languages. There are also a number of commands you can explore to customize the coloring of the syntax. Here is an example of a simple .vimrc file that you may use:

syntax on
set tabstop=4
set expandtab
set number
set hls

In addition to syntax highlighting, the above customizations will set tabs to be 4 characters wide, replace tabs with spaces, show line numbers along the left-hand side of the screen, and highlight matching words when searching. There is a global vimrc file that sets system-wide Vim initializations (you will not have access to this on Stampede2), and each user has their own located at ~/.vimrc wich can be used for personal initializations.

A Hands-On Tutorial

One of the most effective ways to learn Vim is through the built-in hands-on tutorial that can be accessed via the shell by the command vimtutor. This command will open a text file in Vim that will walk you through all the major functionalities of Vim as well as a few useful tips and tricks. If you plan to use Vim even occasionally, it is a great resource. Furthermore, the above list of features and commands is not exhaustive, and the interested new Vim user should certainly explore the man pages and online resources to discover more Vim features.

 
©  |   Cornell University    |   Center for Advanced Computing    |   Copyright Statement    |   Inclusivity Statement