Google

Lubee's Blog--Digtial IC Fan's Home

WELCOME IC FANS' HOME! --ASIC/IC Design,Logical Design,STA,Digital IC Design,Synthesis, and so on.

2007-12-19

Vim Study Book(part 2)--Cursor motion

Cursor motion

The mode

When you want to move the cursor, first go to command mode. Although you can use Ctrl combined keys to move the cursor in insert mode, they just inferior ways. I suggest never using Ctrl combined commands to move cursor. By all means, don't think about the arrow keys and the mouse when trying to move the cursor.


h j k l

h j k l are the four most basic cursor motion commands. You should use the index finger to press h and j, use your middle finger for k and your ring finger for l. These commands are designed for efficiency of your work and the comfort of your hand, but not so that you can do without learning. You should practice until they become of your instincts. Try open a file and use only your right hand to browse the whole file and move the cursor around, without looking at the keyboard. To practice effectively, never use the arrow keys and the mouse.


Faster

After you get used to h j k l, you may find these commands a bit slow. Yes, they are for short range movements. To move quickly, you need to learn other commands.

The w command moves cursor forward by a word, while b command moves cursor backward by a word. If you're locating a target in a single line, these two might be useful.

The 0 command moves the cursor to the beginning of the current line, while $ command moves the cursor to the end of the current line. The ^ command moves the cursor to the first non-blank character of the current line.



Even faster

If you want to go to the end of the file, press G. If you want to go to the beginning of the file, press 1G.

If you want to go to the nth line, type the number n followed by G. For example, 150G takes you to the 150th line. If you don't have as many as 150 lines, you won't be punished -- the cursor just jump to the last line.

There is an alternative command to jump to a line if you know its number. Type the colon character : followed by the line number and ENTER, such as :150. This has the same effect as the G command, but the advantage is that the colon and the line number you have typed are shown at the bottom of the screen.

But how do we know the number of a line? Usually we don't know. Therefore, usually we only use G and 1G, which take us to the end or the beginning of the file. Of course, you may have an estimate of the line number where you want to go. Then just go there and then use j k to adjust the cursor location further.

If you do want to use line numbers precisely, you can type :set number to let vim display line numbers on the screen. The line numbers on the screen are not part of your file and can be hidden by type :set nonumber.

A good place to use line numbers is when you compile and debug programs. The compiler usually report the number of the lines where errors occur. Using those numbers you can jump directly to the error spot.

Move by screen.
Press Ctrlf moves you forward by one screen of text, and Ctrlb moves you backward by one screen of text.

Ctrlf and Ctrlb are faster, but pressing Ctrl key can be tiring. To avoid that, you may use the following commands to map Space key and Backspace key to them respectively.


:map
:map

Move to the matched bracket
The % command is a special cursor motion command. When the cursor is at any one of the bracket characters ( ) [ ] { }, hitting the % takes the cursor to its match, if there is one. If you're a C programmer, you'll find this very useful.



Know what you really want

Sometimes what you want to do is not just moving cursor. Then perhaps there is a command which combines your desired cursor motion and your other intention. For example, you may try to move the cursor to the right by l and then press i to insert. But that two operations is what the command a for. You may want to move cursor to the end of the line by $ and then use a to enter insert mode, but you can do it by one command A.

There are a lot of editing commands which combine cursor motions with other functions such as a A I o O cw C s. Try using them instead of doing two the things separately.



Move cursor by searching a pattern

Sometimes you don't know where your target is, but you know what your target is. Then don't browse the file from start to end, word by word. That is harmful to your eyes. Try searching the word or the pattern using the slash command /. For example, you want to move the cursor the word target, you can type
/target

Then the cursor will jump to the first occurrence of target. If this target is not your target, just keep pressing
n
to go to subsequent occurrences of the word target untill you find your target.

Search is the most effective and fastest way of moving the cursor and locating the target, especially when you're doing things like writing HTML documents or LaTeX source files. The formatted documents are shown in one window, where the source files are edited by vim. When you watch the formatted documents and spot some problems, remember a couple of words from the place. Then go to the source file search these words to find the place.

When you edit large files, you can insert some location tags which can help you navigate through the file easily. For example, in a LaTeX file, you may insert comments like %Section 1 and %Section 2. Whenever you want to go to the start of Section 1, type

/Section 1


Move cursor by searching a character

Search a character within the line. The f and F commands can be used to move cursor quickly. To move the cursor to the first occurrence of a character c in the current line
fc
To go to the first occurrence of a character c in the left
Fc
If one fc didn't take the cursor to the desired place and you need to repeat the search, remember that instead of doing fc again, you can use the semicolon command to repeat the last fc or Fc search
;
in the same direction, and use the comma command
,
to do it in the opposite direction. This can be very handy since the semicolon and comma keys are very easy to hit.

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home