Vim 80 column layout concerns

2019-01-02 21:30发布

I feel like the way I do 80-column indication in Vim is incorrect: set columns=80. At times I also set textwidth but I like to be able to see and anticipate line overflow with the set columns alternative.

This has some unfortunate side effects -- I can't set number for fear of splitting between files that have different orders of line numbers; i.e. < 100 line files and >= 100 line files will require two different set columns values because of the extra column used for the additional digit display. I also start new (g)Vim sessions instead of splitting windows vertically, which forces me to use the window manager's clipboard -- vsplits force me to do set columns every time I open or close a pane, so starting a new session is less hassle.

How do you handle the 80-character indication when you want to set numbers, vertically split, etc.?

14条回答
做自己的国王
2楼-- · 2019-01-02 22:06

As of vim 7.3, you can use set colorcolumn=80 (set cc=80 for short).

Since earlier versions do not support this, my .vimrc uses instead:

if exists('+colorcolumn')
  set colorcolumn=80
else
  au BufWinEnter * let w:m2=matchadd('ErrorMsg', '\%>80v.\+', -1)
endif

See also the online documentation on the colorcolumn option.

查看更多
一纸荒年 Trace。
3楼-- · 2019-01-02 22:06

Simon Howard's answer is great. But /\%81v.\+/ fails to highlight tabs that exceed column 81 . So I did a little tweak, based on the stuff I found on VIM wiki and HS's choice of colors above:

highlight OverLength ctermbg=darkred ctermfg=white guibg=#FFD9D9
match OverLength /\%>80v.\+/

And now VIM will highlight anything that exceed column 80. Cheers!

查看更多
三岁会撩人
4楼-- · 2019-01-02 22:08

You can try this to set the window size to allow 80 characters of actual text. This still doesn't work with vertical splits though.

let &co=80 + &foldcolumn + (&number || &relativenumber ? &numberwidth : 0)

This requires vim 7+, 7.3 for relativenumber.

查看更多
beautiful°
5楼-- · 2019-01-02 22:10

You also can draw line to see 80 limit:

let &colorcolumn=join(range(81,999),",")
let &colorcolumn="80,".join(range(400,999),",")

Result:

enter image description here

查看更多
做自己的国王
6楼-- · 2019-01-02 22:17

this one is out of left field but its a nice little map for resizing your current split to 80 characters if you've got the line numbers on:

" make window 80 + some for numbers wide  
noremap <Leader>w :let @w=float2nr(log10(line("$")))+82\|:vertical resize <c-r>w<cr> 
查看更多
Animai°情兽
7楼-- · 2019-01-02 22:18

enter image description here

Minimalistic, not-over-the-top approach. Only the 79th character of lines that are too long gets highlighted. It overcomes a few common problems: works on new windows, overflowing words are highlighted properly.

augroup collumnLimit
  autocmd!
  autocmd BufEnter,WinEnter,FileType scala,java
        \ highlight CollumnLimit ctermbg=DarkGrey guibg=DarkGrey
  let collumnLimit = 79 " feel free to customize
  let pattern =
        \ '\%<' . (collumnLimit+1) . 'v.\%>' . collumnLimit . 'v'
  autocmd BufEnter,WinEnter,FileType scala,java
        \ let w:m1=matchadd('CollumnLimit', pattern, -1)
augroup END

Note: notice the FileType scala,java this limits this to Scala and Java source files. You'll probably want to customize this. If you were to omit it, it would work on all file types.

查看更多
登录 后发表回答