我正在使用 vim 編寫 C 程式碼,並正在尋找一種可能性來右對齊我的註釋,以便它們都以第 80 列結束。
int a = 80; /* initialize a */
int b = 7; /* initialize b */
printf("%d + %d = %d", a, b, a+b); /*calculate the result */
應該變成
int a = 80; /* initialize a */
int b = 7; /* initialize b */
printf("%d + %d = %d", a, b, a+b); /*calculate the result */
^col 80
我已經安裝了vim-easy-align
其他格式,但尚未找到如何執行此對齊。也許有人知道怎麼做?
我不強求vim-easy-align
。如果您有另一個可以完成這項工作的插件..請告訴我;)。
答案1
以下是如何使用普通的 vim 命令來完成此操作,不使用任何插件:
在正常模式下,將遊標放在要右對齊的字串的第一個字元(例如註釋分隔符號)上,然後按然後leader將tab文字右對齊。
nnoremap <leader><tab> mc80A <esc>080lDgelD`cP
附有解釋:
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.
若要標記多個註釋,您可以搜尋下一個出現的註釋分隔符,然後按leadertabnleadertabnleadertabn...
答案2
我的從遊標對齊插件提供了執行此操作的<Leader>ri
映射和:RightAlignFromCursor
命令。只需將遊標放在註釋之前的空白處(手動或透過:[range]normal
命令)並呼叫映射或命令。它使用'textwidth'
或 前綴[count]
。