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

后端 未结 4 1276
情书的邮戳
情书的邮戳 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: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.

提交回复
热议问题