vim tips

Keyword Lookup
The K command is designed to look up the selected text using the “man” command. It
works just like the normal-mode K command except that it uses the highlighted text
as the keyword.


Inserting Text
The command Istring<Esc> inserts the text on each line starting at the left side of
the visual block, as seen in Figure 6.9.
You start by pressing CTRL-V to enter visual block mode. Now you define your
block. Next you type I to enter insert mode followed by the text to insert. As you
type, the text appears on the first line. After you press <Esc> to end the insert, the text
will magically be inserted in the rest of the lines contained in the visual selection.
Figure 6.9 shows how this process works.
If the block spans short lines that do not extend into the block, the text is not
inserted in that line. Figure 6.10 illustrates what happens to short lines.
If the string contains a newline, the I acts just like a normal-mode insert (i) com-
mand and affects only the first line of the block.

The Vim editor has lots of commands that help the programmer indent his program
correctly.The first ones discussed here merely shift the text to the left (<<) or the
right (>>).
The left shift command (<<) shifts the current line one shift width to the left.The
right shift command (>>) does the same in the other direction. But what is a shift
width? By default, it is 8. However, studies have shown that an indentation of 4 spaces
for each level of logic is the most readable. So a shift width of 4 would be much nicer.
To change the size of the shift width, use the following command:

block = indent


The = Command
The =motion command indents the selected text using Vim’s internal formatting pro-
gram. If you want to indent a block of text, for example, you can use the = command
to do it.The motion in this case is the % (go to matching {}) command. Figure 7.5
shows the results.


Shifting a Block of Text Enclosed in {}
Suppose that you want to indent the text encoded in {} one level. Position the cursor
on the first (or last) {.
Execute the command >%.This shifts the text right to where the motion takes you.
In this case, % takes you to the matching {}.
Figure 7.14 shows how these commands work.

Unfortunately this shifts the {} in addition to the text. Suppose you just want to shift
what is in the {}.Then you need to do the following:
1. Position the cursor on the first {.
2. Execute the command >i{.

The K command gets the subject from the word under the cursor. But what if you need
to select the section number? It is simple; the K command takes a count argument. If
specified, this is used as the section number.Therefore, if you position the K over the
word mkdir and execute the 2K, you will get the mkdir(2) page.
You can customize the K command. It runs the program specified by the
'keywordprg' option. By default, on UNIX this is man.


The command :cprevious or :cNext goes to the previous error. Similarly, the com-
mand :clast goes to the last error and :crewind goes to the first.The :cnfile goes to
first error message for the next file (see Figure 7.28).
If you forget what the current error is, you can display it using the following
command:
:cc
To see a list of errors, execute this command:
:clist
Figure 7.29 shows the output of a :clist command.
If you want to list only a subset of the errors, you can give :clist a range of errors
to list. For example:
:clist 3,5
:clist ,5
:clist 5,
(List errors 3 through 5)
(List errors 1-5)
(List errors 5 to the end)
The Vim editor suppresses all informational messages. If you want everything, use the
following command:
:clist!
The override option (!) tells Vim to not suppress anything.

Saving Your Setting
After performing all your :map, :abbreviate, and :set commands, it would be nice if
you could save them and use them again.
The command :mkvimrc writes all your settings to a file.The format of this com-
mand is as follows:
:mkvimrc file
file is the name of the file to which you want to write the settings.
You can read this file by using the following command:
:source file
During startup, the Vim editor looks for an initialization file. If it is found, it is auto-
matically executed. (Only the first file found is read.)


Automatic Completion Details
For the most part, the Vim editor does the right thing when it comes to automatic
completion. At times, however, you might want to fine-tune your completions.
The ‘complete’ option controls where Vim searches for words.The form of this
option is as follows:
:set complete=key,key,key
key is a key letter (and possible argument).The possible key values are as follows:
. Current file
b Files in loaded buffers, not in a window
d Definitions in the current file and in files included by a #include directive
i Files included by the current file through the use of a #include directive
k The file defined by the ‘dictionary’ option (discussed later in this chapter)
kfile The file named {file}
t The “tags” file. (The ] character can be used as well.)
u Unloaded buffers
w Files in other windows



The S Command
The S command deletes the current line and puts the editor in insert mode. If a count
is specified, it deletes count lines.This differs from the C command in that the C com-
mand deletes from the current location to the end of the line, whereas the S command
always works on the entire line. Figure 18.18 illustrates the use of the S command.

Instant Word Searches
The * command searches for the word under the cursor. For example, position the
cursor on the first const. Pressing * moves the cursor to the next occurrence of the
word, specifically line 26. Figure 19.5 shows the results.


Special Registers
Vim has a number of special registers.The first is the unnamed register, whose name is
double quote (“).
Others include the registers 1 through 9. Register 1 contains the last text you
deleted; register 2 the next to last, and so on.
(Back in the bad old days of Vi, these registers were a lifesaver.You see, Vi had only
one level of undo. So if you deleted three lines by executing dd three times, you were
out of luck if you wanted to undo the delete using the u command. Fortunately, the
three lines were stored in registers 1, 2, and 3, so you could put them back with
“1P”2P”3P.You can also use the command “”P.. (“”P and two dots).
Other special registers include the following:
Register Description Writeable
0 The last yanked text Yes
- The last small delete No
. The last inserted text No
% The name of the current file No
# The name of the alternate file No
/ The last search string No
: The last “:” command No
_ The black hole (more on this later) Yes
= An expression (see next page) No
* The text selected with the mouse Yes

Spaces and Tabs
If you are using a combination of tabs and spaces, you just edit normally.The Vim
defaults do a fine job of handling things.
But you can make life a little easier by setting the ‘softtabstop’ option.This
option tells Vim to make the Tab key look and feel as if tabs were set at the value of
‘softtabstop’, but use a combination of tabs and spaces to fake things (see Figure 23.3).
After you execute the following command, every time you press the Tab key the
cursor moves to the next 4-column boundary:
:set softtabstop=4
The first time you press it, however, you get 4 spaces inserted in your text.The second
time, Vim takes out the 4 spaces and puts in a tab (thus taking you to column 8).


Smart Tabs
Another related option is the ‘smarttab’ option.With this option on (:set smarttab),
tabs inserted at the beginning of a line are treated like soft tabs.The tab size used in
this case is defined the ‘shiftwidth’ option.
But tabs inserted elsewhere in the text act just like normal tabs. Note that you must
have soft tabs off (:set softtabstop=0) for this option to work. Figure 23.4 shows
sample results.
Smart indenting is a combination of soft tabs and normal tabs.When you execute
the following command, Vim treats tabs at the beginning of a line differently:
:set smarttab
Suppose, for example, that you have the following settings:
:set shiftwidth=4
:set tabstop=8
:set smarttabs
Tab stops are every eight spaces and the indentation size is four spaces.When you type
<Tab> at the beginning of a line, the cursor will move over the indentation size (four
spaces). Doing a double <Tab> moves over two indention sizes (eight spaces [4*2]).


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章