-1

I want to include a new highlight group and autocommand that will automatically highlight non-ASCIIs as soon as either a vimscript or a Python file is loaded in buffer (i.e. any file with filetype 'vim').

I included what follows in my ~/.vimrc:

highlight NonASCII ctermbg=Black ctermfg=Red cterm=italic
                    " guibg=Black guifg=Red
autocmd BufReadPost * if  count(['vim','python'],&filetype) 
            \ | syntax match Nonascii "[^\u0000-\u007F]"  containedin=ALL 
            \ | endif

... does not highlight anything.

I tried replacing "[^\u0000-\u007F]" with the equivalent "[^\x00-\x7F]" and "[^\d0-\d127]" without any more success.

Any help on this much welcome.

EDIT:

The syntax highlight group Nonascii does show up in output of :hi along with others I tweaked or created.

The complete content of my ~/.vimrc is available as the file vimrc here. So is output of :syntax in file syn.out.

I created file test.vim whose content is simply:

#!/usr/bin/vim
set encoding=utf-8
scriptencoding=utf-8
set termencoding=utf-8
inoremap <C-º> <Esc> 
"  é è ä

... then executed $ vim -V13debug.log test.vim.
For those (as I) new to this, a good tutorial to get started with debugging vimscripts is here.

  • 1
    Sorry, I mixed up two things. You can do autocmd BufRead *.vim,*.python (you need glob like file patterns) OR autocmd filetype vim,python. The latter triggers when filetype is set so go for the first option. – B Layer Oct 28 '18 at 13:22
  • 1
    au BufWinEnter,BufWinEnter * :call matchadd('ErrorMsg', '[^\x00-\x7f]') – Christian Brabandt Oct 28 '18 at 18:07
  • Thanks @ChristianBrabandt, but why the repeated BufWinEnter ?? As a side note, the augroup I wrote up initially in my OP, is confirmed right (see comments beneath @BLayer's answer), but seems to introduce an incompatibility with one of my nine plugins. I'm new to this so it's taking me time to get things right... – Cbhihe Oct 28 '18 at 18:15
  • @Cbhihe that is a quick working solution. For a real solution, one would need to handle errors gradefully, do not call is several times, etc. That's why it was posted as comment and not as solution. – Christian Brabandt Oct 29 '18 at 8:02
1

As we discussed in comments you can restrict this to specific file patterns in place of *. For vim type files *.vim is usually sufficient.

Next, I tried it with a known highlighting, ErrorMsg:

autocmd BufRead *.vim,*.python,*.py syntax match ErrorMsg "[^\x00-\x7F]" containedin=all

This works fine for me. I opened foo.vim and added a couple digraphs and they were colored as expected.

Next I tried your highlighting command and that also works. Still, terminals and coloring can be finicky so you should verify that it works for you outside of the syntax matching.

All in all things work for me as specified above so you might have a typo or other small error somewhere.

(From a previous comment...)

I assumed syntax highlighting is working for you in general and you therefore have all correct configuration, generally speaking. So what to try next...

  • Are your entries showing up in :syn and :hi (or :verbose hi to show origin) listings? (OP already checked this.)
  • Speaking of :verbose you might want to think about setting that and stepping through initialization
  • Try a stripped down vimrc with only the relevant parts
  • Use --noplugin to rule out any interference from installed plugins

Update: OP observes that everything works fine when Vim is started with --noplugin. They tracked the issue down to a vim-autoclose plugin, reported a bug (though the plugin may no longer be under maintenance), and switched to alternative plugin delimitMate. That's probably the end-of-the-road for this Q&A.

  • Mine is inside augroup. I just omitted that since it's not that important here. I assumed syntax highlighting was working for you in general and you therefore have all correct configuration, generally speaking. Are your entries showing up in :syn and :hi (or :verbose hi to show origin) listings? (oh, you already said that one) Speaking of :verbose you might want to think about setting that and stepping through initialization. Other options: use stripped down vimrc with only the relevant parts, use --noplugin to rule out interference from plugins. – B Layer Oct 28 '18 at 14:15
  • Running vim --noplugin produces the perfect highlight result I want. – Cbhihe Oct 28 '18 at 18:05
  • Edit in OP with added output files in particular the debug.log produced when stepping thru initialization with plugins. – Cbhihe Oct 28 '18 at 19:09
  • One plugin seems to be at the origin of the pbm: `Plugin 'Townk/vim-autoclose' . If I start a session with it disabled, everything returns to normal, except I am back to closing every quote and bracket myself. – Cbhihe Oct 28 '18 at 19:23
  • Replaced Plugin 'Townk/vim-autoclose' with Plugin 'Raimondi/delimitMate' and all is fine. Pffeew ! I did bother with opening an issue on Townk's repo tracker, but the plugin does not seem to be maintained from what I saw of the tracker page. – Cbhihe Oct 28 '18 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.