For beginners, may need to read getting started with vi first.
First, how to QUIT the "help page": :q<CR>
, <CR>
is the 'Enter' or 'Return' on the keyboard
To enter help page of vim, just input :help[ topic]<CR>
, and [ topic]
means something you wanna know about, wrap in []
means this portion can be omit, e.g. :help<CR>
to enter start help page. Look out for the "space" before the "topic", means "if you wanna a topic, don't forget the 'space' before it", e.g. help about "tabe", should be :help tabe<CR>
,
Ctrl-]
follow the link under the cursor.Ctrl-o
jump back(like a 'backword' in internet browser)Ctrl-i
jump forwrod(like a 'forward' in internet browser)
As a short recap, vi is a modal editor – there’s an insert mode and a standard command mode. In insert mode, vi functions similar to a normal text editor. In command mode, you take advantage of these key bindings.
i
– Enter insert mode.Escape
– Leave insert mode. If you’re already in command mode,Escape
does nothing, so you can pressEscape
to ensure you’re in command mode.
Vi uses the hjkl keys to move the cursor in command mode. Early computer systems didn’t always have arrow keys, so these keys were used instead. One advantage of these keyboard shortcuts is that you don’t have to move your fingers from the home row to use them.
h
– Move cursor left.j
– Move cursor down.k
– Move cursor up.l
– Move cursor right.
You can also use search commands to quickly move the cursor.
/
– Type a/
followed by some text you want to find and press Enter to quickly move your cursor to the location of the text in the file. For example, if you have the word iguana in your file, type/iguana
and pressEnter
to quickly move the cursor there.?
– Like/
, but searches backwards.f
– Type anf
followed by any character to quickly move the cursor to the next occurrence of the character on the current line. For example, if you have the line “Hello world” on a line and your cursor is at the beginning of the line, typefo
to move to the o in Hello. Type fo again to move to the o in world.F
– Likef
, but searches backwards.%
– Jump between the nearest (), [], or {} characters on the line.
Use these commands to quickly move to locations in the file:
H
– Move cursor to highest (top) line in file.M
– Move cursor to middle line in file.L
– Move cursor to lowest (bottom) line in file.#G
– Type a number and then typeG
to go to that line in the file. For example, type4G
and pressEnter
to move to the fourth line in the file.
Moving between words:
w
– Move forward a word.#w
– Move forward a number of words. For example,2w
moves forward two words.b
– Move back a word.#b
– Move back a number of words. For example,3b
moves back three words.e
– Move to end of the current word.
Vi refers to the act of copying as “yanking.”
v
– Pressv
and move the cursor to select a section of text.y
– Copy (yank) the selected text.p
– Paste at cursor.x
– Cuts the selected text. Cuts the character under the cursor if no text is selectedr
– Typer
and then type another character to replace the character under the cursor.
Some commands – including the y
and v
commands above and the d
(delete) command accept cursor motion commands.
For example, when you press d
to delete some text, nothing will happen until you enter a cursor motion command. For example:
dw
– Deletes the next word.db
– Deletes the previous wordde
– Deletes to the end of the current word.dL
– Deletes all text below the cursor in the file.d/unicorn
– After pressingEnter
, deletes all text between the cursor and the word “unicorn” in the current file.dd
– Deletes an entire line.
As you can see, the combination of combining a command with a cursor movement command is very powerful.
Vi’s repeat command is very powerful, as it can repeat complex, combined commands.
u
– Undo..
– The.
repeats the last full command. The insert command also functions as a command here. For example, typeiunicorn
and pressEscape
. You can then use the.
key to insert the word unicorn at the cursor.
Once you’ve mastered the vi key bindings, you may want to use them elsewhere on your system. No problem – you can set the Bash shell to use vi-style key bindings.
Try this out in the current session by running the following command in a Bash terminal:
set -o vi
Bash will start in insert mode – press Escape
to enter command mode and use these key bindings.
If you like this, you can add the command to your ~/.bashrc file and it will be automatically run each time you log in. Use the vi .bashrc command to open and edit the file in vi.
This isn’t a complete list of key bindings for vi, but it should help you flex your vi wings and learn to fly. This list of key bindings at Harvard’s website is more complete and has more information, although it’s less organized and harder to digest all at once.