1 of 22

Introduction to vim

2 of 22

Goals of this lesson

Understand when vim is useful for editing code and other text files

Understand the main modes of vim (command, normal, visual, edit) and how to switch between them.

How to enter and exit vim

3 of 22

4 of 22

5 of 22

Anatomy of Vim

6 of 22

7 of 22

Exercises:

-1- Use vim to create a new file called summerSchoolNotes.txt

Use insert mode to add a few lines about what you have just learned.

Use w and q together in command mode to save and exit

-2- Use vim to open the existing file called mystery.txt

Read the content of the file.

Use insert mode to add a single word title line to the top.

Save the file as your title. E.g. if your title is “cats”, your filename should be cats.txt

8 of 22

Moving around your files in Vim

Modified from Wikipedia.org

9 of 22

Moving around your files in Vim

10 of 22

Moving around your files in Vim

11 of 22

Anatomy of a Vim command:

Quantifier

Command

Motion

Optional #

Default: 1

Direct action, ex:

Insert

Delete

Paste

Specifies location for action

12 of 22

Vertical movement:

gg

G

:42

42j

42k

Move to top of file

Move to bottom of file

Move to line 42

Move down 42 lines

Move up 42 lines

13 of 22

Scavenger Hunt Exercise:

Open bigFile.txt in Vim.

In pairs: what are three ways to get to line 90? Which is the fastest?

Use your method to go to line 72. Your next instructions are found there.

14 of 22

Horizontal movement:

w

b

0

$

Move to next word

Move to previous word

Move to beginning of line

Move to end of line

15 of 22

Horizontal movement exercise:

Use Vim to open spider.txt

Consider the following keystrokes:

G

5w

3b

w

w

Which word will be highlighted? Make a prediction and then verify with Vim.

16 of 22

Anatomy of a Vim command:

Quantifier

Command

Motion

Optional #

Default: 1

Direct action, ex:

Insert

Delete

Paste

Specifies location for action

17 of 22

Deleting:

x

dw

cw

dd

u

Delete character

Delete word

Change word

Delete line

Undo

18 of 22

Deleting exercise:

Use Vim to open spider.txt

Navigate to the first instance of “spider”.

Delete the three words before this word.

Undo the change.

19 of 22

Copying and Pasting:

p

y

yy

yw

Paste

Copy

Copy current line

Copy next word

20 of 22

Copying and Pasting Exercise:

Use Vim to open spider.txt

Copy the phrase “The itsy bitsy spider”.

Paste this phrase between each line.

Save your changes.

21 of 22

Find and Replace:

f

/

:s

:%s

Find character

Find string (note: \ for special characters)

String replace on current line

String replace on all lines

22 of 22

Find and Replace Exercises:

Use Vim to open spider.txt

Use find and replace to remove all instances of “spider” and replace with “bobcat”.

Let’s say that you forgot to Esc from insert mode, and “:wq” is still in your file. How would you remove it without going into command mode?