使Vim在设置的文字宽度的边缘显示一条线


26

我最近使用的大多数文本编辑器和IDE都有一个功能,它们可以在文本缓冲区中以特定字符长度显示一行。当您想将文件中的行数保持一定长度时,此功能很有用。

有什么方法可以使Vim做到这一点,最好使用已经定义的textwidth值?线条将在此时自动换行,但我真的很希望能够看到它的位置。

如果很重要,我主要在Windows上使用gVim,但是如果该解决方案可在Vim版本之间使用,我会喜欢的。

Answers:


41

对于(g)vim,请使用以下命令:

set colorcolumn=80

或您想要的任何宽度。适用于vim和gvim。我在IF中有我的文件,所以这取决于我编辑的文件类型。

您也可以使用+ x / -x来设置&textwidth中+/-列的基本位置。

set textwidth=80
set colorcolumn=-2

将有效地在char位置78绘制彩色条。当然,您可以自行设置文本宽度,也可以不设置,因此它可以为0(默认值)。我使用绝对头寸表格。

您还可以根据需要更改使用的颜色:

highlight ColorColumn ctermbg=green guibg=orange

(尽管我不推荐这些颜色)

在(g)vim 7.3中添加了此选项。


支持哪个版本的Vim?在gvim 7.2中对我不起作用。
爱马仕(Herms)2010年

我正在运行7.3。我只是拉出源代码,然后看一下,该命令是7.3中引入的。
lornix 2010年

适用于具有vim 7.3的Ubuntu 12.04。Windows的 gvim 7.3 至少从2010年10月开始提供。@Herms,您能否回答这个问题?
poindexter 2013年

如果这可以并排使用多个窗口,则很好。我的监视器足够宽,可以连续显示几个窗口,每个窗口> 80个字符,但colorColumn仅在第一个(最左侧)窗口上有效。
伊诺2015年

3

Google代码中有一个代码段,您可以尝试:

augroup vimrc_autocmds
au!
    autocmd BufRead * highlight OverLength ctermbg=red ctermfg=white guibg=#592929 
    autocmd BufRead * match OverLength /\%81v.*/
augroup END

嗯,那个线程暗示Windows的gvim已经支持它了,但是我不知道如何打开它。
爱马仕

在该线程中还有一些代码可以添加到您的gvimrc文件中。
EBGreen

似乎只是改变了超出该行的字符的背景。我宁愿像其他编辑一样拥有一个始终可见的指南。
爱马仕

由于gui的性质,我不太乐观,这是当前可能的。
EBGreen

这不适用于问题中要求的textwidth。
史蒂文·罗斯

2

每个StackOverflow答案

highlight OverLength ctermbg=red ctermfg=white guibg=#592929
match OverLength /\%81v.*/

调整以适合您的口味。


嗯,不是什么我正在寻找(仅显示在人物晃过线),但它是聊胜于无。
爱马仕

这不适用于问题中要求的textwidth。
史蒂文·罗斯

1

我喜欢lornix”回答了很多,但我并不想突出列所有的时间,只有当至少一个行超过长度限制:

行过长时显示列

这是我为Haskell文件执行的操作:

augroup HaskellCommands
autocmd!
  " When a Haskell file is read or the text changes in normal or insert mode,
  " draw a column marking the maximum line length if a line exceeds this length
  autocmd BufRead,TextChanged,TextChangedI *.hs call ShowColumnIfLineTooLong(80)
augroup END

" Color the column marking the lengthLimit when the longest line in the file
" exceeds the lengthLimit
function! ShowColumnIfLineTooLong(lengthLimit)
  " See /programming/2075276/longest-line-in-vim#2982789
  let maxLineLength = max(map(getline(1,'$'), 'len(v:val)'))

  if maxLineLength > a:lengthLimit
    highlight ColorColumn ctermbg=red guibg=red
    " Draw the vertical line at the first letter that exceeds the limit
    execute "set colorcolumn=" . (a:lengthLimit + 1)
  else
    set colorcolumn=""
  endif
endfunction

0

经常在#vim和某些论坛上对此进行讨论。就目前情况而言,这是不可能的。因此,提到的解决方案是您唯一的选择,afaik。

问题是,vim可以在有字符的地方(它们是字母,数字或纯空白)执行任何操作。但是,如果那里什么也没有,它就无法以其他颜色绘制背景(就像您想要的那样)。而且在您键入内容之前,这里没有任何内容,因此它无法绘制线/边距。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.