Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a different colorscheme for each file type in Vim?

Tags:

vim

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".


EDIT:

If anyone's interested, here is my .vimrc repo in github.com. I'll update it with the solution I find here once given.

like image 365
Ory Band Avatar asked Mar 02 '11 17:03

Ory Band


People also ask

How do I change the default Colorscheme in vim?

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.

How do I change the filetype in vim?

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.

How do I change colors in vim?

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.

Where do I put vim Colorscheme?

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.


3 Answers

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.)

like image 90
User Avatar answered Oct 09 '22 02:10

User


What you want are filetype plugins, rather than the autocmds. 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:

  1. Create a file named python.vim in ftplugin
  2. Create a file named python_whatever.vim in ftplugin
  3. Create a file named whatever.vim in ftplugin/python

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:

  • Create a file named filetype.vim in the ftplugin directory for every filetype you want.
  • In each of these files, add the line colorscheme name_of_colorscheme
  • Add filetype plugin on to your vimrc.
  • In order to set a default colorscheme, just set it in your vimrc file. If I remember correctly, filetype plugins are loaded after 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.

like image 32
cledoux Avatar answered Oct 09 '22 03:10

cledoux


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

like image 4
Francesc Guasch Avatar answered Oct 09 '22 02:10

Francesc Guasch