1 hour
Here are links to lessons that should be completed before this lesson:
The goal of this topic is to provide a basic introduction to a powerful and ubiquitous text editor called Vim. With a foundation in Vim, you will be able to edit text files (e.g. code, configuration files, etc.) on just about any UNIX- or Linux-based computer. (And you can use Vim on Windows machines too if you want!)
Some situations where you might use Vim include:
- Editing Git commit messages (useful when you learn about Git a few lessons from now)
- Editing your bash profile (which you'll learn about today)
- Accidentally opening Vim from the command line and needing to know how to exit it :-)
Participants will be able to:
- Open up a file in the Vim editor
- Navigate around within a file
- Make basic changes
- Save changes made to a file
- Exit the editor (with or without saving changes)
- Understand the difference between insert and command mode, and how to switch between them.
- Understand some of the basic structure of how editing commands work
- Use the help feature to learn more
- Invoking Vim
- Exiting vim
- Without saving
- With saving
- Moving around
- Basic movement:
h
,j
,k
,l
- Basic movement:
- Inserting text with
i
- Moving around
- Word-based movement:
w
,e
,b
, and their capital-letter equivalents - Fast movement
- Moving to the beginning (
gg
) or end (G
) of the file - Moving to a specific line number (
<n>G
and:<n>
) - Moving by "paragraphs" (
{
and}
) - Matching parens/braces/etc (
%
) - Matching word-at-cursor (
*
and#
) - movement with command modifiers (see below)
- Finding a letter (
f
andF
)
- Moving to the beginning (
- Searching (
/
and?
,n
andN
)
- Word-based movement:
- Inserting text (
i
,I
,a
,A
,o
,O
) - Changing lines
- Deleting lines (
d
/dd
command) - Command modifiers (inserting or prepending a number)
- modified basic movement
- modified delete
- modified insert
- Combining commands with movement (e.g.
d3w
) - Undo (
u
) and redo (ctrl-R
) - "ex mode" (
:
)
Vim is a program you can use on the command line (Terminal) to edit text files. When using the command line, sometimes you'll need to use Vim to edit a file, so it's good to know the basics of Vim.
Vim does not have official documentation. The interactive Vim tutorial is enough to cover all necessary information about Vim.
If you want to get a feel for Vim commands to save you time, you can watch this video : How to Use Vim - Tutorial
To get started with Vim, try doing these things on the command line:
- Open a new file in Vim called favorites.txt by typing
vim favorites.txt
- Now you're in Vim! Type the letter
i
. Typingi
puts you in "insert mode", which lets you insert text into the file. - Type some text! For example, type your three favorite foods.
- Press the escape key. This takes you out of insert mode and into called "command mode". In "command mode" you can no longer type text. Try typing some letters to test that. (But note:
i
,a
, ando
will put you back in insert mode!) - Once you've pressed escape to leave insert mode, save your work by typing
:w
and then press enter. - Now you can quit Vim by typing
:q
and pressing enter. - Nice, you created and edited a file using Vim! You can open your file again by typing
vim favorites.txt
again. You can usei
to edit your file again, and escape to leave insert mode when you're done. - A good shortcut to know is you can type
:wq
to save and quit at the same time.
Now you've learned enough to handle 90% of Vim situations :-). But there are tons of additional Vim commands you can learn, including commands to move through the file, edit text, copy and paste, and much more.
Try using vimtutor
to learn more about Vim! vimtutor
is a Vim tutorial that comes on your computer. vimtutor
will actually open the tutorial file on your computer using Vim, and as you go through the tutorial you'll use Vim commands to navigate and edit the file.
To get started:
- On the Terminal, type
vimtutor
. Read the text on the screen. - The letter
j
is like a down arrow in Vim. Use thej
key to scroll down through the tutorial. - Follow the tutorial! Be sure to do all the exercises. Go through the end of lesson 1.
- If you have more time, do lesson 2.
Do the rest of vimtutor
to learn a lot more about Vim! There are 7 lessons in total. Go through as much as you have time to do (we recommend spending about 1 hour total on this lesson).
See if you can do these things in Vim without looking at any reference material:
- Open a new file in Vim
- Write some text, including multiple lines (such as a bulleted list of your favorite foods)
- Save and quit the file
- Open the file again
- Use the "hjkl" keys to go to the end of the third line
- Edit that line to contain different text
- Save and quit
More advanced:
- Delete a word without going into insert mode
- Move around in the file without pressing
h
,j
,k
, andl
a bunch of times. Which commands did you use instead? - Search for a certain word in the file