如何将制表符转换为一个目录的每个文件中的空格(可能递归)?
此外,是否有一种方法来设置每个制表符的空格数?
如何将制表符转换为一个目录的每个文件中的空格(可能递归)?
此外,是否有一种方法来设置每个制表符的空格数?
当前回答
我的建议是使用:
find . -name '*.lua' -exec ex '+%s/\t/ /g' -cwq {} \;
评论:
Use in place editing. Keep backups in a VCS. No need to produce *.orig files. It's good practice to diff the result against your last commit to make sure this worked as expected, in any case. sed is a stream editor. Use ex for in place editing. This avoids creating extra temp files and spawning shells for each replacement as in the top answer. WARNING: This messes with all tabs, not only those used for indentation. Also it does not do context aware replacement of tabs. This was sufficient for my use case. But might not be acceptable for you. EDIT: An earlier version of this answer used find|xargs instead of find -exec. As pointed out by @gniourf-gniourf this leads to problems with spaces, quotes and control chars in file names cf. Wheeler.
其他回答
你可以使用vim:
find -type f \( -name '*.css' -o -name '*.html' -o -name '*.js' -o -name '*.php' \) -execdir vim -c retab -c wq {} \;
正如Carpetsmoker所说,它将根据你的vim设置重新标签。文件中的modeline(如果有的话)。此外,它不仅将替换行首的制表符。这通常不是你想要的。例如,你可能有文字,包含制表符。
对于递归应用程序,我喜欢上面的“find”示例。为了使其具有非递归性,只更改当前目录中匹配通配符的文件,shell glob扩展可以满足少量文件的需求:
ls *.java | awk '{print "expand -t 4 ", $0, " > /tmp/e; mv /tmp/e ", $0}' | sh -v
如果在您相信它工作之后希望它保持沉默,只需在sh命令的末尾加上-v。
当然,您可以在第一个命令中选择任何一组文件。例如,以受控的方式只列出一个特定的子目录(或多个目录),如下所示:
ls mod/*/*.php | awk '{print "expand -t 4 ", $0, " > /tmp/e; mv /tmp/e ", $0}' | sh
或者反过来运行find(1)与深度参数等的一些组合:
find mod/ -name '*.php' -mindepth 1 -maxdepth 2 | awk '{print "expand -t 4 ", $0, " > /tmp/e; mv /tmp/e ", $0}' | sh
如何将制表符转换为一个目录的每个文件中的空格(可能吗 递归地)?
这通常不是你想要的。
你想为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)
将制表符转换为空格只是在”。Lua " files [tab -> 2个空格]
find . -iname "*.lua" -exec sed -i "s#\t# #g" '{}' \;
将目录下的所有Java文件递归转换为4个空格而不是制表符:
find . -type f -name *.java -exec bash -c 'expand -t 4 {} > /tmp/stuff;mv /tmp/stuff {}' \;