Skip to content

Vim Cheat Sheet

You can type vimtutor in the terminal while connected to the ENGR servers to get a guided tutorial on how to use vim.

Consider learning vim while playing a game: Vim Adventures

Want more? Check out this Vim cheat sheet or this one.

If you create a file directly in your ENGR home folder with the name .vimrc (yes, its name must be exactly this, starting with a period), then you can specify a list of vim commands in this file—one per line—that will execute whenever you startup vim. Note that all files starting with a period are considered to be “hidden files”, which means that they won’t show up when you run ls unless you supply the -a flag, like so: ls -a

When listing commands in the .vimrc file, they should not start with a colon, although you’d usually prefix commands with a colon in order to run them within vim.

Here’s a simple .vimrc file:

set mouse=a
set nu
set autoindent
filetype plugin indent on
set expandtab
set tabstop=4
set shiftwidth=4
colorscheme slate
syntax on
set colorcolumn=81
  • set mouse=a: Enables mouse support in all modes.
  • set nu: Displays line numbers.
  • set autoindent: Automatically indents new lines to match the indentation of the previous line.
  • filetype plugin indent on: Enables file type detection, loading of file type plugins, and indentation settings.
  • set expandtab: Use spaces instead of tabs
  • set tabstop=4: Set tab width to 4
  • set shiftwidth=4: Set indent width to 4
  • colorscheme slate: Sets the color scheme to “slate”. You can change this to any other color scheme you like.
  • syntax on: Enables syntax highlighting.
  • set colorcolumn=81: Highlights the 81st column to help with line length limits.

There are various hotkeys that you can use in normal mode to speed up development in vim. These hotkeys should not be prefixed with a colon; they’re not full-fledged commands—just hotkeys that you can use at any time while in normal mode. As you type them, nothing will appear on the screen. That’s okay since each of these hotkeys is just a couple of key presses. These hotkeys are case-sensitive (e.g., the gg hotkey command is very different from the G hotkey command).

  • Pressing yy will copy the current line of code (the one the cursor is on) into the vim clipboard.
  • Pressing dd will cut the current line of code into the vim clipboard.
  • Pressing p will paste the currently copied / cut text from the vim clipboard after the cursor (if you yanked / deleted whole lines using yy or dd, this pastes below the current line). Pressing P will paste before the cursor (for whole lines, this pastes above the current line).
  • Pressing [count]yy will copy the current line of code plus the next count - 1 lines. For example, typing 12yy will copy 12 total lines of code, including the current line.
  • Pressing [count]dd will cut the current line of code plus the next count - 1 lines. For example, typing 12dd will cut 12 total lines of code, including the current line.
  • Pressing gg will jump your cursor to the beginning of the file.
  • Pressing G will jump your cursor to the end of the file.
  • Pressing =G will automatically correct the indentation of all code that appears after the cursor. Running this command after gg will correct the indentation of your entire file, i.e., gg=G to re-indent the entire file. It’s a good idea to run this before submitting your code if you’re concerned that your code might not conform to proper indentation as described by the course’s style guidelines.
  • Pressing u will perform an undo operation.
  • Holding ctrl and pressing r will perform a redo operation.