我如何使vi-Vim从来不使用制表符(将空格转换为制表符,坏!),使制表键== 4个空格,并自动缩进代码花括号块后像Emacs ?

此外,我如何保存这些设置,这样我就不必再次输入它们了?

我见过其他与此相关的问题,但它似乎总是有点偏离我想要的。


当前回答

最简单的是一个vim文件

set tabstop=4

其他回答

获得特定于文件类型的缩进的最好方法是在vimrc中使用文件类型插件缩进。然后你可以在.vim/ftplugin/c中指定set sw=4 sts=4 et。例如,vim,而不必为所有被编辑的文件设置全局,其他非c类型的语法也会正确地缩进(甚至是口齿不清)。

相关的,如果你打开一个同时使用制表符和空格的文件,假设你有

set expandtab ts=4 sw=4 ai

可以将整个文件中的所有制表符替换为空格

:%retab

来自VIM wiki:

:set tabstop=4
:set shiftwidth=4
:set expandtab

编辑~/.vimrc

$ vim ~/.vimrc

增加如下行:

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

要在大多数文件中使用4个空格的制表符,在Makefiles中使用真正的8宽制表符字符,并在包括C/ c++在内的各种文件中使用自动缩进,请将此放在~/中。vimrc文件:

" Only do this part when compiled with support for autocommands.
if has("autocmd")
    " Use filetype detection and file-based automatic indenting.
    filetype plugin indent on

    " Use actual tab chars in Makefiles.
    autocmd FileType make set tabstop=8 shiftwidth=8 softtabstop=0 noexpandtab
endif

" For everything else, use a tab width of 4 space chars.
set tabstop=4       " The width of a TAB is set to 4.
                    " Still it is a \t. It is just that
                    " Vim will interpret it to be having
                    " a width of 4.
set shiftwidth=4    " Indents will have a width of 4.
set softtabstop=4   " Sets the number of columns for a TAB.
set expandtab       " Expand TABs to spaces.