我目前正在使用vim,并希望每次需要自动缩进(例如在javascript中的括号之后或在Python中的冒号之后)。出于某种原因,我尝试了autoindent和smartindent,但每个新行都会给我2个制表符而不是一个。为什么会这样?我以为它只会插入一个制表符。
我的当前~/.vimrc文件内容为:
set ts=4
set autoindent
set smartindent
filetype plugin indent on
我目前正在使用vim,并希望每次需要自动缩进(例如在javascript中的括号之后或在Python中的冒号之后)。出于某种原因,我尝试了autoindent和smartindent,但每个新行都会给我2个制表符而不是一个。为什么会这样?我以为它只会插入一个制表符。
我的当前~/.vimrc文件内容为:
set ts=4
set autoindent
set smartindent
filetype plugin indent on
您还需要进行设置:
:set shiftwidth=4 softtabstop=4
tabstop仅适用于实际"\t"制表符占用的列数:
:he shiftwidth
Number of spaces to use for each step of (auto)indent. Used for
|'cindent'|, |>>|, |<<|, etc.
When zero the 'ts' value will be used.
:he softtabstop
Number of spaces that a <Tab> counts for while performing editing
operations, like inserting a <Tab> or using <BS>. It "feels" like
<Tab>s are being inserted, while in fact a mix of spaces and <Tab>s is
used.
相比之下,tabstop:
:he tabstop
Number of spaces that a <Tab> in the file counts for. Also see
|:retab| command, and 'softtabstop' option.
nmap <Leader>t2 :set expandtab tabstop=2 shiftwidth=2 softtabstop=2<CR>
nmap <Leader>t4 :set expandtab tabstop=4 shiftwidth=4 softtabstop=4<CR>
nmap <Leader>t8 :set expandtab tabstop=8 shiftwidth=8 softtabstop=4<CR>
nmap <Leader>T2 :set noexpandtab tabstop=2 softtabstop=2 shiftwidth=2<CR>
nmap <Leader>T4 :set noexpandtab tabstop=4 softtabstop=4 shiftwidth=4<CR>
nmap <Leader>T8 :set noexpandtab tabstop=8 softtabstop=8 shiftwidth=8<CR>
我在我的 .vimrc 文件中使用类似以下的配置
filetype plugin indent on
set smartindent
set autoindent
set shiftwidth=4
set expandtab
set tabstop=4
set softtabstop=4
使用expandtab命令可以将制表符替换为空格,我认为这对编程非常完美。
shiftwidth=4
- user1717828