Skip to content

Vim Tips

I collected some tips on vim that I find helpful for students.

Prerequisite

You have gone through the basic quick lessons and have set up your vim in your PE account.

Learning Objectives

Students should

  • be able to compare files (e.g., input/output matching for correctness).
  • be able to recover from .swp file in vim.

1. Useful Configuration

Showing Line Numbers

If you prefer to show the line number on every line in vim, add

1
set number

to your ~/.vimrc.

2. Navigation

Faster Navigation

If you find yourselves typing too many HJKL to navigate around your code, check out the following shortcuts to navigate around:

To move word-by-word:

  • W jump to the beginning of the next word
  • B ump to the beginning of the previous word (reverse of w)
  • E jump to the end of the word (or next word when pressed again)

To search:

  • F char: search forward in the line and sit on the next matching char
  • T char: search forward in the line and sit on one space before the matching char

0 would move you to the beginning of the line, but when coding, it is sometimes useful to jump to the first non-blank character instead. To do so, use Shift+6 (i.e., ^).

In coding, we have many pairs of [], {}, () and <>. You can use Shift+5 (i.e., %) jump between matching parentheses.

Jump to a Line

If the compiler tells you there is an error on Line \(x\), you can issue :<x> to jump to Line \(x\). For instance, :40 will go to Line 40.

3. Editing Operations

Undo and Redo

Since we are on the topic of correcting mistakes, U in command mode undo your changes. Prefix it with a number \(n\) to undo \(n\) times. If you want to undo your undo, Ctrl+R will redo.

vim is powerful because you can combine operations with navigation. For instance C to change, D to delete, Y to yank (copy). Since W is the navigation command to move over the current word, combining them we get:

  • CW change the current word (delete the current word and enter insert mode)
  • DW delete the current word
  • YW yank the current word (copy word into buffer)

Can you guess what each of these does:

  • DFShift+0
  • DFShift+0
  • CShift+4
  • Y0

If you repeat the operation C, D, and Y, it applies to the whole line, so:

  • CC change the whole line
  • DD delete the whole line
  • YY yank the whole line

You can add a number before an operation to specify how many times you want to repeat an operation. So 5DD deletes 5 lines, 5DW deletes 5 words, etc.

See the article Operator, the True Power of Vim for more details.

Swapping Lines

Sometimes you want to swap the order of two lines of code, in command mode, DDP will do the trick. DD deletes the current line, P paste it after the current line, in effect swapping the order of the two lines.

Commenting blocks of code

Sometimes we need to comment out a whole block of code in C for testing purposes. There are several ways to do it in vim:

  • Place the cursor on the first line of the block of code you want to comment on.
  • 0 to jump to the beginning of the line
  • Ctrl+V enter visual block mode (if your terminal intercepts Ctrl+V as paste, try Ctrl+Q).
  • Use the arrow key to select the block of code you want to comment on.
  • Shift+I to insert at the beginning of the line (here, since we already selected the block, we will insert at the beginning of every selected)
  • // to insert the C/Java comment character (you will see it inserted in the current line, but don't worry)
  • Esc to escape from the visual code.

To uncomment,

  • Place the cursor on the first line of the block of code you want to comment.
  • 0 to jump to the beginning of the line
  • Ctrl+V enter block visual mode
  • Use the arrow key to select the columns of text containing //
  • X to delete them

4. Other Useful Commands

Search and Replace in vim

1
:%s/oldWord/newWord/gc

: enters the command mode. % means apply to the whole document, s means substitute, g means global (otherwise, only the first occurrence of each line is replaced). c is optional — adding it cause vim to confirm with you before each replacement

Shell Command

If you need to issue a shell command quickly, you don't have to exit vim, run the command, and launch vim again. You can use !,

1
:!<command>

will issue the command to shell. E.g.,

1
:!ls

You can use this to compile your current file, without exiting vim.

1
:!make

make is a builtin command for vim, so you can also simply run

1
:make

Terminal

You can open an interactive shell from within vim with:

1
:terminal

This command splits the window and add a terminal, within which you can compile or run your code.

Abbreviation

You can use the command ab to abbreviate frequently typed commands. E.g., in your ~/.vimrc,

1
ab sop System.out.println

Now, when you type sop, it will be expanded into System.out.println

Auto-Completion

You can use Ctrl+P or Ctrl+N to auto-complete. By default, the autocomplete dictionary is based on the text in your current editing buffers. This is a very useful keystroke saver for long function names and variable names.

Auto-Indent the Whole File

You can GG=Shift+G in command mode (i.e., type out gg=G) to auto-indent the whole file. GG is the command to go to the beginning of the file. = is the command to indent. Shift+G is the command to go to the end of the file.

Split vim's Viewport

  • :sp file.c splits the vim window horizontally
  • :vsp file.c splits the vim window vertically
  • Ctrl+WCtrl+W moves between the different vim viewports

Alternatively, run vim -O file1 file2 to immediately open both files in two different viewpoints.

Compare two files

You can compare two files with vim, using the -d flag. For instance,

vim -d file1 file2

would open up two files for line-by-line comparison. This is most useful if you want to compare the output of your program with the expected output.

5. Recovery Files

Vim automatically saves the files you are editing into temporary swap files, with the extension .swp. These files are hidden, so you don't normally see them when you run ls. (You need to run ls -a to view the hidden files)

The swap files are useful if your editing session is disrupted before you save (e.g., the network is disconnected, you accidentally close the terminal, your OS crashes, etc).

When you launch vim to edit a file, say, foo.c. vim will check if a swap file .foo.c.swp exist. If it does, vim with display the following

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
Found a swap file by the name ".foo.c.swp"
          owned by: elsa   dated: Sat Aug 21 15:01:04 2021
         file name: ~elsa/foo.c
          modified: no
         user name: elsa   host name: pe116
        process ID: 7863 (STILL RUNNING)
While opening file "foo.c"
             dated: Mon Jul 12 18:38:37 2021

(1) Another program may be editing the same file.  If this is the case,
    be careful not to end up with two different instances of the same
    file when making changes.  Quit, or continue with caution.
(2) An edit session for this file crashed.
    If this is the case, use ":recover" or "vim -r a.c"
    to recover the changes (see ":help recovery").
    If you did this already, delete the swap file ".a.c.swp"
    to avoid this message.

Swap file ".a.c.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort:

The messages above are self-explanatory. Read it carefully. Most of the time, you want to choose "R" to recover your edits, so that you can continue editing. Remember to remove the file .foo.c.swp after you have recovered. Otherwise, vim will prompt you the above messages every time you edit foo.c.

Warning

If foo.c is newer than the state saved in .foo.c.swp, and you recover from .foo.c.swp, you will revert to the state of the file as saved in the swap file. This can happen if (i) you edit the file without recovery, or (ii) you recover the file, continue editing, but did not remove the .foo.c.swp file after.