谁能简单地给我解释一下,根据文件类型改变Vim缩进行为的最简单方法是什么?例如,如果我打开一个Python文件,它应该缩进2个空格,但如果我打开一个Powershell脚本,它应该使用4个空格。
当前回答
编辑~/。Vimrc,并为不同的缩进添加不同的文件类型,例如。我想html/rb缩进2个空间,和js/coffee文件缩进4个空间:
" by default, the indent is 2 spaces.
set shiftwidth=2
set softtabstop=2
set tabstop=2
" for html/rb files, 2 spaces
autocmd Filetype html setlocal ts=2 sw=2 expandtab
autocmd Filetype ruby setlocal ts=2 sw=2 expandtab
" for js/coffee/jade files, 4 spaces
autocmd Filetype javascript setlocal ts=4 sw=4 sts=0 expandtab
autocmd Filetype coffeescript setlocal ts=4 sw=4 sts=0 expandtab
autocmd Filetype jade setlocal ts=4 sw=4 sts=0 expandtab
参考:根据文件类型设置Vim空白参数
其他回答
根据~/.vimrc中的文件后缀放置autocmd命令
autocmd BufRead,BufNewFile *.c,*.h,*.java set noic cin noexpandtab
autocmd BufRead,BufNewFile *.pl syntax on
您要查找的命令可能是ts=和sw=
我通常使用expandtab set,但这对makefile不好。我最近补充道:
:autocmd FileType make set noexpandtab
到。vimrc文件的末尾,它可以识别Makefile, Makefile和*。Mk作为makefile文件,不展开制表符。可以推测,您可以扩展它。
编辑~/。Vimrc,并为不同的缩进添加不同的文件类型,例如。我想html/rb缩进2个空间,和js/coffee文件缩进4个空间:
" by default, the indent is 2 spaces.
set shiftwidth=2
set softtabstop=2
set tabstop=2
" for html/rb files, 2 spaces
autocmd Filetype html setlocal ts=2 sw=2 expandtab
autocmd Filetype ruby setlocal ts=2 sw=2 expandtab
" for js/coffee/jade files, 4 spaces
autocmd Filetype javascript setlocal ts=4 sw=4 sts=0 expandtab
autocmd Filetype coffeescript setlocal ts=4 sw=4 sts=0 expandtab
autocmd Filetype jade setlocal ts=4 sw=4 sts=0 expandtab
参考:根据文件类型设置Vim空白参数
使用ftplugins或autocommands设置选项。
ftplugin
在~ /维姆- ftplugin python。维姆:
setlocal shiftwidth=2 softtabstop=2 expandtab
别忘了打开~/.vimrc:
filetype plugin indent on
(:h ftplugin获取更多信息)
autocommand
在~ / . vimrc:
autocmd FileType python setlocal shiftwidth=2 softtabstop=2 expandtab
我还建议学习tabstop和softtabstop的区别。很多人都不知道softtabstop。
虽然你可以使用缩进插件或手动使用设置来配置Vim的缩进,但我建议使用一个名为Vindect的python脚本,当你打开一个python文件时,它会自动为你设置相关设置。使用这个技巧可以使Vindect的使用更加有效。当我第一次开始编辑别人用各种缩进样式(制表符vs空格和空格数量)创建的python文件时,这令人难以置信地沮丧。但是Vindect和这个缩进文件一起
还建议:
pythonhelper python_match python_ifold
推荐文章
- 点击按钮时如何打开手机设置?
- 如何从Python包内读取(静态)文件?
- 为什么我得到“Pickle - EOFError: run out of input”读取一个空文件?
- 写字符串到文本文件,并确保它总是覆盖现有的内容。
- 从另一个文件导入变量?
- 在Python中格式化多行字典的正确方法是什么?
- 寻找不属于特定用户的文件
- 如何复制文件跨计算机使用SSH和MAC OS X终端
- 如何在node.js中移动文件?
- 在Vim/Vi中,如何将光标移动到前一个单词的末尾?
- 如何修改文本文件?
- 如何在vim中自动删除尾随空格
- 为什么Path。以Path.DirectorySeparatorChar开头的文件名合并不正确?
- git可以自动在空格和制表符之间切换吗?
- 为什么Vim使用~扩展名保存文件?