Introduction to vim
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
Anatomy of Vim
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
Moving around your files in Vim
Modified from Wikipedia.org
Moving around your files in Vim
Moving around your files in Vim
Anatomy of a Vim command:
Quantifier
Command
Motion
Optional #
Default: 1
Direct action, ex:
Insert
Delete
Paste
Specifies location for action
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
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.
Horizontal movement:
w
b
0
$
Move to next word
Move to previous word
Move to beginning of line
Move to end of line
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.
Anatomy of a Vim command:
Quantifier
Command
Motion
Optional #
Default: 1
Direct action, ex:
Insert
Delete
Paste
Specifies location for action
Deleting:
x
dw
cw
dd
u
Delete character
Delete word
Change word
Delete line
Undo
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.
Copying and Pasting:
p
y
yy
yw
Paste
Copy
Copy current line
Copy next word
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.
Find and Replace:
f
/
:s
:%s
Find character
Find string (note: \ for special characters)
String replace on current line
String replace on all lines
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?