文件局部变量设置用于每种文件类型的自定义折叠

时间:2014-07-08 10:21:14

标签: scope vim

我在vimrc中有以下变量来自定义折叠标题文本:

let &foldtext = "EightHeaderFolds( '\\=s:fullwidth-3', 'left', [ repeat( '   ', v:foldlevel - 1 ), '.', '' ], '\\= s:foldlines . \" lines\"', '' )"

我想为特定文件类型(如.otl)更改此变量。我希望它成为.otl文件的以下内容:

let &foldtext = "EightHeaderFolds( '\\=s:fullwidth-3', 'left', [ repeat( '  ', v:foldlevel - 1 ), '.', '' ], '\\= s:foldlines . \" lines\"', '' )"

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:4)

来自Vim FAQ 26.1

26.1. How do I set different options for different types of files?

You can create filetype plugins to set different options for different
types of files. You should first enable filetype plugins using the command: 

    :filetype plugin on

A filetype plugin is a vim script that is loaded whenever Vim opens or
creates a file of that type.  For example, to ensure that the 'textwidth'
option is set to 80 when editing a C program (filetype 'c'), create one of
the following files: 

        ~/.vim/ftplugin/c.vim (Unix)
        %HOME%\vimfiles\ftplugin\c.vim (Windows)

with the following text in it: 

        setlocal textwidth=80

You can also use autocommands to set specific options when editing specific
type of files. For example, to set the 'textwidth' option to 75 for only
*.txt files, you can use the following autocmd: 

    autocmd BufRead *.txt setlocal textwidth=80

我赞成使用ftplugin方法,以便更容易找到并避免使用autocmds和特定于某些文件类型的函数来混淆.vimrc。查看:help filetype-plugin以获取有关如何创建它的更多详细信息。