Vim 中任意宽度窗口中的 80 个字符软换行
我想使用 Vim 的 软包装 功能 (:set wrap
) 将一些代码包装在 80 个字符处,而不管我的实际窗口宽度如何。
我还没有找到一种方法来做到这一点 - 所有软包装似乎都与窗口的宽度相关
textwidth
和wrapmargin
都用于硬包装(他们在文件中插入换行符)- 垂直拆分为多个窗口,并在其中一个窗口上使用
:vertical resize 80
(可能使用:set breakat=
以允许在任何字符上中断)它们可以工作(尽管有点hackish),但是在使用:set number
时会中断,因为行号占据可变数量的列(取决于文件长度),并且这些是80.
有没有办法在vim中做到这一点? 根据其他消息来源,这看起来前景并不乐观。
现在我的近似是将 /^.\{80}\zs.\+
作为我的默认搜索,因此它至少会突出显示。 我考虑过为其添加一个 :syntax
项,但是当它与其他语法项重叠时,它就会崩溃,所以我放弃了这个想法。
I want to use Vim's soft wrap capability (:set wrap
) to wrap some code at 80 characters, regardless of my actual window width.
I haven't been able to find a way to do this yet - all the soft wrapping seems tied to the width of the window
textwidth
andwrapmargin
are both for hard wrapping (they insert newline characters into the file)- vertical splitting into multiple windows and using
:vertical resize 80
(possibly with:set breakat=
to allow breaks on any character) on one of them sort of works (even though it's a bit hackish), but breaks when using:set number
as the line numbers take up a variable number of columns (depending on the file length) and these are part of the 80.
Is there any way to do this in vim? It doesn't look promising, according to other sources.
Right now my approximation is just to have /^.\{80}\zs.\+
as my default search so it's at least highlighted. I thought about adding a :syntax
item for it, but that broke when it overlapped other syntax items, so I dropped that idea.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以
:set numberwidth=6
为行号列设置较大的最小宽度,然后:set columns=86
(或使用鼠标)到适当的尺寸。如果您编辑一个包含一百万行的文件,您可能会遇到麻烦,但这不太可能。 这样您也浪费了 6 列的屏幕空间。 所以还是存在各种各样的问题。
您可以使用
:match
突出显示第 80 列,就像它所说的 此处和此处。除此之外,我看不到任何方法可以做到这一点。 不过,这似乎是一个不错的功能。
You could
:set numberwidth=6
and:set columns=86
(or with the mouse) to the proper size.If you edit a file with a million lines in it, you may have trouble, but that's unlikely. You're wasting 6 columns of screen real estate this way too. So there are still all kinds of problems.
You can highlight past the 80th column using
:match
like it says here and here.Beyond that I can't see any way to do this. Seems like it'd be a nice feature though.
试试这个:
如果您总是想要 80 列,则可以删除
if (&columns > 80) |
。Try this:
You can remove the
if (&columns > 80) |
if you always want 80 columns.我没有软包装的解决方案,但对于标记列,从 Vim 7.3(2010-08-15 发布)开始
:set colorcolumn=80
将突出显示第 80 列。颜色将取决于您的语法文件。请参阅 Vim 80 列布局问题、
:h colorcolumn
。I don't have a solution to the soft wrap, but as for marking a column, as of Vim 7.3 (released 2010-08-15)
:set colorcolumn=80
will highlight column 80. The color will depend on your syntax file.See Vim 80 column layout concerns,
:h colorcolumn
.将eborisch的答案与我在这里找到的其他一些答案以及我必须解决的问题相结合,我想出了以下两个-部分解决方案:
第一部分使编辑长行文本变得更容易:
仅凭这一点,您就可以获得不错的文本换行,以便编写诸如 Markdown 或自述文件之类的内容。
正如其他答案中所述,以精确的列宽进行换行需要准确地告诉 vim 有多少列,并在每次调整 vim 大小时覆盖该列:
这仍然存在一些问题:
set shm+=I
来禁用该提示Draft
函数以将列宽作为参数,或者使用全局变量(g:explicit_vim_width
?)如果您的窗口大小发生变化,请手动设置。Combining eborisch's answer with some other answers I found here and things I had to work around, I came up with the following two-part solution:
This first part makes it easier to edit text with long lines:
With this alone you can get decent text wrapping for writing something like markdown, or a Readme.
As noted in other answers, getting wrapping at an exact column width requires telling vim exactly how many columns there are, and overwriting that each time vim gets resized:
There are still a couple of problems with this:
set shm+=I
to disable that promptDraft
function above to take a column width as an argument, or use a global variable (g:explicit_vim_width
?) that can be set manually if your window size changes.您尝试过
'linebreak'
吗?Have you tried
'linebreak'
?没有什么好的办法。 如果我们修改 @eborisch 答案,我们可以使用
autocmd
破解一个临时的setlocal softwrap
。 如果我们每次进入缓冲区时都调整大小,并且在设置局部变量softwrap
时调整大小到特定长度,我们就会得到所需的行为。假设我们想要软包装到 80 列,我们可以在
.vimrc
中编写以下内容。要打开特定缓冲区的模式,请使用以下命令:
There is no good way to do it. We can hack a makeshift
setlocal softwrap
withautocmd
if we modify @eborisch answer. If we resize every time we enter a buffer, and we resize to a particular length when the local variablesoftwrap
is set, we get the desired behaviour.Let's suppose that we want to soft wrap to 80 columns, we can write the following in
.vimrc
.To turn on the mode for a particular buffer, use the following commands: