主页 PC知识 网管技术 黑客帝国 安全技术 开放系统 程序设计 搜索 技术论坛

 

相关联接
 
RHU本级分类

编程语言
程序代码
WIN/*NIX编程
其他类别
JAVA专区

 
RHU阅读排行
·在LinuxShell程序中进行身份验证
·截获流经本机网卡的IP数据包
·枚举进程和杀进程的小工具
·从蓝屏最小的asp木马来看C/S木马的编写
·用tc+iptables+HTB解决ADSL宽带速度瓶颈技术
·通过 Visual C++ 的编程模型和编译器优化增强您的应用程序
·Windows管道技术简述
·利用键盘钩子开发按键发音程序
·一个Linux病毒原型分析
·XP终端服务远程登录批处理

 
 
RHU最新文章
·Webshell下破解计算机管理员密码
·打造XP下可运行的微型PE文件(292字节)
·Win32平台下的Rootkit习作
·Linux如何在系统运行过程中修改内核参数
·Shell编程:Linux系统环境程序设计之路
·汇编创建简单的窗口
·深入浅出Win32多线程程序设计之线程控制
·VC实现Win2000下屏蔽Ctrl+Alt+Del键
·隐藏任意进程 目录/文件 注册表 端口
·Windows 的多线程程序设计初步 

 
 
RHU相关搜索









 
 
RHU广而告之

 
 
>您的位置:首页 -> 程序设计 -> WIN/*NIX编程
用Vim进行C/C++编程介绍

作者:RHU-TAC编辑员 来自:RHU网络采集 时间:2005-5-26 双击滚屏 收藏本页 字体:


点击 查看RHU2004全年文章


原著:Keith Jones(kmj9907@cs.rit.edu)

翻译整理:小赵 (slimzhao@21cn.com)


Introduction to Programming in C/C++  with Vim
Written By: Kmj

用Vim进行C/C++编程介绍
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 ctagsc-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', `:hcinoptions', `: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会使用下划线, 粗体字, 试图进行等效的替代, 但对我而言, 这样太难看了.



OVER

[1] 页 RedHyphone.Union 投稿邮箱
[特别声明]:
本站文章大多搜索转载自网络中,如果侵犯了您的权利,请告之我们。本站将立即删除。
本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
查看评论】【向上滚屏】【关闭窗口】【 打印
-相关文章
  • openSUSE 11.1 Final - 正式发布
  • [视频]Opera Mini 4.2 正式版发布
  • dll注入系统进程(开源代码)
  • 认知盲区 解惑双网卡双线路DNS解析
  • FlashFXP 简体中文版 3.7.5 Build 1303 Beta[烈火]
  • -文章评论 (关闭)
    ·还没有相关的评论!

    网上大名:
    红旋风网络技术联盟 RHUTech.Union
     
    Copyright © 2000-2007 RedHyphone.Union All Rights Reserved. 红旋风联盟版权所有.皖ICP备05011033号
    中国红旋风网络技术联盟 | www.RedHyphone.net
    Mailto:Redhyphone@gamil.com