获取当前文件类型:
let current_filetype = &filetype
echo "Current filetype: " . current_filetype
或者使用缩写形式:
let current_filetype = &ft
echo "Current filetype: " . current_filetype
根据文件类型执行不同操作:
if &filetype == 'vim'
" 在 Vim 脚本文件中执行的操作
setlocal expandtab
elseif &filetype == 'python'
" 在 Python 文件中执行的操作
setlocal tabstop=4
setlocal shiftwidth=4
endif
上述示例检查当前文件类型,并根据不同的文件类型执行不同的配置。在 Vim 脚本文件中,可能希望启用 expandtab,而在 Python 文件中,可能希望设置不同的缩进。
在文件类型自动命令中使用:
augroup FileTypeSpecific
autocmd!
autocmd FileType vim setlocal expandtab
autocmd FileType python setlocal tabstop=4 | setlocal shiftwidth=4
augroup END
上述示例使用 autocmd 命令在不同的文件类型下设置不同的配置。这样,每次打开对应文件类型的文件时,自动触发相应的命令。
在 Vim 中,可以使用 :h filetype 查看有关文件类型的更多信息,并了解如何为不同的文件类型设置特定的配置。
转载请注明出处:http://www.zyzy.cn/article/detail/10395/vim编辑器