Tab键== 4个空格并在Vim中的花括号后自动缩进

mmc*_*ole 1224 vi vim whitespace indentation auto-indent

我如何制作vi - Vim从不使用制表符(将空格转换为制表符,不好!),制作Tab键== 4个空格,并在像Emacs这样的大括号块之后自动缩进代码?

另外,如何保存这些设置,以便我再也不必输入它们?

我已经看到了与此相关的其他问题,但它似乎总是与我想要的有点不同.

Ken*_*Ken 1819

正如下面的几个答案中所指出的,现在首选的方法不是使用smartindent,而是使用以下(在你的.vimrc中):

filetype plugin indent on
" show existing tab with 4 spaces width
set tabstop=4
" when indenting with '>', use 4 spaces width
set shiftwidth=4
" On pressing tab, insert 4 spaces
set expandtab
Run Code Online (Sandbox Code Playgroud)

.vimrc:文件中:

set smartindent
set tabstop=4
set shiftwidth=4
set expandtab
Run Code Online (Sandbox Code Playgroud)

帮助文件需要一些时间来习惯,但是你读的越多,Vim就越好:

:help smartindent
Run Code Online (Sandbox Code Playgroud)

更好的是,您可以在源代码中嵌入这些设置以实现可移植性:

:help auto-setting
Run Code Online (Sandbox Code Playgroud)

要查看您当前的设置:

:set all
Run Code Online (Sandbox Code Playgroud)

作为graywh在评论中指出,smartindent已取代cindent其中"作品更巧妙",但主要还是与类似C的语法的语言:

:help C-indenting
Run Code Online (Sandbox Code Playgroud)

  • 抱歉,smartindent被cindent取代,cindent本身仅适用于C风格的语法.在使用其他语言时,在vimrc中启用可能是一个问题.只需使用"filetype indent on". (60认同)
  • 如果你有expandtab设置,那么它应该使用空格.你还"设置兼容"吗?这有各种副作用,包括将expandtab重置为默认值"off" (4认同)
  • 如果我启用expandtab,有没有办法在文本中实际输入制表符? (4认同)
  • 好吧,smartindent也*仅用于C风格的语法,基本上已被弃用. (2认同)
  • Ken:您应该更新您的答案,请参阅更新的http://stackoverflow.com/a/23426067/2987828. (2认同)
  • 我注意到当我执行`backspace`时,wim一次只会删除一个空格,这很烦人。有什么办法吗? (2认同)
  • @DanieleSegato <ctrl v> <tab>应该在插入模式下工作:http://stackoverflow.com/questions/4781070/how-to-insert-tab-character-when-expandtab-option-is-on-in-vim (2认同)

net*_*eff 232

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

set expandtab ts=4 sw=4 ai
Run Code Online (Sandbox Code Playgroud)

您可以使用整个文件中的空格替换所有选项卡

:%retab
Run Code Online (Sandbox Code Playgroud)

  • @ Rob-Wells:我将"空白"改为"空格".你现在高兴了?;-) (11认同)
  • 仅供参考,如果您不希望用空格替换标签,请删除expandtab行. (9认同)
  • 标签是不是空白?;-) (6认同)
  • expandtab确定选项卡是否扩展为空格.ts = tabstop =文件中<Tab>所依据的空格数.sw = shiftwidth =(自动)缩进的每个步骤使用的空格数.ai = autoindent =开始换行时从当前行复制缩进. (2认同)

gra*_*ywh 87

获取特定于文件类型的缩进的最佳方法是filetype plugin indent on在vimrc中使用.然后你可以指定像set sw=4 sts=4 et.vim/ftplugin/c.vim 这样的东西,例如,不必为正在编辑的所有文件创建全局文件,其他非C类型语法也将正确缩进(甚至是lisps).

  • 恕我直言,比标记正确的答案更好.文件类型缩进取代了cindent和smartindent. (9认同)

She*_*ami 57

要在大多数文件中使用4个空格的选项卡,在Makefile中使用真正的8宽Tab键,并在包括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.
Run Code Online (Sandbox Code Playgroud)

  • 嗨@lucidbrot,"autocmd FileType make"语句基本上告诉vim在打开Makefile时要使用的一些设置.而它下面的行是设置默认值.换句话说,"tabstop = 8 ..."设置将在以后打开文件时应用,并将覆盖适用于初始化的"tabstop = 4 ..."设置. (5认同)
  • +1使其可扩展。我选择这个,因为关于每个部分的作用的评论允许我完全按照我的方式设置事情(不会破坏任何东西),因为我知道一切都在做什么。万岁! (2认同)

Eri*_*ick 54

在许多Linux系统上,如Ubuntu,.vimrc默认情况下该文件不存在,因此建议您先创建它.

不要使用.viminfo主目录中存在的文件.它用于不同的目的.

第1步:转到您的主目录

cd ~

第2步:创建文件

vim .vimrc

第3步:添加上述配置

filetype plugin indent on
set tabstop=4
set shiftwidth=4
set expandtab
Run Code Online (Sandbox Code Playgroud)

第3步:按Shift+ 保存文件ZZ.

  • 不要使用`Shift + ZZ`它会创建.swp文件.用`wq`. (11认同)
  • @shas:["ZZ"相当于":x"](http://vimdoc.sourceforge.net/htmldoc/editing.html#ZZ),它与":wq"相同,只是它只保存如果文件已被更改...(`Ctrl-Z`是另一个故事......) (5认同)
  • @shas 这根本不真实。如果您不需要编辑会话的交换文件,则必须运行“vim -n &lt;file&gt;”。交换文件不是在保存时生成的;而是在保存时生成的。这几乎会破坏它的目的,因为交换文件在恢复突然终止的编辑会话中变得有用。 (2认同)

Cha*_*aid 31

建议的方法是使用基于文件类型的缩进,并且只使用smartindent和cindent,如果这还不够.

将以下内容添加到.vimrc中

set expandtab
set shiftwidth=2
set softtabstop=2
filetype plugin indent on
Run Code Online (Sandbox Code Playgroud)

希望它有助于作为一个不同的答案.


Yus*_*him 11

编辑你的〜/ .vimrc

$ vim ~/.vimrc
Run Code Online (Sandbox Code Playgroud)

添加以下行:

set tabstop=4
set shiftwidth=4
set softtabstop=4
set expandtab
Run Code Online (Sandbox Code Playgroud)


Use*_*ser 10

来自VIM维基:

:set tabstop=4
:set shiftwidth=4
:set expandtab
Run Code Online (Sandbox Code Playgroud)


Joe*_*son 6

自动缩进基于当前语法模式.我知道如果你正在编辑Foo.java,那么输入一个{并点击Enter下一行的缩进.

至于标签,有两种设置.在Vim中,键入冒号然后"设置tabstop = 4",这将设置选项卡显示为四个空格.再次点击冒号并输入"set expandtab",它将为制表符插入空格.

您可以将这些设置放在主目录中的.vimrc(或Windows上的_vimrc)中,这样您只需键入一次即可.