CSE 333 Vim cheet Sheet

This page is designed to help you get Vim to gelp you. Vim has a long history and a massive amount of commands, key board shortcuts, and more. This guide brings to you the most commonly used and most useful commands for writing C/C++ code in vim.

.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/22sp/resources/vimrc.txt && curl --create-dirs -o ~/.vim/after/ftplugin/c.vim https://courses.cs.washington.edu/courses/cse333/19au/resources/c_vim.txt
(or download the file using the link on the course resources web page)

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’, allows you to edit text but you can’t run commands

exc - Exit ‘input mode’/’selection 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> - go to line <number>

u - Undo the last change

ctr+r - Redo the last change the last undo

v - Start highlighting by character ('selection mode')

V - Start highlighting by line

y - Copy current selection

yy - Copy current line

d - Delete/Cut current selection

dd - Delete/Cut current line

p - Past the last thing that was cut/copied

P - Paste the last thing that was cut/copied before the cursor

Searching in Files

/<regular expression> - Search for occurrences of <regular expressions>

n - Go to the next occurrence of the search term

N - Go to the previous occurrence of the search team

:noh - Stop highlighting ocurrences of the current search term

Editing Multiple Files

:sp <filename> - Split the current window in two, horizontally, and open <filename> in the new window

:sp <filename> - Split the current window in two, vertically, and open <filename> in the new window

Moving Between Windows

ctr+w ctr+w - Move clockwise to the next window

ctr+w → or ctr+w l - Move to the window to the right of the current one

ctr+w ↑ or ctr+w k - Move to the window above the current one

ctr+w ← or ctr+w h - Move to the window to the left of the current one

ctr+w ↓ or ctr+w j - Move to the window below the current one

Rearrranging Windows

ctr+w x - Exchange the current window with the next one in the current row/column

ctr+w L - Move the current window to occupy the right side of the screen

ctr+w K - Move the current window to occuppy the top of the screen

ctr+w H - Move the current window to occupy the left side of the screen

ctr+w J - Move the current window to occupy the bottom of the screen

Resizing Windows

ctr+w = - Have all windows take up an equal space of their row/column

<number> ctr+w + - Increase window height by <number>

<number> ctr+w - - Decrease window height by <number>

<number> ctr+w > - Increase window width by <number>

<number> ctr+w < - Decrease window width by <number>