如何在 vim 中缩进 c 的多行注释

  • 本文关键字:多行注释 缩进 vim c vim
  • 更新时间 :
  • 英文 :


我在我正在处理的项目中用 c 代码编写的每个函数之前正确缩进多行注释。我使用 gvim 编辑器。问题是很多时候我在函数中进行了一些更改,并且必须重新编辑注释,而重新编辑注释会使注释取消缩进。我再次不得不重新缩进评论。我发现很难重新编辑多行评论。gvim 中是否有任何快捷方式或实用程序,我可以使用它轻松自动缩进多行注释?

我在网上搜索了这个问题,找不到任何有用的东西来解决这个问题。 在这里,我展示了一个多行注释的示例,该注释是在函数之前编写的,然后在重新编辑后编写多行注释(请注意,它现在未缩进),然后是预期结果。

我还有一个问题。你是否在用 c 编写的每个函数之前写注释。如果是,如果您在函数中进行一些更改,如何保持注释的缩进?

/*
* Function: remove_item_from_list
* --------------------
*  Removes an item from a list. Here list is a sequence of same type objects. 
*  pItem is also same type object. This is a generic function in the sense
*  that it can work for different type of objects. Currently pList will have 
*  only one copy of pItem w.r.t. the calling places. So whenever the item  
*  found a break statement is used.
*
*  pList : a sequence of objects
*  pItem : object
*
*  returns: modified list
*/

现在,我在对函数进行一些更改后重新编辑了注释。现在 gvim 编辑器中的注释如下所示。我在评论中的更改存在于####块中。

/*
* Function: remove_item_from_list
* --------------------
*  Removes an item from a list. Here list is a sequence of same type objects. 
*  pItem is also same type object. This is a generic function in the sense
*  that it can work for different type of objects. ##Currently it works for Animal and Bird type objects. If you want this function to work for some there type object then you have add check for that object. Note that this function assumes that pItem can't be NULL.## Currently pList will have 
*  only one copy of pItem w.r.t. the calling places. So whenever the item  
*  found a break statement is used.
*
*  pList : a sequence of objects
*  pItem : object
*
*  returns: modified list
*/
/*
* Function: remove_item_from_list
* --------------------
*  Removes an item from a list. Here list is a sequence of same type objects. 
*  pItem is also same type object. This is a generic function in the sense
*  that it can work for different type of objects. Currently it works for 
*  Animal and Bird type objects. If you want this function to work for some 
*  there type object then you have add check for that object. Note that this 
*  function assumes that pItem can't be NULL.## Currently pList will have 
*  only one copy of pItem w.r.t. the calling places. So whenever the item  
*  found a break statement is used.
*
*  pList : a sequence of objects
*  pItem : object
*
*  returns: modified list
*/

您可以使用gq命令(:help gq)。在段落周围运行可视选择以设置和键入gq的格式。根据设置textwidth(如@Michail所述),Vim 将正确包装文本,请记住这是一个注释。

通过这种方式,您可以键入您的评论,编辑它,然后返回它,而无需关心太多的格式,一旦您对它感到满意,就可以格式化。

最新更新