Reputation: 14836
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
Upvotes: 2
Views: 1788
Reputation: 33
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 :)
Upvotes: 0
Reputation: 17843
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.
Upvotes: 0
Reputation: 14836
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.
Upvotes: 2
Reputation: 9948
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
Upvotes: 1