按文件类型更改Vim缩进行为

有人可以简单地向我解释根据文件类型更改Vim缩进行为的最简单方法吗?例如,如果我打开Python文件,则应缩进2个空格,但是如果我打开Powershell脚本,则应缩进4个空格。



斯蒂芬大帝
浏览 851回答 3
3回答

慕虎7371278

使用ftplugins或自动命令来设置选项。ftplugin在 ~/.vim/ftplugin/python.vim:setlocal shiftwidth=2 softtabstop=2 expandtab并且不要忘记将它们打开~/.vimrc:filetype plugin indent on(:h ftplugin有关更多信息)自动命令在~/.vimrc:autocmd FileType python setlocal shiftwidth=2 softtabstop=2 expandtab您可以代替任何的长命令或设置及其短版本:autocmd:ausetlocal:setlshiftwidth:swtabstop:tssofttabstop:stsexpandtab:et我也建议学习之间的差异tabstop和softtabstop。很多人不知道softtabstop。

弑天下

编辑~/.vimrc,并为不同的缩进添加不同的文件类型,例如,我希望html/rb缩进2个空格,js/coffee文件缩进4个空格:" by default, the indent is 2 spaces. set shiftwidth=2set softtabstop=2set tabstop=2" for html/rb files, 2 spacesautocmd Filetype html setlocal ts=2 sw=2 expandtabautocmd Filetype ruby setlocal ts=2 sw=2 expandtab" for js/coffee/jade files, 4 spacesautocmd Filetype javascript setlocal ts=4 sw=4 sts=0 expandtabautocmd Filetype coffeescript setlocal ts=4 sw=4 sts=0 expandtabautocmd Filetype jade setlocal ts=4 sw=4 sts=0 expandtab请参考:按文件类型设置Vim空格首选项
打开App,查看更多内容
随时随地看视频慕课网APP