vim.c 使用

This article is part of the ongoing Vi / Vim Tips and Tricks Series. As a programmer, you may do lot of repetitive tasks while coding such as:

  • Adding file header
  • Adding function/frame comment
  • Including default code snippet
  • Performing syntax check
  • Reading documentation about a function
  • Converting a code block to comment, and vice versa


The C-Support Vim Plugin offers easiest way to do all of the above, saving lot of time and keystrokes for C and C++ programmers.

The plugin was written by Fritz Mehner, who explains the purpose of the plugin as: “Write and run programs. Insert statements, idioms, comments”.

He also highlights following features:

  • Statement oriented editing of C / C++ programs
  • Speed up writing new code considerably.
  • Write code and comments with a professional appearance from the beginning.
  • Use code snippets


This article explains how to install the plugin in 3 easy steps and 7 powerful features of the plugin.

3 Steps to Install the C.Vim Plugin

Step 1: Download C Vim Plugin

Download the plugin from vim.org website.

$ cd /usr/src
$ wget http://www.vim.org/scripts/download_script.php?src_id=9679

Step 2: Install the C Vim Plugin

$ mkdir ~/.vim
$ cd ~/.vim
$ unzip /usr/src/cvim.zip

Step 3: Enable the plugin in the ~/.vimrc

Add the following line to the ~/.vimrc to enable the plugin for Vim editor.

$ vim ~/.vimrc
filetype plugin on

8 Powerful Features of C.Vim Plugin

Feature 1: Add Automatic Header to *.c file

When you open a file with the extension .c it opens the file with header as shown below. This will also place the cursor in the Description field in Insert mode.

$ vim myprogram.c
/*
* =================================================
*       Filename:  myprogram.c
*
*    Description:
*
*        Version:  1.0
*        Created:  01/19/09 20:23:25
*       Revision:  none
*       Compiler:  gcc
*
*         Author:  Dr. Fritz Mehner (mn), mehner@fh-swf.de
*        Company:  FH Südwestfalen, Iserlohn
*
* =================================================
*/


To change the default value of the AUTHOR and COMPANY, modify the default value in ~/.vim/c-support/templates/Templates

$ vim ~/.vim/c-support/templates/Templates
|AUTHOR|    = geekstuff
|AUTHORREF| = gk
|EMAIL|     = subscribe@geekstuff
|COMPANY|   = thegeekstuff.com


Now, when you create a new c file, it will show the modified values for AUTHOR and COMPANY as shown below.

$ vim myprogram.c
/*
* =================================================
*
*       Filename:  myprogram.c
*
*    Description:
*
*        Version:  1.0
*        Created:  01/19/09 20:26:43
*       Revision:  none
*       Compiler:  gcc
*
*         Author:  geekstuff (gk), subscribe@geekstuff
*        Company:  thegeekstuff.com
*
* =================================================
*/


Note: To add custom fields to the header, modify the ~/.vim/c-support/templates/file-description.template file and add your own custom field.

Feature 2: Adding C function using \if

For writing a subroutine, type \if in normal mode, which will prompt for the function name (as shown in Fig1 below) and inserts the subroutine with default function content (as shown in Fig2 below).

Vim C/C++ IDE - Adding C Function - 1

Fig1:Insert C Function Automatically

Vim C/C++ IDE - Adding C Function - 2

Fig 2:Insert C Function Automatically

Feature 3: Insert main Function Header using \im

For inserting main function, type \im in normal mode, which will add the main function as shown below.

Fig 3: Insert C main function automatically

Feature 4: Insert a Function Header using \cfu

For inserting a function header, type \cfu in normal mode, which will ask the function name as shown in Fig 4, and adds the comment as shown in Fig 5.

Vim C/C++ IDE - Insert C Function Header - 1

Fig 4: Insert C Function Header Automatically

Vim C/C++ IDE - Insert C Function Header - 1

Fig 5: Insert C Function Header Automatically

Feature 5: Add a Frame comment using \cfr

To add a frame comment, type \cfr in normal mode, which will give the following formatted comment.

Vim C/C++ IDE - Insert Frame Comment

Fig 6: Insert a Frame Comment Automatically

Feature 6: To include header file, use \p<

Type \p< in the normal mode, which will include the text “#include <>”, and places the cursor in the < symbol in Insert mode where you can type the header file name.

Feature 7: Save the file, compile it and execute it immediately.

To save and compile the file use \rc.

To run use \rr.

Feature 8: Insert pre-defined code-snippet to the C code using \nr

The plugin comes with few pre-defined code snippets that you can insert into your code. Following are the default code snippets that comes with the plugin.

$ ls ~/.vim/c-support/codesnippets
Makefile                        calloc_double_matrix.c  main.c   print_double_array.c.noindent
Makefile.multi-target.template  calloc_int_matrix.c     main.cc  print_int_array.c.noindent

For example, if you want to create a function that will Allocate a dynamic int-matrix of size rows*columns; return a pointer, you can re-use it from the existing code snippets. Following is the content of the calloc_int_matrix.c pre-defined code snippets.

/*
* ===  FUNCTION  ======================================================================
*         Name:  calloc_int_matrix
*  Description:  Allocate a dynamic int-matrix of size rows*columns; return a pointer.
* =====================================================================================
*/
int**
calloc_int_matrix ( int rows, int columns )
{
int   i;
int **m;
m     = calloc ( rows, sizeof(int*) );        /* allocate pointer array     */
assert( m != NULL );                          /* abort if allocation failed */
*m    = calloc ( rows*columns, sizeof(int) ); /* allocate data array        */
assert(*m != NULL );                          /* abort if allocation failed */
for ( i=1; i
m[i]  = m[i-1] + columns;
return m;
}  /* ----------  end of function calloc_int_matrix  ---------- */


To insert this into your working c program, type \nr from the normal mode inside vim, which will prompt “read snippet /home/ramesh/.vim/c-support/codesnippets/”, type calloc_int_matrix.c at the end and press enter, which will insert the content of the ~/.vim/c-support/codesnippets/ calloc_int_matrix.c to your working file automatically.

Note: You can define your own code snippets and place it under ~/.vim/c-support/codesnippets/. You can also build your own code snippets from the existing code – select the part of code need to be made as code snippet, press \nw, and give a file-name to it. From next time, type \nr and the file-name to get your custom code snippet.

There are lot of powerful features in the C-Support Vim Plugin. Read the documentation for more information. The documentation is located in the following location on your system.

<think>我们正在讨论的是VNC显示问题,但用户现在询问的是修改Vim配置文件以解决VNC显示问题。这里存在一个误解:Vim的配置文件(vimrc或viminfo)并不控制VNC的显示设置。VNC的显示设置通常通过VNC服务器配置(如启动参数、xstartup脚本等)来调整。但是,用户可能是在编辑VNC相关配置文件(如xstartup或vncserver的init脚本)时需要使用Vim,而他们想知道应该编辑哪个文件以及如何编辑。根据引用[1]和引用[3],我们看到在配置VNC时修改的文件包括:1. `~/.vnc/xstartup`:用于设置VNC会话启动时的桌面环境。2. `/etc/init.d/vncserver`:用于设置开机自启的脚本。用户提到的“vim.rc”和“vim.info”应该是vim的配置文件(通常为`.vimrc`和`.viminfo`),但这些文件与VNC显示设置无关。因此,我们需要澄清并指导用户修改正确的文件:1.要调整VNC的分辨率,可以修改VNC启动命令中的`-geometry`参数(如引用[3]中在`/etc/init.d/vncserver`脚本里设置的`vncserver -depth24 -geometry1920x1080 :1`)。2.要调整VNC启动时的桌面环境,可以修改`~/.vnc/xstartup`文件(如引用[1]和引用[3]中的内容)。所以,用户应该使用Vim编辑器去修改上述两个文件之一,而不是修改Vim自身的配置文件。具体步骤:如果要修改分辨率:-编辑开机启动脚本(如果使用开机启动):`sudo vim /etc/init.d/vncserver`,找到包含`vncserver`命令的行,修改`-geometry`后面的分辨率值。-或者,在启动VNC时直接指定分辨率:`vncserver -geometry1920x1080 :1`如果要修改VNC启动时的桌面环境(比如解决显示问题):-编辑`~/.vnc/xstartup`文件:`vim~/.vnc/xstartup`,然后按照引用[1]或引用[3]中的示例进行修改。因此,回答用户:用户您好,关于VNC显示问题的调整,实际上不需要修改Vim的配置文件(如.vimrc或.viminfo),而是需要修改VNC相关的配置文件。具体如下:1.**修改分辨率**:如果您是通过开机启动脚本启动VNC,请编辑`/etc/init.d/vncserver`文件,使用Vim命令:```bashsudo vim /etc/init.d/vncserver```在文件中找到类似下面的行(可能略有不同):```bashsu $VNCUSER -c 'vncserver -depth24 -geometry1920x1080 :1'```将`-geometry`后面的分辨率(例如1920x1080)修改为您需要的值。或者,您也可以在每次启动VNC时直接指定分辨率:```bashvncserver -geometry1920x1080 :1```2. **调整VNC启动环境**:如果您遇到VNC启动后桌面显示不正常(例如缺少桌面环境),可能需要修改`~/.vnc/xstartup`文件。使用Vim编辑:```bashvim ~/.vnc/xstartup```参考引用[1]或引用[3]中的示例内容,确保启动正确的桌面环境(如GNOMEFlashback)。注意:修改配置文件后需要重启VNC服务才能生效。如果您在使用Vim编辑这些文件时需要帮助,以下是一些基本操作:-打开文件:`vim文件名`-按`i`进入编辑模式(此时可以输入内容)-编辑完成后按`ESC`退出编辑模式-输入`:wq`保存并退出(或`:q!`不保存退出)希望这能解决您的问题。相关问题:1.如何设置VNC服务开机自启?2.VNC连接出现画面模糊如何优化?3.不同VNC实现(TigerVNC/RealVNC)的主要区别是什么?</think>### 调整VNC显示设置的配置文件指南 您需要修改的是 **VNC的配置文件**,而不是Vim的配置文件(vim.rc或vim.info)。Vim配置仅影响文本编辑器行为,与VNC显示无关。以下是正确的修改方法: #### 核心配置文件(必改) 1. **~/.vnc/xstartup** - VNC会话启动脚本 ```bash vim ~/.vnc/xstartup # 使用Vim编辑此文件 ``` 在文件中添加分辨率设置(参考引用[1]和引用[3]): ```bash # 在gnome-session启动命令前添加 xrandr --fb 1920x1080 # 设置分辨率 gnome-session --session=gnome-flashback-metacity --disable-acceleration-check & ``` 2. **~/.vnc/config** - 永久分辨率设置(推荐) ```bash vim ~/.vnc/config # 使用Vim创建/编辑 ``` 添加内容: ```ini geometry=1920x1080 # 设置默认分辨率[^3] alwaysshared # 允许多用户连接 ``` #### 系统级配置(可选) 3. **开机自启脚本**(参考引用[3]) ```bash sudo vim /etc/init.d/vncserver ``` 在启动命令中添加参数: ```bash su $VNCUSER -c 'vncserver -depth 24 -geometry 1920x1080 :1' ``` #### 操作步骤总结 1.Vim编辑用户级配置文件: ```bash vim ~/.vnc/xstartup # 添加xrandr命令 vim ~/.vnc/config # 设置geometry参数 ``` 2. 修改后赋予执行权限: ```bash chmod 777 ~/.vnc/xstartup # 参考引用[1] ``` 3. 重启VNC服务生效: ```bash vncserver -kill :1 vncserver :1 ``` > **关键提示**: > - 所有修改均针对VNC配置文件,与Vim自身配置无关 > - `xrandr`命令动态调整分辨率(无需重启服务) > - `geometry`参数永久生效(需重启VNC服务)[^3] > - 分辨率值需适配您的显示器(如1280x720, 1920x1080等)
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值