“嗯,我需要编辑file-i-must-edit-2
,但我不记得它在哪里。”
locate file-i-must-edit
/home/user/file-i-must-edit-1
/home/user/file-i-must-edit-2
/home/user/file-i-must-edit-3
“太好了!我希望有一种方法可以避免/home/user/file-i-must-edit-2
再次打字……”
有没有办法
nano /home/user/file-i-must-edit-2
通过输入类似的内容来避免打字nano <output line 2>
?
答案1
如果你只得到一行输出,那很简单:
locate file-i-must-edit
nano $(!!)
当有更多行时,您可以使用一种技术,但它涉及以不同的方式运行原始命令(您可能不想一直这样做):
$ touch a b c
$ OUT=( $(find .) )
$ echo ${OUT[2]}
./b
为了避免打字,您可以做的一件事是重复上一个命令(当然使用 readline),缩小范围以仅获取一行,然后执行nano $(!!)
或通过管道将其传输到xargs
.
答案2
这是一种编程方法(与使用鼠标进行 X-Windows 选择相比)
# function to nano edit a line (by number) from $list
nano-n() { nano "$(sed -n "$1{p;q}" <<<"$list")"; }
# As you produce your list of files, save them to a variable ($list),
# as well as printng them to the terminal.
# Do this by "duplicating" descriptor 1 (stdout) to
# another available descriptor (eg. 9)
exec 9>&1; list="$(locate file-i-must-edit |tee /dev/fd/9)"
# now you can just type the following at the propmt (assuming that $list is available at the prompt)
nano-n 2