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

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

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

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


#1楼

要在大多数文件中使用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 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.

#2楼

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

将以下内容添加到.vimrc中

set expandtab
set shiftwidth=2
set softtabstop=2
filetype plugin indent on

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


#3楼

正如下面的几个答案所指出的,现在首选的方法不是使用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

.vimrc:文件中:

 
 
 
 
  
  
  set smartindent set tabstop=4 set shiftwidth=4 set expandtab
 
 
  

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

 
 
 
 
  
  
  :help smartindent
 
 
  

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

 
 
 
 
  
  
  :help auto-setting
 
 
  

要查看您当前的设置:

 
 
 
 
  
  
  :set all
 
 
  

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

 
 
 
 
  
  
  :help C-indenting
 
 
  


#4楼

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

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

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


#5楼

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

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

第1步:转到您的主目录

cd ~

第2步:创建文件

vim .vimrc

第3步:添加上述配置

filetype plugin indent on
set tabstop=4
set shiftwidth=4
set expandtab

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


#6楼

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

set expandtab ts=4 sw=4 ai

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

:%retab

#7楼

编辑你的〜/ .vimrc

$ vim ~/.vimrc

添加以下行:

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

#8楼

来自VIM维基

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

#9楼

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值