Skip to content

Quick vim Lessons

Here is a quick walkthrough to get a taste of vim.

Lesson 1: Navigation

Download the following file for practice using vim in this session.

1
$ wget https://raw.githubusercontent.com/nus-unix-workshop/2021-s1/master/jfk.txt

The file named jfk.txt should be downloaded. Now let's start your first vim session.

1
$ vim jfk.txt

When you start, you will be in NORMAL mode. For now, just move around the cursor with H J K L. Get comfortable with using the keys.

Next, try ++parenthesis-left++ and ++parenthesis-right++ to move forward and backward, sentence-by-sentence.

Next, try { and } to move forward and backward, paragraph-by-paragraph.

Use 0 to jump to the beginning of the line, and ++dollar++ to jump to the end of the line.

Use G+G to jump to the beginning of the file, and Shift+G (G) to jump to the last line of the file.

Now try /, type in any word (or prefix of a word) and Enter. This should move the cursor to the beginning of the word. You can use N and Shift+N to move to the next match and the previous match.

When you are comfortable with moving around, you can Shift+Z+Z to exit.

Congratulations, you have just completed your first session in vim!

Lesson 2: Manipulating Text

Now, we are going to open up the same file again and try to manipulate the text. We are going to stay in the NORMAL mode still.

1
$ vim jfk.txt

Deletion

Try 0 D 3 W to move the cursor to the beginning of the line and delete three words.

Press U to undo. This is another lifesaver that you should remember.

In vim, repeating the same command twice usually means applying it to the whole line. So D D deletes the current line. Try that.

Pairing a command with Shift (or the capital letter version) usually means applying the action until the end of the line. So Shift+D deletes from the current cursor until the end of the line.

Copy Pasting

Hit P to paste back what you just deleted. Try moving the cursor to somewhere else and paste.

To copy (or yank) the current line, hit Y Y.

Remember that all these commands can be composed using the movement-action-movement pattern. For instance, Shift+9 Y Shift+0, which corresponds to: move to the beginning of the sentence, yank, and until the end of the sentence, basically copy the current sentence.

As you have seen in the D 2 W example, you can preceed an action with a number to repeat an action multiple times.

Try Y Y 9 P. You should be able to understand what just happened!

Deleting a Character

The X command deletes the current character.

Try this exercise: At the end of the file jfk.txt, there are some typos:

1
libertyi. liberty.
Change libertyi. liberty. to libtery. by positioning the cursor on the second i and delete it. Then use Shift+D to delete the extra liberty. at the end of the sentence.

Visual Mode

In addition to the INSERT and NORMAL modes, vim has the third mode, the VISUAL mode. You can enter the VISUAL mode by hitting V. Once in visual mode, you can move your cursor to select the text and perform some actions on it (e.g., D or X to delete, Y to yank).

Hitting Shift+V will allow you to select line-by-line.

The VISUAL mode allows us to pipe the selected text to another Unix command, and replace it with the result of that command.

Go ahead and try to select a paragraph in jfk.txt, and hit :. You will see that

1
:'<,'>

appears in the last line of the terminal. At this point, you can type in actions that you want to perform on the selected text. For instance,

1
:'<,'>w john.txt

will write it to a file named john.txt.

But, let's try the following:

1
:'<,'>!fmt

!fmt tells vim to invoke the shell and run fmt. fmt is another simple small Unix utility that takes in a text (from standard input) and spew out formatted text in the standard output. You will see that the width of the text has changed to the default of 65.

You can try something that we have seen before. Reselect the text, and hit

1
:'<,'>!wc

The selected text will be replaced with the output from wc.

The : command

You have seen examples of : commands for writing to a file or piping selected text to an external command.

The : command also opens up a large number of actions you can do in vim. Here are a few essential yet simple commands.

  • To jump to a line, hit : followed by the line number.
  • To open another file, hit : and then type in e <filename>
  • To find help on a topic, hit : and then type in help <keyword>

Other advanced features such as search-and-replace, changing preferences, splitting windows, opening new tabs, are also accessible from the : command.

The : command prompt supports Ctrl+P and Ctrl+N for navigating back and forth your command history, just like bash. It also supports Tab for auto-completion.

Lesson 3: Insert mode!

Finally, we are going to try inserting some text. Remember, to use INSERT mode, we always start with a command I A O or S (may paired with Shift) followed by the text that you want to insert, followed by Esc.

Let's try I (insert). Place your cursor anywhere, hit I, and start typing, when you are done. Hit Esc.

You just added some text to the file.

Place your cursor anywhere, hit A (append), and start typing, when you are done. Hit Esc. A appends the text to the end of the current line.

Hit O (open) and start typing, when you are done. Hit Esc. O opens up a new line for the your text.

Hit S (substitute) and start typing, when you are done. Hit Esc. S substitute the current character with your text.

Now try it with Shift and see the difference in behavior.

Learning More

You can run vimtutor to learn more about vim. Check out the tips that we have collected for CS2030S, or watch the various tutorials online. In addition to the introduction to vim by Yong Qi, some interesting ones are: