简体   繁体   中英

sourcing vimrc when opening a file in tab page?

I have a the following in my vimrc to highlight any lines that go past 80 columns:

highlight ColorColumn ctermfg=red ctermbg=bg
call matchadd('ColorColumn', '\%81v.\+', 100)

It works great in most cases. However, I've noticed that if i open a file in a new tab, it doesn't work at all. I'm able to fix this by :source $MYVIMRC . But the issue is, when I source my vimrc I lose my indentLines plugin. I've done a little testing, and I have found that the indentLines goes away anytime the vimrc is sourced in an open instance of vim. Still, I am unable to determine why the 2 lines shown above are not being called when I open a file in a new tab. Any ideas?

my vimrc

The matchadd() only affects the current window. In order to have it on all windows you can add the following to your .vimrc:

if exists("*matchadd")
   augroup colorColumn
      au!
      au BufEnter * call matchadd('ColorColumn', '\%81v.\+', 100)
   augroup END
endif

Edit: As pointed by Ingo on the comments, the BufEnter will trigger many times when it is not necessary. The lines below correct this issue:

if exists("*matchadd")
   augroup colorColumn
      au!
      au VimEnter,WinEnter * call matchadd('ColorColumn', '\%81v.\+', 100)
   augroup END
endif

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM