0

So, I've had the following chunk in my vimrc for a few years:

" This toggles the hilighting of trailing-whitespace.
fun! ToggleExtraWhitespace()

   if exists('b:ews') && b:ews == 1
     "echom "Disabling trailing-whitespace hilighting in" bufnr('%') "..."
      let b:ews=0
      call HighlightExtraWhitespace()

     "echom "-- Removing ExtraWhitespace augroup"
      au!      ExtraWhitespace
      augroup! ExtraWhitespace

   else
     "echom "Enabling trailing-whitespace hilighting in" bufnr('%') "..."
      let b:ews=1
      call HighlightExtraWhitespace()

     "echom "-- Adding ExtraWhitespace augroup"
      augroup  ExtraWhitespace
         au!
         au BufEnter    * match ExtraWhitespace /\s\+$/
         au InsertEnter * match ExtraWhitespace /\s\+\%#\@<!$/
         au InsertLeave * match ExtraWhiteSpace /\s\+$/
      augroup END

      if mode() == "i" | do ExtraWhitespace InsertEnter | else | do ExtraWhitespace BufEnter | endif

   endif
endfun

" This adds (or removes) the actual hilighting to your ColourScheme. (It's must be re-called every
" time you toggle hilighting, or change your scheme.)
fun! HighlightExtraWhitespace()
   if exists('b:ews') && b:ews == 1
     "echom "-- Adding ExtraWhitespace hilighting"
      highlight ExtraWhitespace ctermbg=red guibg=red
   else
     "echom "-- Removing ExtraWhitespace hilighting"
      highlight clear ExtraWhitespace
   endif
endfun
au ColorScheme * call HighlightExtraWhitespace()

" (Uncomment the following line if you want trailing-whitespace hilighted by default!)
bufdo call ToggleExtraWhitespace() | au BufAdd * call ToggleExtraWhitespace()

I've just realised, however, that this prevents me from opening multiple files in one command-line command (I usually do my browsing inside vim, hence why it took me so long to notice!): nvim -o lib/ocameel.ml bin/cli.ml opens two buffers containing cli.ml, and doesn't open lib/ocameel.ml at all!

If I comment out the last line, everything works fine; I can still manually invoke :call ToggleExtraWhitespace(), and everything's hunky-dory.

I'd really like to figure out, though, why adding that autocommand breaks vim-entry. What's my BufAdd screwing up? D:

ELLIOTTCABLE
  • 17,185
  • 12
  • 62
  • 78
  • 1
    Are you sure it's the autocommand? The `bufdo` looks much more suspicious. And why are you trying to add your autocommand repeatedly (for every buffer that's open)? – melpomene Nov 21 '17 at 18:28
  • `bufdo` in `.vimrc` doesn't make sense - buffers have not yet been loaded. If you want a robust solution, have a look at plugins - e.g. my [ShowTrailingWhitespace plugin](http://www.vim.org/scripts/script.php?script_id=3966). – Ingo Karkat Nov 22 '17 at 13:43

0 Answers0