【笔记】Vim设置Tab为4个空格

前言

Vim设置Tab为4个空格

修改配置文件

tabstopts:tab键实际上是由几个空格组成,默认值为8
softtabstopsts:删除键实际上会删除几个空格
shiftwidthsw:实际展示出的tab长度,通常与softtabstop值相同
expandtab:缩进用空格表示,不与noexpandtab连用
noexpandtab:缩进用制表符表示,不与expandtab连用

~/.vimrc
1
2
3
4
set tabstop=4
set softtabstop=4
set shiftwidth=4
set expandtab

根据文件类型指定不同配置

1
2
3
4
5
6
if has("autocmd")
autocmd FileType html setlocal ts=2 sts=2 sw=2 expandtab
autocmd FileType css setlocal ts=2 sts=2 sw=2 expandtab
autocmd FileType javascript setlocal ts=2 sts=2 sw=2 expandtab
autocmd FileType markdown setlocal ts=2 sts=2 sw=2 expandtab
endif

混合设置

  • 我目前使用的设置
  • 先设置个例,再定义其他所有类型文件的设置
1
2
3
4
5
6
7
8
9
10
11
if has("autocmd")
autocmd FileType html setlocal ts=2 sts=2 sw=2 expandtab
autocmd FileType css setlocal ts=2 sts=2 sw=2 expandtab
autocmd FileType javascript setlocal ts=2 sts=2 sw=2 expandtab
autocmd FileType markdown setlocal ts=2 sts=2 sw=2 expandtab
endif

set tabstop=4
set softtabstop=4
set shiftwidth=4
set expandtab

完成

参考文献

博客园——SteelWolf