We don’t allow questions seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(7)
好吧,给出的解决方案都不能让我满意,所以我自己编码了。 :)
请参阅此处:
Ok, none of the given solutions satisfied me, so I coded it myself. :)
See here:
根据源语言,您可以尝试 GNU 缩进。它可以做很多与源代码缩进相关的事情,尽管它可能比您需要的更复杂。
例如,如果我将以下程序提供给
indent -di0
它会将其替换为:
或者,如果您需要一些愚蠢的简单内容,则可以使用
expand/unexpand命令。
Depending on the source language, you could try out GNU indent. It can do a large number of things relating to the indentation of source code, though it might be more complex than you need.
For example, if I give the following program to
indent -di0 <inputfile>
It will replace it with:
Or, if you need something stupid simple, there is the
expand/unexpand
commands.您可以使用正则表达式将 N 个空格替换为制表符。例如在 Python 中:
You could use a regular expression to replace N spaces by a tab charater. For example in Python:
有两件事,
sed -i
是你的朋友 -sed -i XXX.txt 's/^[ ]\{2\}/\t/g'
鉴于我的 AWK-fu 并不强大(而且我不知道它是否可以做到 #2 不能做到的事情),我将编写一个 PHP 脚本来计算空格并将其替换为制表符。
Two things,
sed -i
is your friend -sed -i XXX.txt 's/^[ ]\{2\}/\t/g'
Given my AWK-fu is not strong (and I don't know if it can do what #2 can't), I will write a PHP script to calculate the spaces and replace them with tabs.
以下是 Python 中可能的解决方案:
Here is a possible solution in Python:
这会将前导空格(甚至散布有制表符)转换为制表符。通过设置变量来指定要转换的空格数。杂乱的空间将被塌陷为空。出现在除空格或制表符之外的任何字符之后的空格和制表符将不会被触及。
示例:
将转换为
如果这不完全是您想要的,可以进行调整。
This will convert leading spaces (even interspersed with tabs) into tabs. Specify the number of spaces to convert by setting the variable. Stray spaces will be collapsed to nothing. Spaces and tabs that appear after any character other than space or tab will not be touched.
Example:
will be converted to
If that's not exactly what you're looking for, adjustments can be made.