ChatGPT解决这个技术问题 Extra ChatGPT

Vim 80 column layout concerns

The way I do 80-column indication in Vim seems incorrect:set columns=80. At times I also set textwidth, but I want 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. This is because vsplit forces me to 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.?

It's considered good code style in many circles; for example, Python's Style Guide (PEP8) recommends 79 character lines: python.org/dev/peps/pep-0008
It's not an absolute "you must have 80 character lines", but it does make it nicer to read on some systems, like for instance any console based application that not running inside a GUI console window that can be resized.
@davr: First, it's easier to read. Some studies suggest a short line length (76 characters a line) is most agreeable and fastest to be read online. Do you know this: suddenly your eyes are on the wrong line when reading? This rarely happens with 80 characters. Secondly I often edit or diff visually with two files side-by-side on my 1600 pixel wide monitor. This only works well if the file does not have lines more than 80 characters. So I often reformat long lines before I can work efficiently.
@davr it's incredibly useful for editing several files side-by-side.
I make far less bugs in both C/C++ and JS code since I adopted an 80 char line width (from the kernel coding style). I occasionally overflow it by a couple, but rarely...

3
3 to 5 business days

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.


You can even automatically base the value of 'colorcolumn' on 'textwidth' with something like :set cc=+1
has("colorcolumn") didn't work for me. I had to use exists("&colorcolumn") instead.
Very nice, except that vim < 7.1.40 does't support matchadd.
you can also change the color of the column with highlight ColorColumn ctermbg=7
S
Simon Howard

I have this set up in my .vimrc:

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

This highlights the background in a subtle red for text that goes over the 80 column limit (subtle in GUI mode, anyway - in terminal mode it's less so).


Great idea! I changed it to "ctermbg=darkred" and "guibg=#FFD9D9" to fit my needs better (light background in gvim and dark in cterm mode).
I think a subtle correction is /\%81v.\+/ -- for some reason the .* highlights places where characters do not exist.
I think this line is slightly better for the match regex: match OverLength /\%>80v.\+/ This doesn't highlight the 'end of line' character and also is more intuitive as you just set the number to what you want the line length to be, not +1.
For some reason it only works in the first opened buffer for me (first window of the first tab) in my vim...
Check out stackoverflow.com/a/10993757/9822 if this solution is only working in your first buffer.
G
Gaelan

Shorter way:

match ErrorMsg '\%>80v.\+'

I really like this because it doesn't pollute my buffer with a long vertical line! (Y)
ErrorMsg is a highlight-group. There are a lot of default highlight groups in Vim which you can use based on the color encoding you want for this case.
C
CDub

I prefer:

highlight ColorColumn ctermbg=gray
set colorcolumn=80

I like this one. I used black instead of gray and it was more subtle since my background is almost black but not quite.
t
the Tin Man

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 exceeds column 80.


Might want to add guifg=white on the end there.
D
Dominykas Mostauskis

https://i.stack.imgur.com/AGsQy.png

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.


Why did you add a FileType scala,java? (not working for me)
@Dorian it limits this to Scala and Java filetypes, because those were the filetypes I was working on at the time. I neglected to mention that. Updating answer.
What font are you using in the screenshot? I find it very pleasing to the eye.
@Idigas: Agreed, and I feel like maybe Comic Sans may have been a bad choice when I originally set up PuTTY :)
I wasn't able to get the snippet in this answer to work in Vim 7.4. (I removed the FileType filter too.)
S
Shanded

A nice way of marking just the first character going out of the specified bounds:

highlight ColorColumn ctermbg=magenta "set to whatever you like
call matchadd('ColorColumn', '\%81v', 100) "set column nr

From Damian Conway's talk.


0
0x8BADF00D

You also can draw line to see 80 limit:

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

Result:

https://i.stack.imgur.com/6kqGm.png


M
Matthew Scharley

Newer versions of vim allow a :set numberwidth=x value, which sets the width of the line number display. I don't really use folding etc, so I wouldn't know about that though. Drawing a thin vertical line is beyond the abilities of a console application though. GVim may allow this (I don't use it, so can't comment there).


L
Lucas Oman

I'm afraid that you've put constraints on the set of solutions that, well, leave you with the null set.

Using :set textwidth=80 will fix all of the problems you mentioned except that you can't easily see the line limit coming up. If you :set ruler, you'll enable the x,y position display on the status bar, which you can use to see which column you're in.

Aside from that, I'm not sure what to tell you. It's a shame to lose the number column, fold column and splits just because you have to :set columns=80.


Yeah, I was afraid of that... I was hoping there would be a hidden way to draw a thin vertical line like in more graphically oriented editors.
A
Aristotle Pagaltzis

You can try this:

au BufWinEnter * if &textwidth > 8
\ | let w:m1=matchadd('MatchParen', printf('\%%<%dv.\%%>%dv', &textwidth+1, &textwidth-8), -1)
\ | let w:m2=matchadd('ErrorMsg', printf('\%%>%dv.\+', &textwidth), -1)
\ | endif

That will set up two highlights in every buffer, one for characters in the 8 columns prior to whatever your &textwidth is set to, and one for characters beyond that column. That way you have some extent of anticipation. Of course you can tweak it to use a different width if you want more or less anticipation (which you pay for in the form of loss of syntax highlighting in those columns).


D
Ding-Yi Chen

Well, looking at the :help columns, it's not really being made to mess with.

In console, it's usually determined by console setting (i.e. it's detected automatically) ; in GUI, it determines (and is determined by) the width of the gvim windows.

So normally you just let consoles and window managers doing their jobs by commented out the set columns

I am not sure what you mean by "see and anticipate line overflow". If you want EOL to be inserted roughly column 80, use either set textwidth or set wrapmargin; if you just want soft wrap (i.e. line is wrapped, but no actual EOL), then play with set linebreak and set showbreak.


E
ErichBSchulz

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> 

M
Mike L

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.


T
Techie

set column line using set colorcolumn

set autoindent                                                                  
set smartindent                                                                 
set nowrap                                                                      
set hlsearch                                                                    
set tabstop=4                                                                   
set shiftwidth=4                                                                
syntax on                                                                       
set colorcolumn=80