EBG*_*een 364 vim settings file indentation
有人可以用简单的语言向我解释根据文件类型更改Vim的缩进行为的最简单方法吗?例如,如果我打开一个Python文件,它应该缩进2个空格,但如果我打开一个Powershell脚本,它应该使用4个空格.
Spo*_*ser 289
.vim只要vim切换到特定文件类型,您就可以添加要执行的文件.
例如,我有一个~/.vim/after/ftplugin/html.vim 包含此内容的文件:
setlocal shiftwidth=2
setlocal tabstop=2
Run Code Online (Sandbox Code Playgroud)
这导致vim使用宽度为2个字符的noexpandtab选项卡进行缩进(该选项在我的配置中全局设置).
这里描述:http://vimdoc.sourceforge.net/htmldoc/usr_05.html#05.4,向下滚动到filetype插件部分.
gra*_*ywh 166
使用ftplugins或autocommands设置选项.
在 ~/.vim/ftplugin/python.vim:
setlocal shiftwidth=2 softtabstop=2 expandtab
Run Code Online (Sandbox Code Playgroud)
别忘了打开它们~/.vimrc:
filetype plugin indent on
Run Code Online (Sandbox Code Playgroud)
(:h ftplugin了解更多信息)
在~/.vimrc:
autocmd FileType python setlocal shiftwidth=2 softtabstop=2 expandtab
Run Code Online (Sandbox Code Playgroud)
你可以用它们的短版本替换任何长命令或设置:
autocmd:au
setlocal:setl
shiftwidth:sw
tabstop:ts
softtabstop:sts
expandtab:et
我也建议学习之间的差异tabstop和softtabstop.很多人都不知道softtabstop.
Siw*_*申思维 80
编辑你的~/.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
Run Code Online (Sandbox Code Playgroud)
Pau*_*lin 59
根据〜/ .vimrc中的文件后缀放置autocmd命令
autocmd BufRead,BufNewFile *.c,*.h,*.java set noic cin noexpandtab
autocmd BufRead,BufNewFile *.pl syntax on
Run Code Online (Sandbox Code Playgroud)
您正在寻找的命令可能是ts =和sw =
Jon*_*ler 23
我通常使用expandtabset,但这对makefile来说很糟糕.我最近补充说:
:autocmd FileType make set noexpandtab
Run Code Online (Sandbox Code Playgroud)
到.vimrc文件的末尾,它将Makefile,makefile和*.mk识别为makefile,并且不会扩展选项卡.据推测,你可以扩展它.
小智 15
我总是对那些为不同文件类型更改选项卡大小的人感到惊讶.当您使用较少的文件查看文件时,您到底做了什么?
就个人而言,我在.vimrc中使用这些设置:
autocmd FileType python set tabstop=8|set shiftwidth=2|set expandtab
autocmd FileType ruby set tabstop=8|set shiftwidth=2|set expandtab
Run Code Online (Sandbox Code Playgroud)
这可能是我们大多数人都知道的,但无论如何(我第一次感到困惑):执行:set et(:setexpandtabs)不会更改文件中已存在的选项卡,而是必须这样做:retab.例如:
:set et
:retab
Run Code Online (Sandbox Code Playgroud)
并且文件中的选项卡被足够的空格替换.要让标签返回,只需:
:set noet
:retab
Run Code Online (Sandbox Code Playgroud)
今天,你可以尝试editorconfig,还有一个vim 插件。这样,您不仅可以更改 vim 中的缩进大小,而且可以在许多其他编辑器中保持一致的编码风格。
下面是一个简单的editorconfig,您可以看到,python 文件将有 4 个空格用于缩进,而 pug 模板文件将只有 2 个。
# 4 space indentation for python files
[*.py]
indent_style = space
indent_size = 4
# 2 space indentation for pug templates
[*.pug]
indent_size = 2
Run Code Online (Sandbox Code Playgroud)
小智 5
对于使用 的用户autocmd,最佳做法是将它们组合在一起。如果分组与文件类型检测有关,您可能会遇到以下情况:
augroup filetype_c
autocmd!
:autocmd FileType c setlocal tabstop=2 shiftwidth=2 softtabstop=2 expandtab
:autocmd FileType c nnoremap <buffer> <localleader>c I/*<space><esc><s-a><space>*/<esc>
augroup end
Run Code Online (Sandbox Code Playgroud)
分组有助于保持.vimrc有条理,尤其是当文件类型具有多个关联规则时。在上面的示例中,定义了特定于 .c 文件的注释快捷方式。
对 的初始调用autocmd!告诉 vim 删除所述分组中任何先前定义的自动命令。如果.vimrc再次来源,这将防止重复定义。查看:help augroup更多信息。
在 Lua(对于 Neovim 用户)中,您可以使用RUNTIMEPATH/ftplugin/*yourfiletype*.lua以下选项:
vim.opt_local.shiftwidth = 2
vim.opt_local.tabstop = 2
Run Code Online (Sandbox Code Playgroud)
请务必在引号中使用字符串值。例如:
vim.opt_local.foldmethod = 'marker'
Run Code Online (Sandbox Code Playgroud)