Genschman
Genschman

Reputation: 187

How to switch syntax on and off in vim with one key

How can i switch syntax highlighting on and off in vim with one key ( for instance <F5>) in command mode and/or in insert mode at any time?

Upvotes: 2

Views: 1410

Answers (1)

drRobertz
drRobertz

Reputation: 3638

From help g:syntax_on:

You can toggle the syntax on/off with this command:

   :if exists("g:syntax_on") | syntax off | else | syntax enable | endif

To put this into a mapping, you can use:

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

[using the <> notation, type this literally]

To get it in insert mode, the simplest way, I think, is to do

imap <F7> <C-o>F7

In my .vimrc i have the same, but with nnoremap and <silent>:

nnoremap <silent> <Leader>ts
             \ : if exists("syntax_on") <BAR>
             \    syntax off <BAR>
             \ else <BAR>  
             \    syntax enable <BAR>
             \ endif<CR>   

Upvotes: 8

Related Questions