Vim:根据文件内容设置文件类型

che*_*eak 2 vim

如何在 Vim 中根据文件内容而不是扩展名设置 aa 文件的文件类型?

即使文件没有扩展名,Vim 似乎也有能力读取 shebang 并推断文件类型。如何定义要在文件中查找的任意标志并更改文件类型。例如,如果文件以 开头,/** @flow */则将文件类型设置为javascript.flow.

jer*_*ile 7

请参阅此处此处了解更多信息,但这里有一些选项:

" Search entire file
au BufRead * if search('mypattern', 'nw') | setlocal ft=myfiletype | endif

" Search first line only
au BufRead * if getline(1) =~ 'mypattern' | setlocal ft=myfiletype | endif
Run Code Online (Sandbox Code Playgroud)