在VIM中设置文本宽度时,修复段落中的行溢出

时间:2013-11-02 19:11:21

标签: vim vim-plugin

我们假设我的.vimrc具有以下配置:

set textwidth=10

在文件中,我输入以下内容:

This is a 
sentence
word wraps
just fine
at 10 
chars.

当我输入这个句子时,VIM将实时自动换行并阻止我传递我设置的标记。但是,我发现自己遇到的一个常见问题是添加更多内容。我忘记了上一段中的一句话,“这句话就是一句话。”

所以我回去添加它。

This is a
a sentence that, suddenly, doesn't
word wrap
just fine
at 10
chars.

如果我现在要修复此问题(所有行都低于我设置的10个字符限制),我必须找到10个字符的点,点击RETURN(将额外的内容移动下来),然后找到10个下一行的字符点,点击RETURN,依此类推。

这个场景在我的程序和函数序言(我通常使用73个字符行限制)中为我提供了很多,因为我发现自己必须在以后添加更多解释或内容。

有没有快速/简单的方法来解决这个问题?

此外:

我在.vimrc中有此突出显示文字宽度违规者:

" highlight anything past the 73 character limit
augroup vimrc_autocmds
    autocmd Filetype cpp autocmd BufEnter * highlight OverLength ctermbg=darkgrey guibg=#592929
    autocmd Filetype cpp autocmd BufEnter * match OverLength /\%73v.*/
augroup END

也许我可以添加一些内容吗?

2 个答案:

答案 0 :(得分:2)

您可以使用gq命令重新格式化受影响的行。通常情况下,gqap(格式段落)会使用视觉模式或动作。

您还可以在使用:set formatoptions+=a键入时让Vim不断重新格式化,但这对我来说效果不佳。

答案 1 :(得分:0)

Vim手动输入:help fo-table报告:

l       Long lines are not broken in insert mode: When a line was longer than
        'textwidth' when the insert command started, Vim does not
        automatically format it.

由于您在评论中说formatoptions包含字母l,因此您应该使用以下命令启用此选项:

:set formatoptions-=l

要修复现有的行,就像Ingo已经建议的那样,您可以使用符合gq设置的textwidth命令。