混合制表符和空格而不混合制表符和空格
我是一名 Vim 用户(由于我的 Python 背景)已经习惯了空格而不是制表符。我正在与另一位使用 Windows IDE 并且非常想使用选项卡的开发人员一起开发 PHP 项目。似乎确实没有任何硬性且快速的 PHP 风格指南更喜欢特定的风格,所以我有点需要处理选项卡。
有什么方法可以通过 Git 或 Vim 处理选项卡式代码并在编辑时将其转换为空格吗?那么也许在 git add/commit 上它可以转换回选项卡?
I'm a Vim user who (due to my Python background) has grown accustomed to spaces instead of tabs. I'm working on a PHP project with another developer who uses Windows IDEs and really wants to use tabs. There really doesn't seem to be any hard and fast style guide for PHP that prefers a specific style, so I'm kind of stuck needing to deal with the tabs.
Is there some way, either through Git or Vim, that I can work with tabbed code and convert it to spaces while I'm editing it? Then perhaps on git add/commit it could be converted back to tabs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
真正重要的一件事是跟踪内容标准化。您和其他开发人员必须就某些事情达成一致。无论你们中的谁想做一些超出商定标准的事情,最终都可能会得到好坏参半的结果。干净地来回转换并不总是可能的。
我真的建议只处理它。我对制表符和空格有自己的看法,并且我已经使用它们编写了代码。最好的办法就是设置您的编辑器以匹配标准化样式,并遵循它。在vim中,关闭expandtab,将tabstop设置为你喜欢的。
如果您仍然想尝试,有两种主要方法:
使用 Vim 中的自动命令进行读/写转换。您可能需要
BufReadPost
、BufWritePre
和BufWritePost
。 (对于编写,您转换为标准,让 Vim 编写它,然后转换回您喜欢的编辑方式。)确保按您喜欢的方式设置 tabstop,然后是这样的(未经测试):*
是将其应用到的文件模式;您可能不得不搞乱它,或者只为某个目录中的文件添加自动命令,以确保您编辑的所有内容都不会发生这种情况。请注意,这很危险;例如,它将替换字符串中的文字制表符。man gitattributes
或 Pro Git。您可以使用它们进行转换以进行编辑,然后返回到标准进行提交。如果从来没有任何奇怪的缩进,那么可能很简单,只需将前导制表符更改为一定数量的空格,并将前导空格更改为制表符数量的一小部分。使用 sed/perl/indent 来完成,无论你喜欢什么。The one really important thing is that the tracked content be standardized. You and the other developer are just going to have to agree on something there. Whichever of you wants to do something besides the agreed-upon standard is may end up with mixed results. It's not always possible to cleanly convert back and forth.
I would really recommend just dealing with it. I have my own opinion about tabs and spaces, and I've worked on code using each. The best thing to do is just to set up your editor to match the standardized style, and go with it. In vim, turn off expandtab, set tabstop to what you like.
If you still want to try, there are two primary ways:
Use autocommands in Vim to convert on read/write. You'd probably need
BufReadPost
,BufWritePre
, andBufWritePost
. (For writing, you convert to the standard, let Vim write it, then convert back to the way you like to edit.) Make sure tabstop is set the way you like, then something like this (untested):The
*
is the filepattern that this will apply to; you may have to mess with that, or only add the autocommands for files within a certain directory, to make sure this doesn't happen for everything you edit. Note that this is dangerous; it will for example replace literal tab characters inside strings.man gitattributes
, or in Pro Git. You could use those to convert for editing, then back to the standard for committing. If there's never any weird indentation, it could be as simple as changing leading tabs to some number of spaces, and leading spaces to a fraction of that number of tabs. Do it with sed/perl/indent, whatever you're comfortable with.