1

I wish to turn off syntax highlightings, only for terminal session having a black-and-white (BW, B/W) terminal.

And only for that particular plugin whose syntax vimscript is CPU-intensive which I am a plugin developer.

Some plugins have huge CPU overhead, notably with syntax highlighter plugins (ones in the $HOME/.vim/syntax).

I've got a black-and-white terminal and wish to turn that off syntax highlighter, preferably using some kind of auto-detection as derived from tput colors or even $TERM.

Is it possible not to have my .vimrc set the value of t_Co.

What is this ideal vimscript snippet?

2 Answers 2

1

This is an excerpt of the default.vim that Vim uses when there are no .vimrc available:

" Switch syntax highlighting on when the terminal has colors or when using the
" GUI (which always has colors).
if &t_Co > 2 || has("gui_running")
  " Revert with ":syntax off".
  " Enable syntax highlighting
  syntax on
endif

This disable the syntax for all the buffers when t_Co <= 2 and gui_running is false.

If you want to disable only a given expensive syntax for a specific filetype I would do:

~/vimfiles/ftplugin/myplugin.vim

let g:force_syntax_x = 0
command! ForceSyntaxX let g:force_syntax_x = 1

~/vimfiles/syntax/myplugin.vim

if &t_Co > 2 || has("gui_running") || g:force_syntax_x
  " Syntax definition for x
  " ...
else
  echo 'run :ForceSyntaxX to enable syntax for x'
endif
6
  • 1
    Is this safe (non-disruptive) enough to include into the .vimec/syntax/myplugin.vim without interfering with the entire Vim ecosystem of plugins? Commented Sep 3 at 18:43
  • I would include it in you .vimrc file instead. The ~/vimfiles/syntax/myplugin.vim is executed when a buffer of filetype myplugin is loaded (and it seems to be an odd place to put such global syntax on call). But maybe I miss some of your intentions. Commented Sep 3 at 18:48
  • It would be useful to have syntax disabled as a default for a very large syntax highlighter vimscript file (ie. netfilter or ISC Bind9) and (somehow output a line into the bottom line "to enable syntax, enter in :syntax on for highlighting"). If this is the ideal location, then we would need to have some form of t_Co logic in that large syntax file. Commented Sep 3 at 18:54
  • 1
    Thanks for your fast answer. I have updated the solution according to my new understanding. That being said if there are no color in the terminal I suppose it makes sense to disable the syntax for all file types (like the default.vim does it). Commented Sep 3 at 19:04
  • 1
    New stuff, gotta experiment with those. Commented Sep 3 at 19:16
0

Keep it simple. There is no need to devise cool heuristics for things that are easily done in interactive mode.

If you want to switch off syntax highlighting for some file then type :set syntax=off and that's it. It will not until you do :set syntax=foobar (or reload file, or re-set file type).

Also, you can switch between automatic and manual syntax setting globally by commands :syntax enable and :syntax manual (also :syntax off to disable syntax scripts completely for all buffers). In "enable" mode Vim may copy :h 'filetype' option into :h 'syntax' option; in "manual" mode the latter is only set by the user.

4
  • Is syntax on a local, global, or context-specific command? Commented Sep 3 at 20:16
  • @JohnGreene In Vim speak "local command" is one that defined only for specific buffer. Clearly, it is not the case. If you mean what it does then syntax on executes a script from Vim's runtime. If you mean what that script could do then it creates some autocommands. These autocommands may affect different buffers if executed. Is that your question?
    – Matt
    Commented Sep 3 at 20:26
  • yes. Thank you for the Vim-speak. Not all there myself ... yet. Commented Sep 3 at 20:33
  • @JohnGreene The whole point of all this is to execute your syntax script for a specific kind of files by specific moment. In principle, you could open that file and type all these "syn-match" commands by hand producing the same outcome.
    – Matt
    Commented Sep 3 at 21:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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