I want to write a simple function that will toggle syntax highlighting, I imagine it'll just be a simple if-else.

Is there a condition I can check? If "syntax is on"?

2 Answers 2


You can use global variable g:syntax_on (more specifically, the existence of such variable) to determine whether syntax highlighting is on or off.

See :help g:syntax_on, which actually even includes an example of a mapping that toggles syntax highlighting:

:map <F7> :if exists("g:syntax_on") <Bar>
    \   syntax off <Bar>
    \ else <Bar>
    \   syntax enable <Bar>
    \ endif <CR>

As an alternative, you can toggle syntax state for each buffer individually,

syntax manual
map <F12> <cmd>let &syntax = exists('b:current_syntax') ? 'OFF' : 'ON'<CR>

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.