请问Vim如何注释掉一段 c 代码

Vim、Emacs配置和使用
回复
头像
nickleeh
帖子: 130
注册时间: 2008-08-06 13:24

请问Vim如何注释掉一段 c 代码

#1

帖子 nickleeh » 2008-08-10 17:32

如题:怎样在 Vim 中注释/反注释掉一段 c 代码?
头像
kofshower
帖子: 1343
注册时间: 2007-03-13 11:23
联系:

#2

帖子 kofshower » 2008-08-10 17:38

自己写一个函数
参考代码:

代码: 全选

function! CPP_CodeComment( mode, style )

	if a:mode=="a"
		if a:style == 'yes' 
			silent exe ":s#^#/\* #"
			silent put = ' */'
		else
			silent exe ":s#^#//#"
		endif
	endif
	
	if a:mode=="v"
		if a:style == 'yes' 
			silent exe ":'<,'>s/^/ \* /"
			silent exe ":'< s'^ '\/'"
			silent exe ":'>"
			silent put = ' */'
		else
			silent exe ":'<,'>s#^#//#"
		endif
	endif

endfunction    
"We are all in the mud, but some of us are looking at the stars." (Oscar Wilde)
We are not born for ourselves.
人生天地间,并非为自己
Homepage:http://sites.google.com/site/polarisnotme/
头像
nickleeh
帖子: 130
注册时间: 2008-08-06 13:24

#3

帖子 nickleeh » 2008-08-10 17:55

谢谢楼上的回复。

我找到了一个非常好的插件,解决这个问题:

http://www.vim.org/scripts/script.php?script_id=1173

非常方便,可以用gc来注释/反注释文本块。(不仅支持c,还支持 TeX/ruby/python/perl
regions in vim scripts, HTML or JavaScript in php code ... .... 推荐大家试一下)
头像
ccbrighty
帖子: 17
注册时间: 2008-05-01 11:36

#4

帖子 ccbrighty » 2008-08-10 23:06

http://www.vim.org/scripts/script.php?script_id=1218
推荐试试这个,我是过很多,这是最优秀的一个 :D
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

#5

帖子 eexpress » 2008-08-10 23:13

EnhancedCommentify.vim
● 鸣学
头像
zcg0696
帖子: 44
注册时间: 2008-05-27 17:30

#6

帖子 zcg0696 » 2008-08-16 11:31

手动

代码: 全选

:n1,n2 s/^/#/g  

代码: 全选

:n1,n2 s/^#//g
修改下'#'可以对付其它的注释符
sirxenofex
帖子: 204
注册时间: 2008-01-07 16:39

#7

帖子 sirxenofex » 2008-08-16 13:00

ctrl+v进入块选择模式,然后选定要注释的若干行,按I(大写)进入插入模式,在行首插入#,再按Esc退出插入,再按一次退出块选择就行了。如果是反注释,进入块选择后选定若干行首的#再删掉就行了。
wangtwo
帖子: 73
注册时间: 2007-05-07 9:42

Re:

#8

帖子 wangtwo » 2008-11-15 19:05

zcg0696 写了:手动

代码: 全选

:n1,n2 s/^/#/g  

代码: 全选

:n1,n2 s/^#//g
修改下'#'可以对付其它的注释符
sirxenofex 写了:ctrl+v进入块选择模式,然后选定要注释的若干行,按I(大写)进入插入模式,在行首插入#,再按Esc退出插入,再按一次退出块选择就行了。如果是反注释,进入块选择后选定若干行首的#再删掉就行了。

佩服
回复