Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable and disable a Vim syntax script?

Tags:

css

vim

plugins

I'm using the css_color.vim plugin to highlight CSS hexadecimal color codes with the actual color. It's a bit distracting to have the highlighting turned on all the time so I am wondering if there is any way to toggle a syntax plugin on and off.

like image 361
bab Avatar asked Jan 21 '13 03:01

bab


People also ask

How do I enable syntax highlighting in vim Mac?

If you want to toggle this on/off (without creating a . vimrc file) simply type :syntax on while in vi/vim. Save this answer.

What is vim syntax?

VIM is an alternative and advanced version of VI editor that enables Syntax highlighting feature in VI. Syntax highlighting means it can show some parts of text in another fonts and colors. VIM doesn't show whole file but have some limitations in highlighting particular keywords or text matching a pattern in a file.

Where is vim syntax files?

The runtimepath option contains a list of paths, separated by commas, where vim looks for plugins, syntax files, etc. On unix, the first path is $HOME/. vim/ (aka ~/. vim/ ), which means that Vim looks for syntax files in your home folder first before it looks anywhere else.


2 Answers

Create new syntax instead.

Put the file in .vim/syntax/css_color.vim and add runtime syntax/css.vim at the top. Now it acts like its own syntax definition that happens to inherit the base CSS syntax.

When you want CSS with smart colors, use :setf css_color. When you want to back to stock CSS, :setf css.


edit: To bind a key:

function! ToggleCssColor()
    if &filetype == 'css'
        setf css_color
    else
        setf css
    endif
endfunction

noremap <Leader>c :call ToggleCssColor()<CR>

Then \c will switch between the two styles in normal mode. You can set the key to whatever you want, use inoremap to bind a key in insert mode, etc.

like image 109
Eevee Avatar answered Oct 11 '22 15:10

Eevee


Have a look at alternative plugins. I use colorizer - Highlight #rrggbb or #rgb color, because it has advanced features, works not just for CSS, and can be turned on/off easily.

like image 31
Ingo Karkat Avatar answered Oct 11 '22 16:10

Ingo Karkat