分享
 
 
 

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

王朝c/c++·作者佚名  2006-11-24
窄屏简体版  字體: |||超大  

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. Vi

[1] [2] 下一页

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
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- 王朝網路 版權所有