143

我想使用 Vim 的软包装功能 ( :set wrap) 将一些代码包装成 80 个字符,而不管我的实际窗口宽度如何。

我还没有找到一种方法来做到这一点——所有的软包装似乎都与窗户的宽度有关

  • textwidth并且wrapmargin都用于硬包装(它们在文件中插入换行符)
  • 垂直拆分为多个窗口并在其中一个上使用:vertical resize 80(可能:set breakat=允许在任何字符上中断)某种作品(即使它有点hackish),但是在使用时中断,:set number因为行号占用可变数量的列(取决于关于文件长度),这些是 80 的一部分。

有没有办法在vim中做到这一点? 根据其他消息来源,它看起来并不乐观

现在我的近似值只是/^.\{80}\zs.\+作为我的默认搜索,所以它至少被突出显示。我想:syntax为它添加一个项目,但是当它与其他语法项目重叠时就中断了,所以我放弃了这个想法。

4

6 回答 6

44

你可以

  • :set numberwidth=6通过和为行号列设置较大的最小宽度
  • 然后您可以使用:set columns=86(或使用鼠标)将窗口大小调整为适当的大小。

如果您编辑包含一百万行的文件,您可能会遇到麻烦,但这不太可能。您也以这种方式浪费了 6 列屏幕空间。所以还是有各种各样的问题。

您可以使用此处此处:match所说的方式突出显示第 80 列。

除此之外,我看不到任何方法可以做到这一点。似乎这将是一个不错的功能。

于 2009-06-12T22:16:19.260 回答
25

试试这个:

set columns=80
autocmd VimResized * if (&columns > 80) | set columns=80 | endif
set wrap
set linebreak
set showbreak=+++

if (&columns > 80) |如果您总是想要 80 列,则可以删除。

于 2014-10-09T17:27:20.757 回答
17

我没有软包装的解决方案,但至于标记列,从 Vim 7.3(2010-08-15 发布):set colorcolumn=80开始将突出显示第 80 列。颜色将取决于您的语法文件。

请参阅Vim 80 列布局问题:h colorcolumn.

于 2011-06-12T13:32:23.247 回答
3

你试过'linebreak'吗?

        *'linebreak'* *'lbr'* *'nolinebreak'* *'nolbr'*
  'linebreak' 'lbr' boolean (default off)
        local to window
        {not in Vi}
        {not available when compiled without the  |+linebreak|
        feature}
If on Vim will wrap long lines at a character in 'breakat' rather
than at the last character that fits on the screen.  Unlike
'wrapmargin' and 'textwidth', this does not insert <EOL>s in the file,
it only affects the way the file is displayed, not its contents.  The
value of 'showbreak' is used to put in front of wrapped lines.
This option is not used when the 'wrap' option is off or 'list' is on.
Note that <Tab> characters after an <EOL> are mostly not displayed
with the right amount of white space.
于 2010-04-12T19:57:27.743 回答
1

eborisch 的答案与我在这里找到的其他一些答案以及我必须解决的问题相结合,我想出了以下两部分的解决方案:

第一部分使编辑长行文本变得更容易:

" Allow enabling by running the command ":Freeform", or <leader>sw
command! Softwrap :call SetupSoftwrap()
map <Leader>sw :call SetupSoftwrap() <CR>

func! SetupFreeform()
  " Use setlocal for all of these so they don't affect other buffers

  " Enable line wrapping.
  setlocal wrap
  " Only break at words.
  setlocal linebreak
  " Turn on spellchecking
  setlocal spell

  " Make jk and 0$ work on visual lines.
  nnoremap <buffer> j gj
  nnoremap <buffer> k gk
  nnoremap <buffer> 0 g0
  nnoremap <buffer> $ g$

  " Disable colorcolumn, in case you use it as a column-width indicator
  " I use: let &colorcolumn = join(range(101, 300), ",")
  " so this overrides that.
  setlocal colorcolumn=

  " cursorline and cursorcolumn don't work as well in wrap mode, so
  " you may want to disable them. cursorline highlights the whole line,
  " so if you write a whole paragraph on a single line, the whole
  " paragraph will be highlighted. cursorcolumn only highlights the actual
  " column number, not the visual line, so the highlighting will be broken
  " up on wrapped lines.
  setlocal nocursorline
  setlocal nocursorcolumn
endfunc

仅凭这一点,您就可以获得不错的文本包装,以编写诸如降价或自述文件之类的内容。

如其他答案所述,以确切的列宽进行换行需要准确地告诉 vim 有多少列,并在每次调整 vim 大小时覆盖它:

command! -nargs=? Draft :call SetupDraftMode(<args>)
func! SetupDraftMode()
  " I like 80 columns + 4 for line numbers
  set columns=84
  autocmd VimResized * if (&columns > 84) | set columns=84 | endif

  :Softwrap
endfunc

这仍然存在几个问题:

  • vim 不会在调用 set columns 后清除你指定的列之外的屏幕,我不知道如何告诉它,所以理想情况下你应该在打开 vim 后立即调用它
  • vim 会在您打开它时显示带有版本号和一些有用命令的提示,因此这些不会被清除。您可以添加set shm+=I以禁用该提示
  • 您不能打开任何垂直拆分,因为这样两个拆分都将是 ~40 列。您需要将列设置为所需宽度的 2 倍,然后始终打开拆分。
  • 我的 vimscript 很糟糕,但理想情况下,有人可以修改Draft上面的函数以将列宽作为参数,或者使用g:explicit_vim_width可以在窗口大小发生变化时手动设置的全局变量 (?)。
于 2021-12-09T22:57:10.200 回答
-1

没有好的方法可以做到这一点。如果我们修改@eborisch 的答案setlocal softwrap,我们可以破解临时工具。autocmd如果我们每次进入缓冲区时都调整大小,并且在softwrap设置局部变量时调整到特定长度,我们会得到所需的行为。

假设我们要软换行到 80 列,我们可以在.vimrc.

augroup softwrap
    autocmd VimResized * if (exists('b:softwrap') && &columns > 80) | set columns=80 | endif
    autocmd BufEnter * set columns=999
augroup END

要打开特定缓冲区的模式,请使用以下命令:

let b:softwrap=1
set columns=80
于 2018-12-24T17:53:27.683 回答