博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Tab键== 4个空格并在Vim中的花括号后自动缩进
阅读量:3580 次
发布时间:2019-05-20

本文共 2425 字,大约阅读时间需要 8 分钟。

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

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

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


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

#2楼

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

将以下内容添加到.vimrc中

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

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


#3楼

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

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

:help smartindent

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

:help auto-setting

要查看您当前的设置:

:set all

作为 在评论中指出,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 onset tabstop=4set shiftwidth=4set expandtab

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


#6楼

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

set expandtab ts=4 sw=4 ai

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

:%retab

#7楼

编辑你的〜/ .vimrc

$ vim ~/.vimrc

添加以下行:

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

#8楼

来自 :

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

转载地址:http://rtogj.baihongyu.com/

你可能感兴趣的文章
学习日记04
查看>>
js自定义数据顺序进行升序或者降序排序
查看>>
【零】简单数仓框架优化、配置及基准测试
查看>>
Sqoop的安装及测试
查看>>
Kylin的简单使用
查看>>
Presto的概念和安装使用
查看>>
Druid的Web页面使用
查看>>
Scala-HelloWorld
查看>>
Scala-IDEA中环境部署
查看>>
Scala-HelloWorld解析
查看>>
Scala-变量和数据类型
查看>>
Scala-流程控制
查看>>
iOS蓝牙原生封装,助力智能硬件开发
查看>>
iOS 代码的Taste(品位)
查看>>
iOS开发代码规范
查看>>
iOS组件化实践(基于CocoaPods)
查看>>
数据结构之栈
查看>>
Elastic Stack简介
查看>>
关于deepin系统安装design compiler的问题解答
查看>>
hadoop3.0+spark2.0两台云服务器集群环境配置。
查看>>