
• Redo by
Ctrl
+
R
.
•
.
repeats the last action. This can be a sequence
of actions you do in insert mode.
3.16 Splitting Window
Split a window horizontally using :sp (split). Split a win-
dow vertically using :vs (vertical split). Precede the com-
mand with a number (e.g. :10sp) to control the height (or
width) of the first window.
Combining split and edit command, we have :new and
:vnew. Cycle through the list of files using :bn and :bN.
Move to the adjacent windows using
Ctrl
+
W
and
then cursor key. Remove a window using :q. Adjust the
size of the window using
Ctrl
+
W
{
+
,
-
,
>
,
<
}
3.17 Navigation and Marks
You jump to elsewhere in a file or another file for various
reasons. Searching, or simply press the wrong key, brings
you away from where you are working. To go back, press
Ctrl
+
O
(‘oh’). To go forward, press
Ctrl
+
I
.
To mark a position, press
m
hci. This saves the loca-
tion in mark hci. To go back to that location press
‘
hci.
3.18 Search and Replace
• Perform a search using
/
hpatterni.
• Perform a replace using
:hris/hpatterni/hstringi/hflagsi
where
hri defines the range
– % : the whole file
– a,b : from line a to line b
– ’<,’> : range equals to lines highlighted (in-
serted automatically in visual mode)
hpatterni is the pattern to search for. Vim accepts
regular expression. Include \c in search pattern to
ignore case in search.
– gr[ae]y matches both gray and grey.
– red\|blue matches both red and blue.
– ^ matches beginning of line, $ matches end of
line.
hstringi is the string to replace with. You can use
backreference (\1, \2, . . . ) to refer back to the string
matched (enclosed in \( . . . \)).
– :%s/ \([4-9]\)/\1th/g replaces 4 with 4th, 5
with 5th, etc.
hflagsi modifies the behavior of a search. You can
mix some of them.
– g Replace all occurrence in the line. Without
this only the first matched string in each line
will be replaced.
– c Confirm each substitution.
– n Just report the number of matches, substitu-
tion is not done. (version 7.0 or above)
3.19 Constant manipulation
Sometimes you have constants in your program that you
want to modify. The shortcut
Ctrl
+
A
add one to the
number and
Ctrl
+
X
subtract one from the number.
The advantage of using this is your cursor don’t have to be
right on top of the number, it just search first number after
the cursor on the same line and do the job. This function,
by default, recognize decimal numbers, 0 for octal numbers
and 0x for hexadecimal numbers.
3.20 Retabbing for printing
By default, tab is equal to 8 spaces when printed. This
causes printouts hard to read when you have more than a
few level of indentation. Retab is the solution. Perform
the following:
’ Specify how much spaces one tab is equivalent
’ to (Not required if you are happy with the
’ current settings)
:set ts=4
’ With expandtab, tabs are converted to spaces
:set expandtab
’ Replace tabs with white spaces
:ret
’ Save and do your printing, e.g.
’ lpr -Popenlab xx.c
’ With noexpandtab, spaces are converted to tabs
:set noexpandtab
’ Undo changes to your code using ’u’, or
:ret
3.21 Jump to declaration
g
d
and
g
D
go to local and global declaration re-
spectively. Place the cursor on a variable or a function and
these commands will bring you to the declaration. (Go
back using
Ctrl
+
O
)
Note that these function is not guaranteed to work as it
may bring you to the wrong place (e.g. when you define
the function multiple times).
Display the definition of the current variable by
[
i
.
3.22 Using ctags
ctags is a powerful utility help to analyze the code and
help the programming process. Create a tag file using
ctags:
ctags *.c *.h
The tag file defaults to tags. After that you can
use
Ctrl
+
]
to ask for declaration. Go back by
Ctrl
+
T
. ctags is more powerful than
g
d
and
相关文档
评论