Quickly redistribute number of words/character per line in VIM

时间:2015-11-12 12:03:06

标签: vim text

For text writing in vim, when I need to edit a line/add some new text, the number of words per line gets all out of whack.

Is there a way of quickly re-adjusting/line-wrapping to evenly disperse the number of words per line in VIM?

Example with 80 character auto-line-wrapping:

Before edit

This is something that I have writting with automatic line-wrapping before I 
have edited the file. I'll write some more just for demonstration purposes for
this whole line wrapping problem of mine.

After edit

This is something that I have writting with automatic line-wrapping after I
have edited the file. I'm demonstrating
this whole line wrapping problem of mine.

Manually fixing these is a bore, especially if I fix it and go on to edit things again.

My dream

It would be perfect if I could visually select a number of lines using shit+v, and then automatically distribute the number of words per line.

2 个答案:

答案 0 :(得分:5)

  • v i p 选择当前段落,然后
  • g q 使用textwidth重新格式化段落。

  • g q i p 组合运算符和范围(请参阅:help text-objects for更多细节)

如果您经常使用它,您可以重新映射一个新命令,该命令将执行前面提到的两个命令。

有关详细信息,请参阅:help gq

答案 1 :(得分:5)

您可以根据需要设置textwidth选项(或简称tw) 宽度。让我们说80。

:set tw=80

现在,使用内置格式化程序或自定义格式化程序 命令gq。查看已知的gqgw'formatprg'的帮助 更多。这与动作一起使用。所以要重新格式化当前段落, 做gqip。整个文本gggqG(光标到顶部(gg)然后格式化 直到底部)。所有其他人都会动作。

此外,请浏览格式选项的帮助(:h 'fo':h fo-table)。一个特别有趣的选项是a。因此,如果 你这样做:

:set tw=80
:set fo+=a

现在你甚至不需要重新格式化。 Vim会继续检查 键入时行的宽度,并在移动中重新格式化段落 通过插入或删除换行符。

我喜欢外部程序的灵活性,也推荐你 检查出来。例如,这个答案的MarkDown格式化为 UNIX程序 par 。检查原始文本。