如何在 Vim 中根据文件内容而不是扩展名设置 aa 文件的文件类型?
即使文件没有扩展名,Vim 似乎也有能力读取 shebang 并推断文件类型。如何定义要在文件中查找的任意标志并更改文件类型。例如,如果文件以 开头,/** @flow */则将文件类型设置为javascript.flow.
" 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)