简体   繁体   English

处理Vim中的代码缩进?

[英]Dealing with code indentation in Vim?

I work on an engineering team of 4 people, mostly writing javascript while occasionally dabbling in ruby and python. 我在一个由4人组成的工程团队工作,主要是编写javascript,偶尔也会涉足ruby和python。 So, as a team, we share code all the time, and as most programmers do, each member of the team has his favorite level of indentation & related settings. 因此,作为一个团队,我们一直在共享代码,而且正如大多数程序员所做的那样,团队中的每个成员都有他最喜欢的缩进级别和相关设置。 I am one of 2 members of the team who use and love Vim as my primary code editor. 我是团队的两名成员之一,他们使用并喜欢Vim作为我的主要代码编辑器。 I love my team but I also love my indentation , which happens to use 4-space tab characters. 我喜欢我的团队,但我也喜欢我的缩进 ,这恰好使用了4个空格的制表符。 For more context, here's what I use in my .vimrc: 有关更多上下文,这是我在.vimrc中使用的内容:

set ts = 4 sts = 4 sw = 4 expandtab " 4 space tabs

With so much code-sharing and collaborative editing going on in the team, the main code files usually start to appear as a mass of mixed tab & space mayhem, so that even the classic Vim trick of selecting all and pressing = to smart indent doesn't have much effect. 随着团队中进行如此多的代码共享和协作编辑,主代码文件通常开始显示为大量混合选项卡和空间混乱,因此即使是经典的Vim技巧选择所有并按=智能缩进也不会不会有太大影响。

Anyway, my question is this: In Vim (MacVim specifically) is there a better (more reliable) way of converting a code file from messy, mixed indentation to my preferred indentation? 无论如何,我的问题是:在Vim(特别是MacVim)是否有更好的(更可靠的)方法将代码文件从凌乱的混合缩进转换为我喜欢的缩进? Whether it be a .vimrc setting or a command I enter while editing the file, I don't care. 无论是.vimrc设置还是我在编辑文件时输入的命令,我都不在乎。

Thanks for any suggestions in advance! 感谢您的任何建议!

Use :retab . 使用:retab

Having said that, I strongly suggest that you, as a team, agree on and use an indentation style when working collaboratively on a certain project. 话虽如此,我强烈建议您作为一个团队,在某个项目上协同工作时,同意并使用缩进方式。

We have the same case of using javascript and ruby in the same shop. 我们在同一个商店使用javascript和ruby的情况相同。

autocmd FileType * set tabstop=4|set shiftwidth=4
autocmd FileType ruby set tabstop=2|set shiftwidth=2
set expandtab

I find I like 4 spaces for javascript but ruby looks much better with just two spaces. 我发现我喜欢javascript的4个空格,但只有两个空格,ruby看起来好多了。

I do agree with Yaser, you need to set the standard (spaces FTW) 我同意Yaser,你需要设置标准(空间FTW)

Once you decided to get rid of all the tab chars use grep to find the files to :retab 一旦你决定摆脱所有标签字符,使用grep查找文件:retab

grep -P '\t' * -R -c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM