An Introduction to vi
(Thanks to Dr. Taylor for this quick-start guide.)
First, some language: there are three common versions of vi
.
vi
is the simplest, and will open directly in your terminal, not
as a separate window. vim
is "vi improved," and (most
crucially) has syntax highlighting. gvim
is "graphical
vim," and will open a new window with clickable buttons that copy, paste,
save, and all sorts of other things that are helpful when you're first getting
started. All three are colloquially referred to as "vi" (pronounced
vee-eye).
The goal of vi is to never use the mouse or arrow keys - if you are, you're not using it right. So, you need keyboard shortcuts for everything you might want to do (copy, paste, save, open a new file, move around, etc.). As a result, vi has two modes: "insert mode," which is when you're actually typing text into your file, and "normal mode," which is when all your keys get remapped to allow you to do everything else you might want to do. As you might guess by the names, when you're good at vi, you spend more time in "normal mode" than actually typing!
vi has a learning curve which can seem steep. However, it will save you a LOT of time in the long run. A LOT.
Below are some of the shortcuts I use most often to help get you started. This is a very, very small subset of the things you can do in vi. Start with even a subset of these which seem most useful to you, and add more as you get better.
Going from insert mode to normal mode
[Esc]
Going from normal mode to insert mode
i
: insert - enter insert mode just before the cursora
: append - enter insert mode just after the cursorI
: Insert - enter insert mode at the beginning of the lineA
: Append - enter insert mode at the end of the line
Moving around the file
h
: move left one characterj
: move down one linek
: move up one linel
: move right one characterw
: move right one wordb
: move left one word0
: move to the beginning of the line$
: move to the end of the lineG
: move to the last line in the file- [number]
G
: move to line [number]. For example, typing19G
would take you to line 19. /
: used to search./some words
followed by an Enter will take you to the next instance of "some words". Underrated by beginners, used heavily by experts.n
: move to the next instance of what you just searched forN
: move to the previous instance of what you just searched for
Making edits
c
followed by a move command : change - performs the move command, deleting everything it passes over, and enters insert mode. For example,cw
deletes the word after your cursor, and enters insert mode so you can replace it.d
followed by a move command : delete - performs the move command, deleting everything it passes over. Stays in normal mode. delete is the same thing as cutting in vi. The text you deleted is now on your clipboard.y
followed by a move command: yank - copies everything the cursor passes over.cc
: "changes" the current linedd
: "deletes (cuts)" the current lineyy
: "yanks" the current linev
: enters "visual mode", where you can use the move commands to highlight part of the text. Then hitc
,d
, ory
to change, delete (cut), or yank (copy).V
: same asv
, but selects entire linesp
: pastes after the cursorP
: pastes before the cursor
Saving/opening files
:w
: write - saves the file under its current name (follow with an Enter):w filename
: save as filename (follow with an Enter):q
: quit (follow with an Enter):wq
: save and quit (follow with an Enter):e filename
: exit this file, and open upfilename
instead (follow with an Enter)