如何使用vim从此代码段中仅删除注释?

时间:2014-12-18 08:37:16

标签: vim

我想使用vim删除以下代码段中的所有注释。请帮助我。

int main()
{
  Computer compute;  
  // To create an 'instance' of the class, simply treat it like you would
  //  a structure.  (An instance is simply when you create an actual object
  //  from the class, as opposed to having the definition of the class)
  compute.setspeed ( 100 ); 
  // To call functions in the class, you put the name of the instance,
  //  a period, and then the function name.
  cout<< compute.readspeed();
  // See above note.
}

2 个答案:

答案 0 :(得分:3)

:g/<Pattern>/d

它将删除与<Pattern>匹配的所有行。例如

:g/\s*\/\//d

将删除所有前两个非空白字符为//的行。

答案 1 :(得分:0)

有几种方法可以做到这一点。

最简单的可能是使用插件,例如NERDCommenter http://www.vim.org/scripts/script.php?script_id=1218

如果您不想安装内容,则可以使用可视模式:   - Ctrl + V进入列可视模式   - 选择//   - 然后x

你也可以使用宏qa,然后删除第一行的//,用q关闭录制,然后重播宏@a

最后,如果您是正则表达式粉丝,那么您可以执行此操作::g/\s*\/\//d