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

后端 未结 4 1259
情书的邮戳
情书的邮戳 2020-12-23 14:28

In Vim, I want to use a different colorscheme for each file type.

e.g. I want to use desert256 colorscheme for Py

相关标签:
4条回答
  • 2020-12-23 14:30

    Use BufWinEnter instead of WinEnter, like this:

    autocmd BufWinEnter,FileType javascript colorscheme jellybeans
    
    0 讨论(0)
  • 2020-12-23 14:32

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

    0 讨论(0)
  • 2020-12-23 14:43

    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.

    0 讨论(0)
  • 2020-12-23 14:47

    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

    0 讨论(0)
提交回复
热议问题