混合制表符和空格而不混合制表符和空格

发布于 2024-12-19 09:11:02 字数 225 浏览 2 评论 0原文

我是一名 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘿嘿嘿 2024-12-26 09:11:02

真正重要的一件事是跟踪内容标准化。您和其他开发人员必须就某些事情达成一致。无论你们中的谁想做一些超出商定标准的事情,最终都可能会得到好坏参半的结果。干净地来回转换并不总是可能的。

我真的建议只处理它。我对制表符和空格有自己的看法,并且我已经使用它们编写了代码。最好的办法就是设置您的编辑器以匹配标准化样式,并遵循它。在vim中,关闭expandtab,将tabstop设置为你喜欢的。

如果您仍然想尝试,有两种主要方法:

  • 使用 Vim 中的自动命令进行读/写转换。您可能需要 BufReadPostBufWritePreBufWritePost。 (对于编写,您转换为标准,让 Vim 编写它,然后转换回您喜欢的编辑方式。)确保按您喜欢的方式设置 tabstop,然后是这样的(未经测试):

    设置 tabstop=4
    autocmd BufReadPost * 设置 Expandtab |重建
    autocmd BufWritePre * 设置 noexpandtab |重新开始!
    autocmd BufWritePost * 设置 Expandtab |重建
    

* 是将其应用到的文件模式;您可能不得不搞乱它,或者只为某个目录中的文件添加自动命令,以确保您编辑的所有内容都不会发生这种情况。请注意,这很危险;例如,它将替换字符串中的文字制表符。

  • 使用 Git 的涂抹/清洁过滤器。您可以在 man gitattributesPro 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, and BufWritePost. (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):

    set tabstop=4
    autocmd BufReadPost * set expandtab | retab
    autocmd BufWritePre * set noexpandtab | retab!
    autocmd BufWritePost * set expandtab | retab
    

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.

  • Use Git's smudge/clean filters. You can read about them in 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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文