2

So I found this solution on StackOverflow here: Vim 80 column layout concerns

If I type a long line in my file, I would like the characters that exceed a limit of 80 characters to be highlighted. A lot of people seem to think that this solution works fine, but I have it in my vimrc file and it behaves as though nothing has changed at all. My long lines do not get highlighted.

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

For reference, here is my entire .vimrc, which isn't that long:

" You'll need to add the following to your ~/.vimrc so that pathogen will be loaded
" properly. Filetype detection must be off when you run the commands so its best to
" You'll need to add the following to your ~/.vimrc so that pathogen will be loaded
" execute them first:
"filetype off
call pathogen#runtime_append_all_bundles()
call pathogen#helptags()
"filetype on

syntax on

let mapleader = ","
let g:CommandTMaxHeight=25
imap ii <Esc>

map <S-Enter> O<Esc>
map <CR> o<Esc>

set guioptions-=T
set guioptions-=r

set hlsearch
highlight OverLength ctermbg=red ctermfg=white guibg=#592929
match OverLength /\%79v.\+/
set nocompatible
set ruler
set number
set shellcmdflag=-ic
set list
set expandtab
set tabstop=4
set softtabstop=4
nmap <C-k> ddkP
nmap <C-j> ddp
vmap <C-k> xkP`[V`]
vmap <C-j> xp`[V`]

au! BufWritePost vimrc source %
colorscheme vividchalk
Community
  • 1
  • 1
jononomo
  • 12,340
  • 25
  • 77
  • 134
  • 1
    if you're using vim 7.3, you can use the second answer from there (`set colorcolumn=80`). – none Oct 24 '12 at 22:16
  • 1
    Have you tried http://stackoverflow.com/questions/235439/vim-80-column-layout-concerns#comment2548357_235970 ? – kgr Oct 24 '12 at 22:22
  • I am using 7.3, but I don't want an entire column highlighted. My preference is for the solution I have described. – jononomo Oct 24 '12 at 22:41
  • @kgr - that correction has already been incorporated in the original answer, which was edited to reflect it. Both the original and the correction suggest using `/\%81v.\+/` The only difference is that I'm using 79 instead of 81. So, yes, I guess I have tried it. – jononomo Oct 24 '12 at 22:46
  • 1
    @JonCrowell ok, I have found out that this works, only not for all file types. E.g. for me works for python source but not for some other files... trying to figure out why.. – kgr Oct 25 '12 at 00:12
  • Hmm, I don't get any highlighting even with python source. For now I'm going with `set colorcolumn=80` but I feel it is second best for my personality type. – jononomo Oct 25 '12 at 02:34
  • possible duplicate of [Vim syntax coloring: How do I highlight long lines only?](http://stackoverflow.com/questions/395114/vim-syntax-coloring-how-do-i-highlight-long-lines-only) – Ciro Santilli新疆棉花TRUMP BAN BAD Oct 22 '13 at 14:52

4 Answers4

2

From: https://stackoverflow.com/a/10993757/1701170

augroup vimrc_autocmds
  autocmd BufEnter * highlight OverLength ctermbg=darkgrey guibg=#111111
  autocmd BufEnter * match OverLength /\%75v.*/
augroup END

This works for me whereas the solution I had before does not. I'm not exactly sure why, but another commenter on the page offers a hint when he remarks, regarding the solution without opening and closing augroup lines, "This only works for the first file you open in any given buffer".

Now if someone could explain why the additional opening and closing lines solve that first-file-in-any-given-buffer problem, and why that problem exists in the first place, then I would feel enlightened.

Community
  • 1
  • 1
jononomo
  • 12,340
  • 25
  • 77
  • 134
  • Opening and closing lines are just for groupping. They don't solve the first line in buffer issue. The "BufEnter" part of each line does. – kgr Oct 25 '12 at 17:50
  • 1
    Thanks for this! Like you I couldn't get the other solutions to work. – smessing Oct 30 '12 at 21:45
1

The following seems to work for me:

highlight OverLength ctermbg=darkred ctermfg=white guibg=#FFD9D9
match OverLength /\%79v.*/

it's bascially the same thing that you have with just \+ changed to * and a different color. For some reason though changing your line didn't work for me. Perhaps there's something wrong with one of the characters?

Anyway - copied from this post: https://stackoverflow.com/a/395326/680238

I think it's the one I used when working on my vimrc, but as a side note I might add I've dropped the idea of highlighting those overlength lines and settled for having vertical line on 81st column. You may want to try this and see which one you like better:

" Highlight first oversize line
set colorcolumn=81
Community
  • 1
  • 1
kgr
  • 9,184
  • 2
  • 36
  • 42
0

In my opinion it's much cleaner to use textwidth and colorcolumn. Look them up in the help for specifics on their function.

set textwidth=80
set colorcolumn=+1

This only highlights the point at which 80 columns is exceeded so may not meet your goals but in my opinion makes things easier to read if you're often loading up files from sources that do not obey your restrictions.

The highlighting is handled by the ColorColumn highlight group.

You can also just set colorcolumn to 81 and don't need to set textwidth but I use textwidth for a few other things also so have it set anyway, and this allows me to change textwidth and get the coloring at the max width regardless.

dash-tom-bang
  • 16,375
  • 4
  • 43
  • 56
0

I know this is a super old post, but I recently found my OverLength highlighting stopped working (Vim 8.1). my highlight code was originally:

augroup vimrc_autocmds
    autocmd BufEnter * highlight OverLength ctermbg=darkred ctermfg=white guibg=#FFD9D9
    autocmd filetype BufEnter * match OverLength /\%>79v.\+/
augroup END

I removed filetype from the 3rd line and it started working again. I don't know which version changed this behaviour, but having filetype in there definitely worked for me previously.

This is pretty much the answer from Jonomono, just clarifying in case anyone else runs into a similar issue :)

Manish M
  • 1
  • 2