Vim Cheatsheet

This page is designed to help you get Vim to help you!
Vim has a long history and a massive amount of commands, keyboard shortcuts, and more.
This guide brings to you the most commonly used and most useful commands for writing C/C++ code in vim.
All of the commands here are case-sensitive, so watch your Shift key and Caps Lock.


.vimrc

If you've taken CSE 391, then you may remember that there is a file called .bashrc which lives in your home directory and contains commands that are run whenever you log in. The .vimrc file is similar to this, except it contains vim commands that will be run whenever you open a file in vim. These commands can do things like change the color theme, turn on line numbers and enable syntax highlighting. If you run the command below on attu or the CSE VM, you can download a .vimrc file that has been created by the TAs that should make your vim experience a lot better. You can read the comments in the file to see exactly what each command is doing. Additionally, this command will place another configuration file in the folder ~/.vim/after/ftplugin/c.vim which contains commands that will only be run when you open a C or C++ file.

Run this command to install the .vimrc:

curl -o ~/.vimrc https://courses.cs.washington.edu/courses/cse333/20wi/vim/vimrc.txt && curl --create-dirs -o ~/.vim/after/ftplugin/c.vim https://courses.cs.washington.edu/courses/cse333/20wi/vim/c_vim.txt

What's included in the .vimrc:

  1. Press F9 to run make (you can also just do :make). Then, you can run :copen to see the output alongside your code. Vim will even take you to the lines that have compiler problems.
  2. Open a window with the man page using the command :Man <page number> <function>.
  3. Adds a ruler at 80 characters so you can avoid those pesky linter complaints.
  4. Highlights whitespace at the end of lines.
  5. Turns on line numbers by default.
  6. Automatically turns tabs into spaces and automatically indents the proper amount after hitting enter.
  7. Sets the color theme to 'ron'. Instructions for changing this are in the .vimrc file.

Basic Commands

i Enter 'input mode', which allows you to edit text but you can't run commands
Esc Exit 'input mode' or 'visual mode' (see v)
:w Save the changes you have on the current file
:q Close the file you are currently editing
:wq or ZZ Save the changes you have on the current file and close it
:q! Close the file you are currently editing without saving
:! <command> Run <command> in your current shell
:<number> or <number>G Go to line <number>
u Undo the last change
Ctrl+r Redo the last undo
v Start highlighting by character ('visual mode' or 'selection mode')
V Start highlighting by line
y Copy/yank current selection
yy Copy/yank current line
d Cut/delete current selection
dd Cut/delete current line
p Paste the last thing that was cut/copied after the cursor
P Paste the last thing that was cut/copied before the cursor

Dealing with Multiple Windows


Opening

:e <filename> Open <filename> in the current window
:split <filename> or :sp <filename> Split the current window in two, horizontally, and open <filename> in the new window
:vsplit <filename> or :vs <filename> Split the current window in two, vertically, and open <filename> in the new window

Moving Between

Ctrl+w Ctrl+w Move clockwise to the next window
Ctrl+w → or Ctrl+w l Move to the window to the right of the current one
Ctrl+w ↑ or Ctrl+w k Move to the window above the current one
Ctrl+w ← or Ctrl+w h Move to the window to the left of the current one
Ctrl+w ↓ or Ctrl+w j Move to the window below the current one

Rearranging

Ctrl+w x Exchange the current window with the next one in the current row/column
Ctrl+w L Move the current window to occupy the right side of the screen
Ctrl+w K Move the current window to occupy the top of the screen
Ctrl+w H Move the current window to occupy the left side of the screen
Ctrl+w J Move the current window to occupy the bottom of the screen

Resizing

Ctrl+w = Have all windows take up an equal space of their row/column
<number> Ctrl+w + Increase window height by <number>
<number> Ctrl+w - Decrease window height by <number>
<number> Ctrl+w > Increase window width by <number>
<number> Ctrl+w < Decrease window width by <number>