This article has been translated into French by Geoffrey Bachelet. You can read the French version here: l'privacy ition efficace avec vim.
"To me, VI is Zen.
To use VI is to practice Zen.
Every command is a koan.
Profound to the user,
Unintelligible to the uninitiated.
You discover truth every time you use it ."
Reddy@lion.austin.com
This tutorial assumes a basic knowledge of VIM -- insert mode, command mode, loading and saving files, etc. It is intended to help VI novices develop their skills so that they can use VI efficiently.
In this tutorial,MeansCTRL-x-- That is, hold downCTRLKey and pressX. You can get help on most of the commands used here by typing: HelpCommandIn vim, whereCommandIs what you need help on.
Moving efficientlystay out of insert mode
In general, you want to spend as little of your time in Vim's insert mode as possible, because in that mode it acts like a dumb editor. this is why most Vim novices spend so much time in insert mode -- it makes Vim easy to use. but Vim's real power lies in command mode! You'll find that the better you know vim, the less time you will spend in insert mode.
Use H, J, K, and l
The first step to efficient editing in VIM is to wean yourself from the arrow keys. one of the advantages of Vim's modal design is that you do not need to constantly move your hands back and forth between the arrow keys and the letter keys; when you are in command mode, the lettersH, J, KAndLCorrespond to the directionsLeft, down, up,AndRight, Respectively. It takes some practice to get used to, but youWillNotice the speed difference once you're used to it.
When you are editing e-mail or other paragraph-formatted text, you might notice that the direction keys skip more lines than you have CT. this is because your paragraphs appear as one long line to vim. typeGBeforeH, J, KOrLTo move by screen lines instead of virtual lines.
Use motions to move the cursor in the current line
Most editors have only simple commands for moving the cursor (left, up, right, down, to beginning/end of line, etc ). vim has very advanced commands for moving the cursor; these commands are referred toMotions. When the cursor moves from one point in the text to another, the text between the points (and including the points themselves) is considered to be"Moved over. "(This will be important later .)
Here are a few of the more useful motions:
FX
Move the cursorFOrward to the next occurance of the characterXOn the current line (obviusly,XCan be any character you like). This is an extremely useful command. You can type;To repeat the lastFCommand you gave.
TX
Same as abve, but moves the cursor to right before the character, not all the way to it. (It's very useful, really .)
FX
Move the cursorBackwardTo the next occurance of the characterXOn the current line.
W
Move the cursor forward by a word.
B
Move the cursor backward by a word.
0
Move the cursor to the beginning of the current line.
^
Move the cursor to the first character on the current line.
$
Move the cursor to the end of the line
)
Move the cursor forward to the next sentence. (useful when editing e-mail or text documents .)
(
Move the cursor backward by a sentence.
Move efficiently through the file
Vim has commands that can send you to where you want to go in your file -- there's rarely a need to scroll manually through it. The below keystrokes are not technicallyMotions, Since they move around in the file instead of in a particle line.
Move the cursorFOrward by a screenful of Text
Move the cursorBAckward by a screenful of Text
G
Move the cursor to the end of the file
NumG
Move the cursor lineNum. (For instance,10gMoves to line 10 .)
Gg
Move the cursor to the beginning of the file
H
Move the cursor to the top of the screen.
M
Move the cursor to the middle of the screen.
L
Move the cursor to the bottom of the screen.
*
Read the string under the cursor and go to the next place it appears. (for instance, if your cursor was somewhere on the word "Bob," the cursor wocould move to the next occurance of "Bob" in your file .)
#
Same as abve, could t it moves the cursor toPreviousOccurance.
/Text
Starting from the cursor, find the next occurance of the stringTextAnd go to it. You will need to press enter to execute the search. To re-execute your last search, TypeN(NEXT occurance ).
?Text
Same/, But searches in the opposite direction.
Ma
Make a bookmark namedAAt the current cursor position. A bookmark can be named any lowercase letter. You can't see the bookmark, but it's there!
'A
Go to bookmarkA. Important: That'sBacktick, Not a single quote. The backtick is located to the left of the 1 on most keyboards.
'.
Go to the line that you last edited. This is very useful! If you need to scroll through the file to look something up, you can go back to where you were without bookmarking it by using'.Command.
Typing efficientlyuse keyword completion
Vim has a very nice keyword completion system. this means that you can type part of a long word, press a key, and have Vim finish the word for you. for instance, if you have a variable calledIamalongandawkwardvarnameSomewhere in your code, you probably don't want to type the whole thing in every time you use it.
To use keyword completion, just type the first few letters of the string (e.g.Iamal) And press(That means hold downCTRLAnd typeN) Or. If Vim doesn' t give you the word you want at first, keep trying -- Vim will cycle through all completions it can find.
Enter insert mode intelligently
Most users new to VIM get into insert mode by typingI. This works, but it's often pretty inefficient, since VI has a host of commands that leave the editor in insert mode. Here are some of the more popular ones:
I
Insert text to the left of the current character.
I
Insert text at the beginning of the current line.
A
Insert text to the right of the current character.
A
Insert text at the end of the current line.
O
Create a new line under the current one and insert text there.
O
Create a new line above the current one and insert text there.
C{Motion}
Delete (CHange) The text moved over{Motion}And insert text to replace it. For instance,C $Wocould Delete the text from the cursor to the end of the line and enter insert mode.CT!Wocould Delete the text from the cursor up to (but not including) The next exclamation mark and enter insert mode. The deleted text is copied to the clipboard and can be pasted.
D{Motion}
Delete the text moved over{Motion}-- SameC{Motion}, But doesn' t enter insert mode.
Moving blocks of text efficientlyuse visual selections and the appropriate selection mode
Unlike the original VI, VIM allows you to highlight text and perform operations on it. There are three main visual selection modes (that is, text highlighting modes). These modes are as follows:
V
Characterwise selection mode. This is the selection mode that most people are used to, so practice with it before trying the others.
V
Linewise selection mode. Whole lines are always selected. This is better than characterwise mode when you want to copy or move a group of lines.
Blockwise selection mode.ExtremelyPowerful and available in very few other editors. You can select a rectangular block and any text inside that block will be highlighted.
All the usual cusor movement keys apply -- so, for instance,VwwwWocould go into visual selection mode and highlight the next three words.VjjWocould go into linewise visual selection mode and highlight the current line and the two lines below it.
Cutting and copying from visual selections
Once you have a highlighted selection, you probably want to do something with it. Some of the more useful commands you can give when an area of text is highlighted:
D
Cut (DElete) The highlighted text and put it into the clipboard.
Y
Copy (orYAnk, Which is vim-ese for "copy") The highlighted text into the clipboard.
C
Cut the highlighted text into the clipboard. This is just likeD, Should t it leaves the editor in insert mode.
Cutting and copying from non-visual selections
If you know exactly what you want to copy or cut, you can do it without entering visual mode. This saves time.
D{Motion}
Cut the text moved over{Motion}To the clipboard. For instance,DWWocould cut a word andDFSWocould cut from the cursor up to and including the next capital S on the current line of text.
Y{Motion}
Copy the text moved over{Motion}.
C{Motion}
Cut the text moved over{Motion}And leave the editor in insert mode.
Dd
Cut the current line.
YY
Copy the current line.
CC
Cut the current line and leave the editor in insert mode.
D
Cut from the cursor to the end of the current line.
Y
Yank the whole line, just likeYY. (Yes, It's inconsistent! You can useY $To do what you wowould perform CTYTo do .)
C
Cut from the cursor to the end of the current line and leave the editor in insert mode.
X
Cut the current character. (This is sort of like a command-mode backspace .)
S
Cut the current character and leave the editor in insert mode.
Pasting
Pasting is easy. Put the cursor where you want the pasted text and typeP.
Using multiple clipboards
Most editors have a single clipboard. Vim has more; clipboards in Vim are calledRegisters. You can list all the currently defined registers and their contents by typing: Reg. Typically, you'll be using the lowercase letter registers; the others are used for varous internal Vim purposes and are only occasionally helpful.
To use a specific register for a copy or paste operation, simply type"Before the command for the operation, whereAIs the register you want to use.
For example, to copy the current line into RegisterK, You cocould type"Kyy. (You coshould also typeV "Ky. Why wocould that work ?). That line wocould stay in registerKUntil you specifically copied something else into RegisterK. You wowould then use"KPTo paste the text from registerK.
Avoiding repetitionthe amazing .Command
In VI, typing.(A period) will repeat the last command you gave. For instance, if your last command wasDW(Delete word), VI will delete another word.
Using counts
Counts are one of the most powerful and time-saving features of vim.AnyCommand can be preceded by a number. The number will tell Vim how many times to execute the command. Here are a few examples:
3jWill move the cursor down three lines.
10ddWill delete ten lines.
Y3 "E;Will Yank (copy) text from the cursor to the third quotation mark after the cursor on the current line. counts are useful to extend the range of a motion in this manner.
Recording macros
Occasionally, you'll find yourself doing the same thing over and over to blocks of text in your document. Vim will let you record an ad-hoc macro to perform the operation.
QRegister
Start macro recording into the named register. For instance,QAStarts recording and puts the macro into RegisterA.
Q
End recording.
@Register
Replay the macro stored in the named register. For instance,@Replays the macro in registerA.
Keep in mind that macros just record your keystrokes and play them back; they are not magic. recording macros is almost an art form because there are so large commands that accomplish a given task in Vim, and you must carefully select the commands you use while your macro is recording so that they will work in all the places you plan to execute the macro.
Writing code in VIM
Vim is an excellent editor for source code because it has features that are specifically designed to help programmers. Here are a few of the more handy ones:
] P
Just likeP, But it automatically adjusts the indent level of the pasted code to match that of the code you paste into. Try it!
%
Putting the cursor on a brace, bracket, or parenthese and pressing%Will send the cursor to the matching brace, bracket, or parenthese. Great for fixing parse problems related to heavily nested blocks of code or logic.
>
Indent the highlighted code. (See the earlier section about efficient text selection. If no text is selected, the current line is indented .)
<
Like >>, but un-indents.
GD
GO toDEfinition (orDEclaration) of the function or variable under the cursor.
K
Go to the man page for the word currently under the cursor. (for instance, if your cursor is currently over the wordSleep, You will see the man pageSleepDisplayed .)