分享
 
 
 

Introduction to Programming in C/C++ with Vim

王朝c/c++·作者佚名  2008-05-18
窄屏简体版  字體: |||超大  

Introduction to Programming in C/C++ with Vim

Written By: Kmj

用Vim进行C/C++编程介绍

作者: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就一直是编程者中最广为流传的文本编辑

工具, 即使不是最流行的, 也一定是最流行者之一.

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已经具有了非常多的功能, 这些功

能使程序员能更加轻松, 便捷地使用它们. 下面是它的一些功能描述, 正是这些

丰富强大的功能使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

c语言风格的缩进

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是必需的). 欲打开该模式, 只需键入命

令: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的默认设置选项一般来说是比较可人的, 但如果你的程序有特殊需求, 也

可以改变它, 设置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).

(其中的N是一个整数值).

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 = 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'

注释符- `: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, 就从这里开始吧.

因特网上有非常多的关于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专家, 但我希望上面

的一些内容对大家有所帮助. 希望大家喜欢!!!

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有