如何将制表符转换为一个目录的每个文件中的空格(可能递归)?

此外,是否有一种方法来设置每个制表符的空格数?


当前回答

尝试命令行工具expand。

expand -i -t 4 input | sponge output

在哪里

-i用于只展开每行的前导制表符; -t 4表示每个制表符将转换为4个空格字符(默认为8个)。 Sponge来自moreutils包,避免清除输入文件。在macOS上,moreutils包可以通过Homebrew (brew install moreutils)或MacPorts (sudo port install moreutils)获得。

最后,在使用Homebrew (brew install coreutils)或MacPorts (sudo port install coreutils)安装coreutils之后,可以在macOS上使用gexpand。

其他回答

没有人提到rpl?使用rpl可以替换任何字符串。 要将制表符转换为空格,

rpl -R -e "\t" "    "  .

非常简单。

警告:这会破坏你的回购。 这将破坏二进制文件,包括那些在svn, .git!使用前请阅读评论!

找到。-iname '*.java' -type f -exec sed -i。/\t/ /g' {} +

原始文件保存为[filename]. trans。

将'*.java'替换为您正在寻找的文件类型的文件结尾。这样可以防止二进制文件的意外损坏。

缺点:

将替换文件中的所有制表符。 如果在这个目录中有一个5GB的SQL转储,将花费很长时间。

如何将制表符转换为一个目录的每个文件中的空格(可能吗 递归地)?

这通常不是你想要的。

你想为png图像做这个吗?PDF文件吗?.git目录?你的 Makefile(需要制表符)?一个5GB的SQL转储?

理论上,你可以通过很多排除选项来寻找 否则你在使用;但是这个是易碎的,只要你再加一个就会碎 二进制文件。

你想要的至少是:

跳过超过一定大小的文件。 通过检查NULL字节的存在来检测文件是否为二进制。 只替换文件开头的选项卡(expand执行此操作,sed 不)。

据我所知,没有“标准”的Unix实用程序可以做到这一点,而且用shell一行程序来做到这一点并不容易,因此需要一个脚本。

之前我创建了一个小脚本叫做 Sanitize_files完全正确 那它还修复了一些其他常见的问题,比如将\r\n替换为\n, 加上后面的\n,等等。

您可以在下面找到一个没有额外特性和命令行参数的简化脚本,但是我 建议您使用上面的脚本,因为它更有可能收到错误修复和 其他更新除了这篇文章。

我还想指出,作为对其他一些答案的回应, 使用shell globbing不是一个健壮的方法,因为 或者稍后,您将最终与更多的文件将适合ARG_MAX(在现代 Linux系统是128k,看起来很多,但迟早不是 足够的)。


#!/usr/bin/env python
#
# http://code.arp242.net/sanitize_files
#

import os, re, sys


def is_binary(data):
    return data.find(b'\000') >= 0


def should_ignore(path):
    keep = [
        # VCS systems
        '.git/', '.hg/' '.svn/' 'CVS/',

        # These files have significant whitespace/tabs, and cannot be edited
        # safely
        # TODO: there are probably more of these files..
        'Makefile', 'BSDmakefile', 'GNUmakefile', 'Gemfile.lock'
    ]

    for k in keep:
        if '/%s' % k in path:
            return True
    return False


def run(files):
    indent_find = b'\t'
    indent_replace = b'    ' * indent_width

    for f in files:
        if should_ignore(f):
            print('Ignoring %s' % f)
            continue

        try:
            size = os.stat(f).st_size
        # Unresolvable symlink, just ignore those
        except FileNotFoundError as exc:
            print('%s is unresolvable, skipping (%s)' % (f, exc))
            continue

        if size == 0: continue
        if size > 1024 ** 2:
            print("Skipping `%s' because it's over 1MiB" % f)
            continue

        try:
            data = open(f, 'rb').read()
        except (OSError, PermissionError) as exc:
            print("Error: Unable to read `%s': %s" % (f, exc))
            continue

        if is_binary(data):
            print("Skipping `%s' because it looks binary" % f)
            continue

        data = data.split(b'\n')

        fixed_indent = False
        for i, line in enumerate(data):
            # Fix indentation
            repl_count = 0
            while line.startswith(indent_find):
                fixed_indent = True
                repl_count += 1
                line = line.replace(indent_find, b'', 1)

            if repl_count > 0:
                line = indent_replace * repl_count + line

        data = list(filter(lambda x: x is not None, data))

        try:
            open(f, 'wb').write(b'\n'.join(data))
        except (OSError, PermissionError) as exc:
            print("Error: Unable to write to `%s': %s" % (f, exc))


if __name__ == '__main__':
    allfiles = []
    for root, dirs, files in os.walk(os.getcwd()):
        for f in files:
            p = '%s/%s' % (root, f)
            if do_add:
                allfiles.append(p)

    run(allfiles)

使用vim-way:

$ ex +'bufdo retab' -cxa **/*.*

做备份!在执行上述命令之前,因为它可能损坏您的二进制文件。 要使用globstar(**)进行递归,请通过shop -s globstar激活。 指定特定的文件类型,例如:**/*.c。

修改制表符,添加+'set ts=2'。

然而,缺点是它可以替换字符串中的制表符。

因此,为了更好的解决方案(使用代换法),尝试:

$ ex -s +'bufdo %s/^\t\+/  /ge' -cxa **/*.*

或者使用ex编辑器+扩展实用程序:

$ ex -s +'bufdo!%!expand -t2' -cxa **/*.*

有关尾随空格,请参见:如何为多个文件删除尾随空格?


你可以在你的.bash_profile中添加以下函数:

# Convert tabs to spaces.
# Usage: retab *.*
# See: https://stackoverflow.com/q/11094383/55075
retab() {
  ex +'set ts=2' +'bufdo retab' -cxa $*
}

下载并运行以下脚本,递归地将纯文本文件中的硬选项卡转换为软选项卡。

在包含纯文本文件的文件夹中执行脚本。

#!/bin/bash

find . -type f -and -not -path './.git/*' -exec grep -Iq . {} \; -and -print | while read -r file; do {
    echo "Converting... "$file"";
    data=$(expand --initial -t 4 "$file");
    rm "$file";
    echo "$data" > "$file";
}; done;