vim中自动缩进无法正常工作?

7

我目前正在使用vim,并希望每次需要自动缩进(例如在javascript中的括号之后或在Python中的冒号之后)。出于某种原因,我尝试了autoindent和smartindent,但每个新行都会给我2个制表符而不是一个。为什么会这样?我以为它只会插入一个制表符。

我的当前~/.vimrc文件内容为:

    set ts=4
    set autoindent
    set smartindent
    filetype plugin indent on
2个回答

6

您还需要进行设置:

: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.

作为一个额外的奖励,这里有一些我设置的映射,当我需要处理那些不使用我的默认选项(将制表符扩展为具有4个空格缩进)的项目时可以使用。
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>

➕① for shiftwidth=4 - user1717828

0

我在我的 .vimrc 文件中使用类似以下的配置

filetype plugin indent on
set smartindent
set autoindent
set shiftwidth=4
set expandtab
set tabstop=4
set softtabstop=4

使用expandtab命令可以将制表符替换为空格,我认为这对编程非常完美。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接