vim 中的右对齐注释

ma0*_*0ho 5 vim programming text

我正在使用 vim 编写 C 代码,并且正在寻找右对齐我的注释的可能性,以便它们都以第 80 列结尾。举一个简短的例子:

int a = 80; /* initialize a */
int b = 7; /* initialize b */
printf("%d + %d = %d", a, b, a+b); /*calculate the result */
Run Code Online (Sandbox Code Playgroud)

应该变成

int a = 80;                               /* initialize a */
int b = 7;                                /* initialize b */
printf("%d + %d = %d", a, b, a+b); /*calculate the result */
                                                           ^col 80
Run Code Online (Sandbox Code Playgroud)

我已经安装vim-easy-align进行其他格式设置,但还没有找到如何执行此对齐。也许有人知道怎么做?

我不坚持vim-easy-align。如果你有另一个插件可以完成这项工作..告诉我;)。

小智 5

以下是使用普通 vim 命令执行此操作的方法,不使用插件:

在正常模式下,将光标放在要右对齐的字符串的第一个字符上,如注释分隔符,然后按leader然后tab右对齐文本。

nnoremap <leader><tab> mc80A <esc>080lDgelD`cP
Run Code Online (Sandbox Code Playgroud)

附说明:

mc80A <esc>080lDgelD`cP
| |        |   ||  ||
mc|        |   ||  ||    Mark the position of the cursor
  |        |   ||  ||
  80A <esc>|   ||  ||    Append 80 spaces at the end
           |   ||  ||
           080l||  ||    Go the the 80th column from the beginning of the line
               ||  ||
               D|  ||    Delete what is next
                |  ||
                gel||    goes after the last string char
                   ||
                   D|    Delete the characters remaining (complement to go 80)
                    |
                    `cP  and paste these to shift the string up to 80 column.
Run Code Online (Sandbox Code Playgroud)

要标记多个评论,您可以搜索评论分隔符的下一个出现,然后按leadertabnleadertabnleadertabn...


Ing*_*kat 4

我的AlignFromCursor 插件提供了执行此操作的<Leader>ri映射和命令。:RightAlignFromCursor只需将光标放在注释之前的空白处(手动或通过命令:[range]normal)并调用映射或命令。它使用'textwidth'或 前缀[count]