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

时间:2019-03-30 08:24:05

标签: 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
 */

1 个答案:

答案 0 :(得分:2)

您可以使用gq命令(:help gq)。在段落周围进行视觉选择,以设置格式并键入gq。根据集合textwidth(如@Michail所述),Vim会正确包装文本,并记住这是注释。

通过这种方式,您可以键入注释,对其进行编辑,再返回到它,而无需关心很多格式,并且一旦满意就可以进行格式化。