In Vim, I want to use a different colorscheme for each file type.
e.g. I want to use desert256
colorscheme for Python & JavaScript files, and use jellybeans
colorscheme for HTML & CSS files.
I've tried putting the following code in my .vimrc
, but the colorscheme change happens only when changing buffers for the first time.
i.e. If I open a new Python file, Python's colorscheme is used, and when I open a new CSS *buffer*, indeed the colorscheme changes to CSS's colorscheme. However, Changing back to Python's buffer does not change the colorscheme back.
I've used autocmd WinEnter
to try and make this rule happen when changing windows (and buffers), but it doesn't help:
autocmd WinEnter,FileType python,javascript colorscheme desert256
autocmd WinEnter,FileType *,html,css colorscheme jellybeans " This includes default filetype colorscheme.
How can I fix this? In addition, a bonus would be to not change a colorscheme when not needed - i.e. Changing from a Python to a JavaScript buffer won't change the colorscheme to "itself".
If anyone's interested, here is my .vimrc
repo in github.com. I'll update it with the solution I find here once given.
You can change color schemes at anytime in vi by typing colorscheme followed by a space and the name of the color scheme. For more color schemes, you can browse this library on the vim website. You can enable or disable colors by simply typing "syntax on" or "syntax off" in vi.
As other answers have mentioned, you can use the vim set command to set syntax. :set syntax=<type> where <type> is something like perl , html , php , etc. There is another mechanism that can be used to control syntax highlighting called filetype , or ft for short.
After typing the command, press “Tab”. This will open a list of all the available color schemes. If you keep pressing “Tab”, Vim will cycle through all of them.
View preinstalled color schemes Some color schemes are already installed on your system. You can find those color schemes in the /usr/share/vim/vim*/colors directory as . vim extension.
I've been looking for the same thing. This inside your .vimrc works reasonably well although not perfect.
autocmd BufEnter * colorscheme default
autocmd BufEnter *.php colorscheme Tomorrow-Night
autocmd BufEnter *.py colorscheme Tomorrow
(Note if you're looking for a good dark color theme Tomorrow-Night looks pretty good. Very similar to theme used on Code Academy.)
What you want are filetype plugins, rather than the autocmd
s. Run help: ftplugin
in vim for more info.
From the vim help page:
A filetype plugin is like a global plugin, except that it sets options and defines mappings for the current buffer only.
In order to use filetype plugins, first put the line filetype plugin on
in your vimrc. Then create the folder ftplugin
in your vim folder (on unix it's ~/.vim/
, I'm not familiar with windows). Then create a script file for each file type you want to customize. These files must be named a specific way. From the vim help page:
The generic names for the filetype plugins are:
ftplugin/filetype.vim
ftplugin/filetype_name.vim
ftplugin/filetype/name.vim
So, for example, if I wanted to create a script for a python file, I would have three options:
This script will then be loaded anytime I open a file that vim recognizes as a python file.
So, in order to accomplish what you want:
colorscheme name_of_colorscheme
filetype plugin on
to your vimrc.Edit: The OP indicated that he had a good reason to avoid using the ftplugin directory. After a bit more diggin, I found this script. It can be placed in the global vimrc and seems intended to solve the same problem as the OP.
I have a hack you may like. It is far from perfect, and it doesn't use a .vimrc, but it works for me. It requires you to type a different command to edit different files. It works using the -c parameter when you call gvim. This argument allows you to run vim commands after loading the file. Add this to your ~/.bashrc ( I guess you are using bash ) :
alias gpy="gvim -c 'colorscheme desert'"
alias gcs="gvim -c 'colorscheme jellybeans'"
Hope this helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With