简体   繁体   English

xargs管道ls命令的输出

[英]xargs to pipe the output of ls command

I am running the command 我在运行命令

ls *my_file.txt* | xargs vim

The shell throws a warning message: shell抛出一条警告消息:

Vim: Warning: Input is not from a terminal

following that the file is opened. 然后打开文件。 Note there is only one instance of *my_file.txt*. 请注意,* my_file.txt *只有一个实例。 On exiting the file, I see that on each ENTER the prompt is not on the next line but continues on the same line. 在退出文件时,我看到在每个ENTER上,提示不在下一行,而是在同一行继续。 The characters are not typed on the display but are buffered and executed on subsequent enter. 字符不会在显示屏上键入,而是在后续输入时进行缓冲和执行。 Basically, the display gets awry. 基本上,显示器出错了。

The intent is basically to pipe the searched file_name to vim. 目的是基本上将搜索到的file_name传递给vim。 So any alternative solutions are welcome. 所以欢迎任何替代解决方案。

Use find and exec instead of xargs . 使用findexec代替xargs

find /search/path/ -type f -name "*my_file.txt*" -exec vim {} \;

You can add more options to find like -depth to restrict traversing recursively, -regex for complex regular expressions for finding files etc. 您可以添加更多选项以find -depth来限制递归遍历, -regex用于复杂正则表达式以查找文件等。

I'm not entirely certain I understand what you're doing. 我不完全确定我明白你在做什么。 If you just want to edit all the *my_file.txt* files, use: 如果您只想编辑所有*my_file.txt*文件,请使用:

vim *my_file.txt*

No need to mess about with find or xargs . 不需要乱搞findxargs The only problem you'll have is if you have so many files of that form that you'll blow the limits of the command line. 你遇到的唯一问题是,如果你有那么多的那种形式的文件,你将会超出命令行的限制。 But, seriously, you probably don't want to be editing that many files anyway. 但是,严肃地说,你可能不想编辑那么多文件。

As per this discussion here , vim is not one of those programs that you can easily play with its standard input and output (at least in vi mode - ex mode may be different). 按照这个讨论在这里vim是不是这些程序,您可以用它的标准输入和输出轻松播放之一(vi模式至少- EX模式可能会有所不同)。

You can see a similar problem with doing things like: 您可以通过以下方式查看类似的问题:

`vi filename`
$(vi filename)
vi filename </dev/null

Basically, vim is not meant to have its stdin/stdout mucked about with, it's an interactive editor and expects a terminal device. 基本上, vim并不意味着它的stdin / stdout被搞砸了,它是一个交互式编辑器,并期望一个终端设备。 The after-effects that you're seeing are probably either curses or vim itself not cleaning up the stty settings properly. 您看到的后果可能是cursesvim本身没有正确清理stty设置。

If you have a more complex command that generates file names for editing, you can use something like: 如果您有一个更复杂的命令来生成用于编辑的文件名,您可以使用以下内容:

vim $(arbitrarily_bizarre_command_which_outputs_filenames)

keeping in mind that the output is simple text substitution so won't easily handle files like my list of porn sites.txt (with spaces in them). 请记住,输出是简单的文本替换,因此不会轻易处理像my list of porn sites.txt文件(包含空格)。 vim will see that as five different arguments. vim将看到五个不同的论点。

"ls my_file.txt* | vim -" (note the dash at the end) “ls my_file.txt * | vim - ”(注意末尾的破折号)

not sure how to do this on bsd though. 不知道如何在bsd上这样做。

"ls my_file.txt* | vim -" “ls my_file.txt * | vim - ”

will not work in bash, as vim treats it as the file content rather than the file name. 将无法在bash中工作,因为vim将其视为文件内容而不是文件名。

find /search/path/ -type f -name "*my_file.txt*" -exec vim {} \\; find / search / path / -type f -name“* my_file.txt *” - exec vim {} \\;

note that this will edit files one at a time. 请注意,这将一次编辑一个文件。 if you still would like to edit multiple files in different buffers you should still use 如果您仍想编辑不同缓冲区中的多个文件,您仍应使用

ls *my_file.txt* | ls * my_file.txt * | xargs vim xargs vim

or 要么

vim $(ls *my_file.txt*) vim $(ls * my_file.txt *)

provided the output fits in command line argument maximum length. 如果输出适合命令行参数最大长度。

another solution: 另一种方案:

ls *my_file.txt*
vim $(!!)

!! will repeat the command that was executed before, $() will insert the command output in the command line. 将重复之前执行的命令,$()将在命令行中插入命令输出。

Careful: Does not work with filenames that contain spaces or other nasty characters. 小心:不适用于包含空格或其他令人讨厌的字符的文件名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM