1

From these two questions:

Vim 80 column layout concerns

Vim syntax coloring: How do I highlight long lines only?

I've extracted the following config for my .vimrc:

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

This works fine for highlighting lines longer that 80 characters in vim, but when I open another tab of the same file using:

:tab split

the highlighting doesn't work in the new tab, only in the original one. How can I achieve the same effect for the new tab?

Community
  • 1
  • 1

1 Answers1

3

Here is a cleaned up version of your snippet:

highlight OverLength ctermbg=darkred ctermfg=white guibg=#FFD9D9

augroup vimrc_autocmds
    autocmd!
    autocmd BufEnter,WinEnter * call matchadd('OverLength', '\%>80v.\+', -1)
augroup END
  • The autocommands in that group are properly cleared when/if you reload your vimrc.
  • The BufEnter event is only triggered once, you need to listen to another event, WinEnter, which is triggered when a window gets the focus.
  • matchadd() is more flexible than :match and allows you to set the priority of the highlighting (useful if you rely on hlsearch).
romainl
  • 158,436
  • 18
  • 236
  • 264