0

I want to highlight lines that are longer than the set textwidth so that it is dynamic for different filetypes. I have the following:

autocmd BufEnter * highlight OverLength ctermbg=darkgrey guibg=#592929  
autocmd FileType * execute 'match OverLength /%\{'.&textwidth.',}v.*/'  

This code doesn't seem to activate the highlighting, however, the following does work:

autocmd BufEnter * highlight OverLength ctermbg=darkgrey guibg=#592929  
autocmd BufEnter * match OverLength /\%>90v.\+*/  

I have tried excluding the execute statement and adding the backslashes accordingly but I still cannot seem to get this to work. Also, I think using the execute statement makes this code cleaner. I would appreciate it if someone could explain to me what I am doing incorrectly.

I have referenced the following examples in pursuit of the answer:

highlight long lines

expand a variable in regular expression

If there is a more portable and cleaner way to go about this then please let me know. I am trying to learn as much as possible.


ANSWER:
What I ended up doing is utilizing vim's vim/after/ftplugin folder hierarchy and adding: set textwidth=100
let &colorcolumn=join(range(&textwidth+1,999),",") to each filetype.vim file e.g. markdown.vim, java.vim, vim.vim

And for help.vim and man.vim files I didn't want colorcolumn highlighting so I changed the textwidth in those files to 999. By doing this vim was able to distinguish between different file types when they were open in the same session.

Community
  • 1
  • 1

1 Answers1

0

What I ended up doing is utilizing vim's vim/after/ftplugin folder hierarchy and adding: set textwidth=100
let &colorcolumn=join(range(&textwidth+1,999),",") to each filetype.vim file e.g. markdown.vim, java.vim, vim.vim

And for help.vim and man.vim files I didn't want colorcolumn highlighting so I changed the textwidth in those files to 999. By doing this vim was able to distinguish between different file types when they were open in the same session.

– user2904000

Armali
  • 18,255
  • 14
  • 57
  • 171