本文共 2425 字,大约阅读时间需要 8 分钟。
我如何制作 - 从不使用制表符(将空格转换为制表符,不好!),制作Tab键== 4个空格,并在像这样的大括号块之后自动缩进代码?
另外,如何保存这些设置,以便我再也不必输入它们?
我已经看到了与此相关的其他问题,但它似乎总是与我想要的有点不同。
要在大多数文件中使用4个空格的选项卡,在Makefile中使用真正的8-wide tab char,并在包括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 noexpandtabendif" 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.
建议的方法是使用基于文件类型的缩进,并且只使用smartindent和cindent,如果这还不够。
将以下内容添加到.vimrc中
set expandtabset shiftwidth=2set softtabstop=2filetype plugin indent on
希望它有助于作为一个不同的答案。
正如下面的几个答案所指出的,现在首选的方法不是使用smartindent,而是使用以下(在你的 ):
filetype plugin indent on" show existing tab with 4 spaces widthset tabstop=4" when indenting with '>', use 4 spaces widthset shiftwidth=4" On pressing tab, insert 4 spacesset expandtab
set smartindent set tabstop=4 set shiftwidth=4 set expandtab
:help smartindent
:help auto-setting
:set all
:help C-indenting
自动缩进基于当前语法模式。 我知道如果你正在编辑Foo.java,那么输入一个{
并按下Enter键缩进以下行。
至于标签,有两种设置。 在Vim中,键入冒号然后“设置tabstop = 4”,这将设置选项卡显示为四个空格。 再次点击冒号并输入“set expandtab”,它将为制表符插入空格。
您可以将这些设置放在主目录中的.vimrc(或Windows上的_vimrc)中,这样您只需键入一次即可。
在许多Linux系统上,如Ubuntu,默认情况下.vimrc
文件不存在,因此建议您先创建它。
请勿使用主目录中存在的.viminfo
文件。 它用于不同的目的。
第1步:转到您的主目录
cd ~
第2步:创建文件
vim .vimrc
第3步:添加上述配置
filetype plugin indent onset tabstop=4set shiftwidth=4set expandtab
第3步:按Shift + ZZ保存文件。
相关的,如果你打开一个同时使用制表符和空格的文件,假设你已经有了
set expandtab ts=4 sw=4 ai
您可以使用整个文件中的空格替换所有选项卡
:%retab
编辑你的〜/ .vimrc
$ vim ~/.vimrc
添加以下行:
set tabstop=4set shiftwidth=4set softtabstop=4set expandtab
来自 :
:set tabstop=4:set shiftwidth=4:set expandtab
获取特定于文件类型的缩进的最佳方法是在vimrc中使用filetype plugin indent on
。 然后,您可以在.vim / ftplugin / c.vim中指定set sw=4 sts=4 et
,而不必为正在编辑的所有文件创建全局文件,其他非C类型语法将正确缩进,太(甚至是lisps) 。
转载地址:http://rtogj.baihongyu.com/