Introduction to Programming in C/C++ with Vim
Written By: Kmj
×÷Õß:Kmj
[СÕÔ] ·ÒëÕûÀí
Vi has been one of the most, if not the most, popular editing tools for programmers
since Bill Joy first created it.
×Ô´ÓBill Joy×î³õд³öVi±à¼Æ÷ÒÔÀ´, Vi¾ÍÒ»Ö±ÊDZà³ÌÕßÖÐ×î¹ãΪÁ÷´«µÄÎı¾±à¼
¹¤¾ß, ¼´Ê¹²»ÊÇ×îÁ÷ÐеÄ, Ò²Ò»¶¨ÊÇ×îÁ÷ÐÐÕßÖ®Ò».
Over the years it has evolved, and the current version of vim has many capabilities
which make a programmer's life easy. Listed below is a brief description
of some tools which have made so many programmers loyal to vi and vim. The
purpose of this document is to inform linux newbies of, and introduce them
to these tools, not necessarily to be the definitive source of information
on them. In most cases, interested readers should check the noted "extra
information" sources.
Vi²úÉúÒÔÀ´, Àú¾²»¶Ï¸ïÐÂ, ÏÖÔÚ×îаæµÄVimÒѾ¾ßÓÐÁ˷dz£¶àµÄ¹¦ÄÜ, ÕâЩ¹¦
ÄÜʹ³ÌÐòÔ±Äܸü¼ÓÇáËÉ, ±ã½ÝµØÊ¹ÓÃËüÃÇ. ÏÂÃæÊÇËüµÄһЩ¹¦ÄÜÃèÊö, ÕýÊÇÕâЩ
·á¸»Ç¿´óµÄ¹¦ÄÜʹviºÍvim³ÉΪÎÞÊý³ÌÐòÔ±µÄÖÁ°®. ±¾ÎÄÖ¾ÔÚÏòlinuxµÄ³õѧÕßÃÇ
½éÉÜÕâЩ¹¦ÄÜ, ¶ø²»ÊÇ×·ËÝÆäÀúÊ·Ô¨Ô´. ¶Ô´Ë¸ÐÐËȤµÄ¶ÁÕß¿ÉÒÔ
²é¿´"extra information"»ñµÃÕâЩÐÅÏ¢.
(Note that throughout this paper, I may use the name vi when referring to
vim. Some options may only be compatible with vim, and not vi.)
(×¢: ±¾ÎÄÖÐʹÓÃvi¼æÖ¸vim, µ«ÓÐһЩѡÏî¿ÉÄÜÖ»ÓÐvimÖ§³Ö)
Ctags
Ctags is a program that comes with vim. Basically, it's purpose is to help
a programmer get to various parts of his program with relative ease. The
typical method of running ctags is by simply typing the following in your
source directory:
CtagsÊÇvimµÄ°éÉú¹¤¾ß, ËüµÄ»ù±¾¹¦ÄÜÊÇÈóÌÐòÔ±ÄÜ×ÔÓÉ´©ËóÓÚ³ÌÐòµÄ²»Í¬²¿·Ö(
Èç´ÓÒ»¸öº¯ÊýÃûÌø×ªµ½¸Ãº¯ÊýµÄ¶¨Òå´¦), ×îͨ³£µÄÓ÷¨ÊÇÏóÏÂÃæÕâÑùÒÔÔ´³ÌÐòÄ¿
¼ÏÂËùÓÐÎļþ×÷Ϊ²ÎÊý.
[/home/someuser/src] $ ctags *
This will create a 'tags' file in you current directory with the following
information for all C or C++ files in you directory. This file contains
a listing of the following objects:
¸ÃÃüÁî»áÔÚµ±Ç°Ä¿Â¼Ï´´½¨Ò»¸öÃûΪ`tags'µÄÎļþ, ¸ÃÎļþ°üº¬ÁËÄ㵱ǰĿ¼ÏÂ
ËùÓеÄC/C++ÎļþÖеÄÏà¹ØÐÅÏ¢, ¾ßÌåÀ´Ëµ°üº¬ÒÔ϶ÔÏóµÄÐÅÏ¢:
macros defined by #define
enumerated values
function definitions, prototypes, and declarations
class, enum, struct, and union names
namespaces
typedefs
variables (definitions and declarations)
class, struct, and union members
ÓÉ#define¶¨ÒåµÄºê
ö¾ÙÖµ
º¯Êý¶¨Òå, ÔÐͺÍÉùÃ÷.
Àà, ö¾ÙÀàÐÍÃû, ½á¹¹ÃûºÍÁªºÏ½á¹¹Ãû
Ãû×Ö¿Õ¼ä
ÀàÐͶ¨Òå
±äÁ¿(¶¨ÒåºÍÉùÃ÷)
Àà,½á¹¹ºÍÁªºÏ½á¹¹µÄ³ÉÔ±
Vim then uses this file to help you locate these tagged items. This can be
done in a few ways. First, one can open vim to the location of a tagged
object. To do this, run vim with the '-t' flag and the tag-name as an argument
. For example:
½ÓÏÂÀ´, Vim¾Íͨ¹ý¸ÃÎļþÖеÄÐÅÏ¢¶¨Î»ÕâЩ³ÌÐòÔªËØ. Óм¸ÖÖ·½·¨¿ÉÒÔ¶ÔÕâЩԪ
ËØ½øÐж¨Î». µÚÒ»ÖÖ·½·¨, ¿ÉÒÔÔÚÃüÁîÉÏÆô¶¯vi³ÌÐòʱͨ¹ý-tÑ¡Ïî¼ÓÒªÌø×ªµÄ³ÌÐò
ÔªËØÃû, ÈçÏÂ:
[/home/someuser/src] $ vi -t foo_bar
would open to the file containing the definition of foo_bar at that line
.
½«»á´ò¿ª°üº¬foo_bar¶¨ÒåµÄÎļþ²¢¶¨Î»µ½¶¨Òåfoo_barµÄÄÇÒ»ÐÐÉÏ.
If you are already in vi, you can enter the command by the tag-name:
Èç¹ûÄãÒѾÔÚvi±à¼»·¾³ÖÐ, Ò²¿ÉÒÔÔÚµ×ÏßÃüÁîÐÐÉϼüÈë:
:ta foo_bar
This may take you out of the file you are currently in, unless the 'autowrite
' option is enabled. (It is off by default.) For more information on autowrite
, type ':h autowrite' from with vi command-mode.
¸ÃÃüÁî¿ÉÄÜʹÄãÀ뿪Ä㵱ǰ´ò¿ªµÄÎļþ(¶øÌø×ªµ½°üº¬foo_bar¶¨ÒåµÄÎļþµÄÏà¹ØÐÐ
ÉÏÈ¥, Èç¹ûÄãÒѾ¸Ä±äÁ˵±Ç°ÎļþµÄÄÚÈݶøÃ»ÓдæÅÌ, ÔòÖ»ÄÜÔÚÄãÉèÖÃÁË
`autowrite'ʱ²Å»áÌø×ªµ½¸ÃÎļþ, ·ñÔò»á¸ø³ö¾¯¸æ, Áí, autowrite¿É¼òдΪµÈЧ
µÄaw, ÒëÕß×¢), ÓûÁ˽â`autowrite'Ñ¡ÏîµÄÏêϸÐÅÏ¢, ¿ÉÒÔʹÓÃÔÚÏß°ïÖú:h autowrite
ÃüÁî(Ò²¿É¼òдΪ:h aw, ÒëÕß×¢)
The final way to jump to a tagged location is by typing 'Ctrl-]' in command
mode while the cursor is on a specific word. For example, if you are looking
at my program, and you come across a point where I call foo_bar(), you can
type 'Ctrl-]', while the cursor is somewhere on that word, and it will jump
to that definition. Note that 'Ctrl-]' is the escape character for telnet
, so this may cause some issues if your editing files remotely. Type ':h
^]' for more information.
×îºóÒ»ÖÖÌø×ªµ½Ò»¸ö³ÌÐòÔªËØµÄ·½·¨ÊÇÔÚ(ÃüÁîģʽÏÂ)¹â±êÍ£ÔڸóÌÐòÔªËØÉÏʱ°´
ÏÂ`CTRL-]'¼ü, Èç, ÄãÔÚ¿´³ÌÐòʱ¿´µ½Ä³´¦µ÷ÓÃÁËÒ»¸ö½Ðfoo_bar()µÄ³ÌÐò, Äã
¿ÉÒÔ½«¹â±êÍ£ÔÚfoo_barµ¥´ÊÉÏ(Í£Ôڸõ¥´ÊÈκÎÒ»¸ö×Ö·û¶¼¿É, ÒëÕß×¢), È»ºó°´
ÏÂ`CTRL-]'¼ü, Ëü¾Í»áÌø×ªµ½¸Ãº¯ÊýµÄ¶¨Òå´¦. ÖµµÃ×¢ÒâµÄÊÇCtrl-]ÅöÇÉÊÇtelnet
µÄÖÕ¶Ë·û, ËùÒÔÈç¹ûÄãÔÚ±à¼Ô¶³Ì¼ÆËã»úÉϵÄÎļþ(ͨ³£ÊÇͨ¹ýtelnetµÇ¼µ½Ô¶³Ì
Ö÷»úÉÏ, ÒëÕß×¢), ¿ÉÄÜ»áÓöµ½Ò»Ð©ÎÊÌâ. ͨ¹ýÔÚÏß°ïÖú':h^]'¿ÉÒÔÁ˽âÕâ·½ÃæµÄ
¸ü¶àÐÅÏ¢.(ÒëÕß: ÔÚ:h^]ÖйØÓÚ¸ÃÎÊÌâÊÇÕâÑù˵µÄ, ¶àÊýtelnet¶¼ÔÊÐíʹÓÃÃüÁî
telnet -E hostnameÀ´´ò¿ª»ò¹Ø±Õ¸ÃÍÑ×Ö·û, »òÕßÓÃtelnet -e escape hostname
À´Ö¸¶¨ÁíÍâÒ»¸öÍÑ×Ö·ûÀ´´úÌæ^], ´ËÍâ, Èç¹û¿ÉÄܵϰ, ¿ÉÒÔʹÓÃrshÀ´´úÌætelnet
À´±ÜÃâÕâ¸öÎÊÌâ, ¹ØÓÚtelnet -E ¼° telnet -eµÄÏêÇé, Çë²Î¿´man telnetÖÐÏà¹Ø
µÄ°ïÖú)
Ctags can also be used with other languages (java, fortran, ...and more) and
editors (emacs, NEdit, ...and more). When set up properly, this tool can
make your job tremendously easier, especially when you have to jump into
a large ongoing project head-first.
Ctags³ÌÐòÒ²¿ÉÓÃÓÚÆäËüÓïÑÔд¾ÍµÄÔ´³ÌÐò, ²¢ÇÒ¿ÉÒÔÓëÆäËüµÄһЩ±à¼Æ÷(Èçemacs
, NEditµÈµÈ)Ðͬ¹¤×÷. ÕýÈ·µØÊ¹ÓÃËü, »á¸øÄãµÄ±à³Ì¹¤×÷´øÀ´¼«´óµÄ±ãÀû, ÓÈÆäÊÇÄãÔÚ
¿ª·¢Ò»¸ö´óµÄÏîĿʱ.
For more information: View the man page, man ctags, or view the vim help
, :h ctags.
¹ØÓÚctags³ÌÐòµÄ¸ü¶àÓ÷¨, Çë²Î¿´ËüµÄÏà¹Ø°ïÖúÒ³, man ctags, »òÕßͨ¹ývimµÄ
ÔÚÏß°ïÖúϵͳ²é¿´ËüµÄÓ÷¨, :h ctags
c-style indenting
Vi has various methods of implementing auto-indenting. The best for C and
C++ programmers is, obviously, cindent mode. This is a very versatile tool
which gives the programmer much control over the look and feel of his source
code, without any effort (except the effort of initial setup, of course
). To enable c-indenting, just type ':set cindent' from the command mode
. The most important thing to note is that cindenting makes use of shiftwidth
, not tabstops. The default shiftwidth is 8. In order to change this, enter
':set shiftwidth=x' where x is the desired number of spaces to shift.
ViÓм¸ÖÖ²»Í¬µÄ·½·¨ÊµÏÖ×Ô¶¯Ëõ½ø. ¶ÔÓÚC/C++³ÌÐòÔ±À´Ëµ, ×îºÃµÄ·½·¨ÏÔÈ»ÊÇ
cindentģʽ, ¸Ãģʽ¾ßÓжàÖÖ¹¦ÄܰïÖú³ÌÐòÔ±ÃÀ»¯³ÌÐòµÄÍâ¹Û, ÎÞÐèÈκζîÍâµÄ
¹¤×÷(µ±È», ÉèÖÃÕýÈ·µÄģʽ:se cindentÊDZØÐèµÄ). Óû´ò¿ª¸Ãģʽ, Ö»Ðè¼üÈëÃü
Áî:set cindent(ËùÓеÄset¶¼¿ÉÒÔ¼òдΪse, ËäȻֻ½ÚÊ¡ÁËÒ»¸ö×Ö·û, ÒëÕß×¢)
ÐèҪעÒâµÄÊÇcindent¿ØÖÆËõ½øÁ¿ÊÇͨ¹ýshiftwidthÑ¡ÏîµÄÖµ, ¶ø²»ÊÇͨ¹ýtabstop
µÄÖµ, shiftwidthµÄĬÈÏÖµÊÇ8(Ò²¾ÍÊÇ˵, Ò»¸öËõ½øÎª8¸ö¿Õ¸ñ, ÒëÕß×¢), Òª¸Ä±ä
ĬÈϵÄÉèÖÃ, ¿ÉÒÔʹÓÃ`:set shiftwidth=x'ÃüÁî, ÆäÖÐxÊÇÄãÏ£ÍûÒ»¸öËõ½øÁ¿´ú±í
µÄ¿Õ¸ñµÄÊýÄ¿.
The default cindent options tend to be nice, but if you find your program
indenting in some way that is annoying, you can modify the behaviour. To
set the cindent options, type ':set cino=string', where string is a
list defining exactly how you want your cindent options to behave. There
are quite a few different types of indenting which can be done, and vim
's help does a good job explaining them, so I won't go over them here. To
view the help for the possible values of cinoptions, type ':h cinoptions
-values'. To view the current values, simply type ':set cino'. Most likely
there will be none, since everything starts at a default value.
cindentµÄĬÈÏÉèÖÃÑ¡ÏîÒ»°ãÀ´ËµÊDZȽϿÉÈ˵Ä, µ«Èç¹ûÄãµÄ³ÌÐòÓÐÌØÊâÐèÇó, Ò²
¿ÉÒԸıäËü, ÉèÖÃcindentµÄÑ¡Ïî, ͨ¹ý`:set cino=string'Ñ¡Ïî(ÆäÖÐ, string
ÊÇÒªÓû§×Ô¼º¼üÈëµÄ×Ö·û´®, ÒëÕß), string¶¨ÒåÁËÒ»¸öÁбí, ¸ÃÁбí¾ö¶¨ÁËÄã
µÄcindentµÄÐÐΪ. Äã¿ÉÒÔ¶¨Òå¶àÖÖindentÀàÐÍ, vimµÄ°ïÖú¶Ô´ËÓкÜÏêϸµÄ˵Ã÷.
Óû²éÕÒ¹ØÓÚ¸ÃÖ÷ÌâµÄ°ïÖú, ʹÓÃÃüÁî`:h cinoptions-values'. ÒªÏë²é¿´µ±Ç°µÄ
ÉèÖÃÖµ, ¿ÉÒÔʹÓÃÃüÁî`:set cino'.
For more information, check out the following: ':h shiftwidth', ':h cindent
', ':h cinoptions', ':h cinoptions-values', ':h cinkeys', and ':h cinwords
'.
ÒªÁ˽â¸ü¶àµÄϸ½Ú, ¿ÉÒÔʹÓÃÔÚÏß°ïÖú`:h shiftwidth', ':h cindent', `:h
cinoptions', `:h cinoptions-values', `:h cinkeys', ºÍ`:h cinwords'
syntax highlighting
Programmers who are used to integrated development environments know the
beauty of syntax highlighting. It doesn't just make your code more readable
; it also helps prevent annoying commenting and string errors. Vim has syntax
highlighting for a number of languages, including C and C++ of course. To
enable it, type ':syntax on'. Using it is as simple as that if you're happy
with the default values. Vim's syntax highlighting tools can be quite complex
, with a number of different things to play around with. To view information
on syntax hilighting, type ':h syntax', which will lead you to vim's extensive
help system. Syntax hilighting with color terminals and with gvim is nice
, but if you don't have color, vim uses underlining, boldface, etc. To me
, this is pretty ugly.
Óùý¼¯³É¿ª·¢»·¾³µÄ³ÌÐòÔ±¶¼ÖªµÀÓï·¨¸ßÁÁµÄÃî´¦ËùÔÚ, Ëü²»¹âʹÄãµÄ´úÂë¸ü¾ß
¿É¶ÁÐÔ, ËüҲʹÄãÃâÓÚÆ´Ð´´íÎó, ʹÄãÃ÷È·×¢Ê͵ķ¶Î§, Vim¶Ô¶àÖÖÓïÑÔ¶¼ÓÐÓï·¨
¸ßÁÁµÄ¹¦ÄÜ, µ±È», C/C++Ò»¶¨°üÀ¨ÔÚÄÚ, ´ò¿ªÓï·¨¸ßÁÁ¹¦ÄÜ, ¿ÉʹÓÃÃüÁî`:syntax on'.
Èç¹ûÄã¾õµÃĬÈϵÄÉèÖÃÒѾ¹»ºÃÁË, ʹÓÃËü¾ÍÊÇÈç´Ë¼òµ¥. VimµÄÓï·¨¸ßÁÁ¹¤¾ßÒ²
¿ÉÒÔÊ®·Ö¸´ÔÓ, ÓµÓÐÖÚ¶àÑ¡Ïî. ÒªÁ˽â¸ü¶àµÄϸ½Ú, ¿Éͨ¹ýÃüÁî`:h syntax'²é¿´
ÔÚÏß°ïÖú, ÔÚÖ§³Ö²ÊÉ«µÄÖÕ¶ËÉÏ»òÕßʹÓÃgvim(vimµÄGUI°æ, ÔöÇ¿ÁËһЩ¹¦ÄÜ, Òë
Õß×¢), µ«Èç¹ûÄ㵱ǰµÄ»·¾³²»Ö§³Ö²ÊÉ«ÏÔʾ, vim»áʹÓÃÏ»®Ïß, ´ÖÌå×Ö, ÊÔͼ
½øÐеÈЧµÄÌæ´ú, µ«¶ÔÎÒ¶øÑÔ, ÕâÑùÌ«ÄÑ¿´ÁË.
For more information: ':h syntax', ':h syn-qstart', ':h syntax-printing
'
ÒªÁ˽â¸üÏêϸµÄÄÚÈÝ, ¿Éͨ¹ýÃüÁî`:h syn-gstart', ':h syntax-printing'²é¿´
ÔÚÏß°ïÖú
edit-compile-edit, a.k.a. Quickfix
This is a really nifty feature. Basically, by typing one command, you can
cause vim to attempt to make the program you're working on, then open to
whatever file first compiler error is in at the line of that error. The
command to execute is ':mak' (or ':make'). Vim will run whatever program
is denoted by the value of 'makeprg'. The default value for 'makeprg' is
'make'. You can change this, if you wish, by typing ':set makeprg=', where
string denotes the desired command. Vim uses the 'errorformat' value to
figure out how to understand the output from the compiler. Since different
compilers have different output format's, you'll probably have to enter
the format string. The method used is rather similar to C-style formatting
with scanf. The most important format specifiers are %f, meaning filename
, %l, meaing line-number, and %m, meaning message.
ÕâʵÔÚÊǼ«ºÃµÄ¹¦ÄÜ, Æä»ù±¾¹¦ÄÜÊÇ, Äã¿ÉÄܲ»ÓÃÀ뿪µ±Ç°±à¼»·¾³, ͨ¹ýÖ¸¶¨
Ò»¸öÃüÁî, ¾Í¿ÉÒÔ±àÒëÄ㵱ǰ±à¼µÄÏîÄ¿, È»ºó, Èç¹û±àÒëʱÒò·¢Éú´íÎó¶øÖжÏ,
vim½«»á´ò¿ªµÚÒ»¸ö·¢Éú´íÎóµÄÎļþ²¢¶¨Î»ÓÚÒýÆð´íÎóµÄÐÐÉÏ. ÕâÒ»ÃüÁî¾ÍÊÇ`:mak'
(»òÕß':make'). vim½«»áÔËÐÐÓÉÑ¡ÏîmakeprgÖ¸¶¨µÄmake³ÌÐò, ËüµÄĬÈÏÖµ¾ÍÊÇ
make(ÒѾ¹»ºÃÓÃÁË, ÒëÕß×¢). Èç¹ûÔ¸ÒâµÄ»°, ÄãÒ²¿ÉÒÔʹÓÃÃüÁî`:set makeprg
=string'¸Ä±äÏîĿά»¤¹¤¾ß(±ÈÈç, ÔÚVCÏÂʹÓÃnmake, :set makeprg=nmake.exe
, ÒëÕß×¢),
vimʹÓÃÑ¡Ïî`errorformat'µÄÉèÖÃÈ¥½âÎö±àÒëÏùÊä³öµÄ´íÎóÐÅÏ¢µÄ¸ñʽ. ÓÉÓÚ²»Í¬
µÄ±àÒëÏùÓв»Í¬µÄ´íÎóÐÅÏ¢¸ñʽ, ËùÒÔ¿ÉÄÜÐèÒªÏÔʽµØÖ¸¶¨´íÎóÐÅÏ¢µÄ¸ñʽ. Ñ¡Ïî
`errorformat'µÄÉèÖÃʹÓÃÓëcº¯Êýscanf·ç¸ñÀàËÆµÄÓï·¨, ×îÖØÒªµÄÊÇÖ¸¶¨%f, ´ú
±íÎļþÃû, %l, ÐкÅ, %m, ´íÎóÐÅÏ¢.
GCC's format string: %f:%l:\%m
GCC¸ñʽµÄerrorformatÉèÖÃ:%f:%l:\%m
This can become quite complex with different compilers, but fortunately, vim
has a world of information in their help at ':h errorformat'.
ÓÐЩ±àÒëÆ÷µÄerrorformat¿ÉÄÜÊ®·Ö¸´ÔÓ, µ«ºÃÔÚvim¶Ô´ËÌṩÁËÍêÕûµÄÔÚÏß°ïÖú
':h errorformat'.
For more information, check out: ':h quickfix', ':h mak', ':h makeprg', ':h
errorfile', ':h errorformat'.
ÒªÁ˽âÆäËüϸ½Ú, ¿ÉÓÃÃüÁî`:h quickfix', `:h mak', `:h makeprg', `:h errorfile
',
`:h errorformat'²é¿´ÏàÓ¦µÄ°ïÖú.
useful keystrokes
There are certain command-mode keystrokes that are especially useful for
programmers. Below is a small subset of these:
ÓÐһЩ¿ì½Ý°´¼ü¶Ô³ÌÐòÔ±¶øÑÔÌØ±ðÓÐÓÃ, ÏÂÃæÊÇÆäÖеÄÒ»²¿·Ö:
Moving around within functions:
[ [ = Go to previous first-column '{'; equivalent to ?^{
ÒÆ¶¯µ½Ç°Ò»¸öÐÐÊ×µÄ'{'×Ö·ûÉÏ, µÈ¼ÛÓÚ?^{] ] = Go to next first-column '{'; equivalent to /^{
ÒÆ¶¯µ½ÏÂÒ»¸öÐÐÊ×µÄ'{'×Ö·ûÉÏ, µÈ¼ÛÓÚ/^{[ ] = Go to previous first-column '}'; equivalent to ?^}
ÒÆ¶¯µ½Ç°Ò»¸öÐÐÊ×µÄ'}'×Ö·ûÉÏ, µÈ¼ÛÓÚ?^}
] [ = Go to next first-column '}'; equivalent to /^}
ÒÆ¶¯µ½ÏÂÒ»¸öÐÐÊ×µÄ'}'×Ö·ûÉÏ, µÈ¼ÛÓÚ?^}
{ = Go to previous blank line.
} = Go to next blank line.
gd = Go to definition of current local variable (current = cursor
is on it)
µ½µ±Ç°¾Ö²¿±äÁ¿µÄ¶¨Òå´¦(µ±Ç°µÄÒâ˼Êǹâ±êÍ£ÁôÆäÉϵĵ¥´Ê).
* = Go to next instance of current word
µ½Ó뵱ǰµ¥´ÊÏàͬµÄÏÂÒ»¸öµ¥´ÊÉÏ
# = Go to previous instance of current word
'' = Go to location where last search was started.
µ½Ó뵱ǰµ¥´ÊÏàͬµÄÉÏÒ»¸öµ¥´ÊÉÏ
Parenthesis Matching:
% Takes you to the matching parenthesis, curly brace, or bracket, depending
on what you are on. This always comes in handy as a quick double-check.
À¨ºÅÆ¥Åä:
% ¿ÉÒÔÈùâ±ê´ÓËüµ±Ç°ËùÔÚµÄÀ¨ºÅÌø×ªµ½ÓëËüÏàÆ¥ÅäµÄÀ¨ºÅÉÏÈ¥, ¶Ô»¨À¨ºÅºÍ
Ô²À¨ºÅ, ·½À¨ºÅ¶¼ÓÐЧ, ³£ÓÃÓÚÊÖ¹¤¼ì²éÀ¨ºÅÊÇ·ñÆ¥¶Ô.
Substution:
Vim has powerful substition capabilities, with a very simple interface. No
annoying GUI to get in the way (though you may need to keep a cheat-sheet
handy). To search for and replace text, use the following command:
Ìæ»»²Ù×÷:
Vim¾ßÓÐÇ¿´óµÄ×Ö·û´®Ìæ»»¹¦ÄÜ, ²Ù×÷ÆðÀ´Ê®·Ö¼òµ¥, ²»ÐèÈÇÈËÉúÑáµÄGUI(ͼÐÎÓÃ
»§½çÃæ), ²éÕÒ²¢Ìæ»»Îı¾, ¿ÉÒÔʹÓÃÏÂÃæµÄÃüÁî:
: [address] s/
/string/[g|c|N] (where N is an integer value).
This finds one (or more) instance of the grep-style regular expression represented
by
, and substitutes it with string. 'address', 'g', and 'N
' are modifiers which determine which and how many occurances of
are replaced.
´ËÃüÁî²éÕÒÓÉgrep·ç¸ñµÄÕýÔò±í´ïʽָ¶¨µÄÆ¥Åäģʽ
, ²¢½«ÆäÌæ»»ÎªÓÉ
stringÖ¸¶¨µÄ×Ö·û´®, `address', `g', ºÍ`N' ÊǶÔÃüÁîµÄ²¹³äÑ¡Ïî, ËüÃÇ·Ö±ð
¾ö¶¨ÁËÃüÁîµÄ×÷Ó÷¶Î§, ÊÇÖ»Ìæ»»µÚÒ»¸öÆ¥ÅäµÄ×Ö·û´®»¹ÊÇÌæ»»ËùÓÐÆ¥ÅäµÄ×Ö·û´®,
Ö»Ìæ»»Ã¿ÐÐÖеÚN´ÎÆ¥ÅäµÄ×Ö·û´®.
g = Global: Replace all occurances of
on the line.
È«²¿: Ìæ»»Ã¿ÐÐÖÐËùÓÐÆ¥ÅäµÄ×Ö·û´®.
c = Cond. Ask before making each replacement.
ѯÎÊ. ÔÚÿ´ÎÌæ»»Ö®Ç°Ñ¯ÎÊÓû§ÊÇ·ñÈ·¶¨Òª½øÐÐÌæ»».
N = Nth Replace only the Nth occurance of
on the line.
µÚN´Î Ö»Ìæ»»ÔÚÒ»ÐÐÖеÚN¸öÆ¥ÅäµÄ×Ö·û´®(Èç:s/zhao/slimzhao/2, ¶ø
µ±Ç°ÐеÄÄÚÈÝÊÇzhao zhao zhao, ÔòÌæ»»ºóµÄÄÚÈÝΪzhao slimzhao zhao, Òë
Õß×¢)
(No modifier implies N=1, the first occurance on that line)
(Èç¹ûûÓÐÖ¸¶¨ÕâЩ¸¨ÖúÐÞÊαêÖ¾, ÔòvimĬÈÏÎªÖ»Ìæ»»Ò»ÐÐÖеÚÒ»¸öÆ¥ÅäµÄ×Ö·û´®
) (¼´µÈ¼ÛÓÚaddress1, address2s/
/string/1, ÒëÕß×¢)
[address values] -May be one specifier or two seperated by a comma. (below
, x represents an integer).
¿ÉÒÔÊÇÒ»¸ö»òÊÇÓɶººÅ·Ö¿ªµÄÁ½¸ö¸¨ÖúÐ޸ıêÖ¾. (ÏÂÃæµÄx´ú±íÒ»¸öÕûÊý)
. = The current line
±íʾµ±Ç°ÐÐ(¼´¹â±êËùÔÚµÄÐÐ, ÒëÕß×¢)
$ = The last line in the file
% = Entire file
Õû¸öÎļþ(¼´¶ÔÿһÐÐ, µÈ¼ÛÓÚ1, $, ÒëÕß×¢)
x = The xth line of the file
+x = x lines after the current line
´Óµ±Ç°ÐпªÊ¼ÏÂÃæµÄµÚxÐÐ(Èç¹ûµ±Ç°ÐÐΪµÚ1ÐÐ, Ôò+3 ´ú±íµÚ4ÐÐ)
-x = x lines before the current line
´Óµ±Ç°ÐпªÊ¼ÉÏÃæµÄµÚxÐÐ(Èç¹ûµ±Ç°ÐÐΪµÚ4ÐÐ, Ôò-3 ´ú±íµÚ1ÐÐ)
The comma may seperate any of the above in order to specify a range. All
lines within the given range will undergo the substitution. The best reference
I have found for subsituting can be found at the Vi Helpfile, linked below
.
¶ººÅÓÃÓÚ·Ö¸ôÈκÎÉÏÃæÖ¸¶¨µÄµ¥¸öÐÐ, ÒÔÐγÉÒ»¸ö·¶Î§(µ±È», Õâ¸ö·¶Î§µÄϽ粻
ÄÜСÓÚÉϽç, Èç10,1Ϊ·Ç·¨µÄ·¶Î§, ´Ëʱvim»á¸ø³öÒ»¸ö¾¯¸æÐÅÏ¢, ÎÊÄãÊÇ·ñ½øÐÐ
·´Ïò²Ù×÷, Èç»Ø´ðy, ÔòµÈ¼ÛÓÚ1,10, ²Ù×÷ÈÔÕý³£½øÐÐ, ·ñÔò, ³·Ïûµ±Ç°²Ù×÷,
ÒëÕß×¢), ÆäºóÖ¸¶¨µÄ²Ù×÷½«×÷ÓÃÓÚ´Ë´¦¸ø³öµÄ·¶Î§, vim°ïÖúÀïÓйØÓÚÌæ»»²Ù×÷
µÄ³ä·ÖÐÅÏ¢.
Miscellany
Vim has so many nifty little things, it would be impossible to list them
all. Here are a few more things that are worth taking a look at.
ÆäËüÔÓÏî
VimÓÐÖÚ¶àÓÕÈ˵ÄС¹¦ÄÜ, ÕâÀï²»¿ÉÄÜÒ»Ò»Áгö, ÏÂÃæÁгöһЩÓÈÆäÖµµÃ×¢ÒâµÄÒ»
Ð©ÌØÐÔ.
Include File Searching- ':h include-search'
°üº¬ÎļþËÑË÷- `:h include-search'
Bookmarking- 'mx' to set, ' 'x' to return; (x can be any letter, must stay
within file to remember)
ÊéÇ©ÉèÖÃ- 'mx'ÓÃÓÚÉèÖÃÊéÇ©, ''x'ÓÃÓÚ´ÓÊéÇ©·µ»Ø;(ÆäÖеÄx¿ÉÒÔΪÈκÎ×Öĸ,
µ«, Ö»ÄܼǼµ±Ç°ÎļþÀïµÄÊéÇ©) (Í˳övimºóÔٴνøÈ뽫²»»á±£ÁôÕâЩÊéÇ©,
ÊéÇ©¾ÍÊÇ´ú±íÔÚÎļþÖÐÄ³Ò»ÌØ¶¨Î»ÖõÄÒ»ÖÖ±ê¼Ç, ÒëÕß×¢)
"Clipboard" buffers- ' "xY ' to cut or copy to buffer x (Y represents
any normal deletion or yank command), ' "xZ ' to paste contents of x (Z
represents pasting... p or P); (x can be any letter, can switch to another
file(:e filename) and still maintain contents of buffers).
"¼ôÌù°å" »º³å- ' "xY ' ÓÃÓÚ¼ôÇлò¸´ÖƵ½Ò»¸öÃûΪxµÄ»º³åÇø(Y ´ú±íÈκεÄ
ɾ³ý»òÈ¡ÑùÃüÁî), ' "xZ ' ÓÃÓÚÕ³ÌùÄÚÈÝ(Z´ú±íÕ³ÌùÃüÁîp »ò P); (ÆäÖÐx¿ÉÒÔΪ
ÈκÎ×Öĸ, Ò²¿ÉÔÚÌø×ªµ½ÁíÒ»ÎļþÖÐʱ¼ÌÐøÉúЧ(:e filename).
Comment Specifiers- ':h comments'
.vimrc- Don't forget your .vimrc!!! (Located in your home directory). You
'll find it very handy to add many of the above things to your .vimrc file
, so that vim "remembers" everything you want it to. (You may find that some
of them are already there.) Remember that you don't need to prepend a ':' to
the command when adding them to the .vimrc.
.vimrc- ±ðÍüÁËÄãµÄ.vimrcÎļþ(ÔÚÄãÓû§Ä¿Â¼ÖÐ~/.vimrc). ¸ÃÎļþ¿ÉÓÃÓÚ
¼Ç¼ÉÏÃæÄãËù×öµÄ´ó¶àÊýÉèÖÃ, ¼ÇסÔÚ.vimrcÎļþÖÐÎÞÐèÔÚÿ¸öÃüÁîǰʹÓÃÒ»
¸öðºÅ":".(ÔÚDOSϵÄvimÖÐ, .vimrcÎļþ·ÅÓÚvim³ÌÐòËùÔÚµÄĿ¼ÖÐ, ÇÒ, ´Ë
ʱ²»½Ð.vimrc, ½Ð_vimrc, Áí, .vimrcÒ²¿ÉΪ.exrc, _vimrcÒ²¿ÉΪ_exrc)
Other Resources
X_Console has written a really nice Vi Crash Course NHF , which should help
you get over the learning curve, if you're just starting out.
There are many, many webpages around with information on Vi/Vim, some good
, some not so good (depending on what level you're looking for). A search
for Vi or Vim at Google or any other search engine will turn up plenty of
results. In my opinion, these are two of the best:
ÆäËü×ÊÔ´
X_Console(´Ë´¦²»ÖªÈçºÎ·Òë, ÒëÕß×¢)ÉÏÓÐÒ»¸ö·Ç³£ºÃµÄvi½Ì³Ì, Èç¹ûÄãÒª¿ªÊ¼
ѧϰʹÓÃvi, ¾Í´ÓÕâÀ↑ʼ°É.
ÒòÌØÍøÉÏÓзdz£¶àµÄ¹ØÓÚvi/vimÐÅÏ¢µÄÍøÒ³, ÓкÃÓлµ(ºÃ»µÒ²¿´ÄãµÄˮƽÈçºÎÁË)
ÔÚGoogle»òÆäËüËÑË÷ÒýÇæÉϲéÕÒvi»òvim»áÕÒµ½·Ç³£¶àµÄËÑË÷½á¹û, ÎÒ¸öÈ˾õµÃ
ÏÂÃæÁ½¸öÊÇ×îºÃµÄ
THE VI LOVER'S HOMEPAGE - Many links, lots of info...
VI°®ºÃÕßÖ÷Ò³ - Á´½Ó¶à¶à, ÐÅÏ¢¶à¶à...
The VI Helpfile - Very comlete, terse reference. Great for ex commands.
VI°ïÖúÎļþ -·Ç³£ÍêÕû¶ø¼òÁ·µÄÒ»·Ý²Î¿¼ÊÖ²á, ÌØ±ðÊÇexÃüÁî.
Unixworld Vi Tutorial - Nine parts, from start to finish... Go through
this and you'll understand why we love Vi.
UnixÊÀ½çVi½Ì³Ì- ¾Å²¿·Ö, ´Ó¿ªÊ¼µ½½áÊø...¿´Á˾ÍÖªµÀ, ÎÒÃÇΪʲôϲ
»¶VI.
This file was created by Keith Jones (kmj9907@cs.rit.edu); I'm no expert
on vim, but I hope some of the above was helpful. Enjoy!!!
±¾ÎÄÓÉKeith Jones(kmj9907@cs.rit.edu)д¾Í; ÎÒ²»ÊÇvimר¼Ò, µ«ÎÒÏ£ÍûÉÏÃæ
µÄһЩÄÚÈݶԴó¼ÒÓÐËù°ïÖú. Ï£Íû´ó¼Òϲ»¶!!!