问题描述
有人可以简单地向我解释根据文件类型更改 Vim 缩进行为的最简单方法吗?例如,如果我打开一个 Python 文件,它应该缩进 2 个空格,但如果我打开一个 Powershell 脚本,它应该使用 4 个空格.
Could someone explain to me in simple terms the easiest way to change the indentation behavior of Vim based on the file type? For instance, if I open a Python file it should indent with 2 spaces, but if I open a Powershell script it should use 4 spaces.
推荐答案
您可以添加 .vim
文件,以便在 vim 切换到特定文件类型时执行.
You can add .vim
files to be executed whenever vim switches to a particular filetype.
例如,我有一个包含以下内容的文件 ~/.vim/after/ftplugin/html.vim
:
For example, I have a file ~/.vim/after/ftplugin/html.vim
with this contents:
setlocal shiftwidth=2
setlocal tabstop=2
这导致 vim 使用宽度为 2 个字符的制表符进行缩进(noexpandtab
选项在我的配置中的其他地方全局设置).
Which causes vim to use tabs with a width of 2 characters for indenting (the noexpandtab
option is set globally elsewhere in my configuration).
此处描述:http://vimdoc.sourceforge.net/htmldoc/usr_05.html#05.4,向下滚动到文件类型插件部分.
This is described here: http://vimdoc.sourceforge.net/htmldoc/usr_05.html#05.4, scroll down to the section on filetype plugins.
这篇关于按文件类型更改 Vim 缩进行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!