Reputation: 15734
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.
Upvotes: 46
Views: 24629
Reputation: 4947
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 make sure filetype plugin and detection is turned on by running :filetype
in vim. It should be on by default but if not, put the line filetype plugin on
etc. in your vimrc. Then create the folder ftplugin
in your vim folder (on unix it's ~/.vim/
, on windows it's C:\Users\{myUsername}\vimfiles
). 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
, and filetype detection on
to your vimrc if needed (usually on by default, can check with :filetype
in vim)If you're having trouble with a specific filetype, you can run :set filetype?
in a file of the type you're trying to configure, to see if vim is interpreting the type of file correctly.
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.
Upvotes: 23
Reputation: 12413
Use BufWinEnter
instead of WinEnter
, like this:
autocmd BufWinEnter,FileType javascript colorscheme jellybeans
Upvotes: 3
Reputation: 65951
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.)
Upvotes: 37
Reputation: 317
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
Upvotes: 4