编辑前一个命令输出的第 n 行的快速方法

发布于 2024-12-18 12:38:57 字数 573 浏览 1 评论 0原文

我经常发现自己在做这样的工作流程:(

$ find . |grep somefile
./tmp/somefile.xml
./test/another-somefile.txt

查看输出)

$ vim ./tmp/somefile.xml

现在,如果有一些方便的方法来使用 find 命令的输出并将其提供给 vim,那就太好了。

我想出的最好的办法是:(

$ nth () { sed -n $1p; }
$ find . |grep somefile
./tmp/somefile.xml
./test/another-somefile.txt

审查输出)

$ vim `!!|nth 2`

我想知道是否还有其他,也许更漂亮的方法来完成同样的事情?

为了澄清这一点,我想要一种方便的方法来从先前运行的命令中获取第 n 行,以快速打开该文件以在 vim 中进行编辑,而无需剪切和编辑。使用鼠标或制表符粘贴文件名,完成文件路径。

I often find find myself doing a workflow like this:

$ find . |grep somefile
./tmp/somefile.xml
./test/another-somefile.txt

(review output)

$ vim ./tmp/somefile.xml

Now, it would be neat if there was some convenient way of using the output of the find command and feed it to vim.

The best I've come up with is:

$ nth () { sed -n $1p; }
$ find . |grep somefile
./tmp/somefile.xml
./test/another-somefile.txt

(review output)

$ vim `!!|nth 2`

I was wondering if there are other, maybe prettier, ways of accomplishing the same thing?

To clarify, I want a convenient way of grabbing the nth line from a previously run command to quickly open that file for editing in vim, without having to cut & paste the filename with the mouse or tab-complete my way through the file path.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

赢得她心 2024-12-25 12:38:57

方式 1:不将确切的文件传递给 vim,而是将整个输出传递给 vim。在 vim 中选择文件

当前您正在分两步操作:

1 - 启动 find/grep...cmd

2 - vim !!....

如果您确定要使用 vim 打开一个文件查找结果中的(或更多)文件。你可以尝试:

find. (with grep if you like)  |vim -

然后你在vim中就有了完整的输出,现在你可以使用vim magic将光标移动到你想要编辑的文件,然后按gf。 (我有时会这样做)

方法 2:在 find (或 grep)中优化你的正则表达式,以获得你想要编辑的单个文件。

这根本不是一件难事。然后你就可以 vim !!

你的 nth() 很好。但是想象一下输出中有 30 行,并且您的文件位于第 16 行。您如何计算它?当然可以在最后加上|nl,那么就不能直接使用了!!不再......

只是我的2美分

way 1: don't pass exact file to vim, but the whole output. choose the file in vim

currently you are working in two steps:

1 - launch the find/grep... cmd

2 - vim !!....

if you are sure that you want to use vim to open one (or more) file(s) from the find result. you may try:

find. (with grep if you like)  |vim -

then you have the whole output in vim, now you can use vim magic to move cursor to the file you want to edit, then press gf. (I do this sometimes)

way 2: refine your regex in your find (or grep), to get the single file, that you want to edit.

this is not a hard thing at all. then you can just vim !!.

your nth() is nice. however imagine there are 30 lines in output, and your file sits in the line# 16. how do you count it? sure you can add |nl at the end, then you cannot directly use !! any longer..

just my 2 cents

萌辣 2024-12-25 12:38:57

评论后修改。不确定它是否“方便”。

command | tail -n3 | head -n1 | xargs vim

Modified after your comment. Not sure if it's "convenient" though..

command | tail -n3 | head -n1 | xargs vim
看春风乍起 2024-12-25 12:38:57

也许这就是您正在寻找的?

find . -name "*somefile*" -exec vim -p {} \;

Maybe this is what you're looking for?

find . -name "*somefile*" -exec vim -p {} \;
那小子欠揍 2024-12-25 12:38:57

如果你想要交互式评论,也许你可以使用这样的东西:

TMP_LIST=""; for i in `find . | grep somefile`; do echo $i; read -p "(y/n)?"; [ $REPLY == "y" ] && TMP_LIST="$TMP_LIST $i"; done; vim $TMP_LIST

If you want an interactive review maybe you can use something like this:

TMP_LIST=""; for i in `find . | grep somefile`; do echo $i; read -p "(y/n)?"; [ $REPLY == "y" ] && TMP_LIST="$TMP_LIST $i"; done; vim $TMP_LIST
情感失落者 2024-12-25 12:38:57

你几乎做到了!

pearl.251> cat file1
a b c d e f pearl.252> find . -name "file*"
./file1
./file2
./file3
./file4
./file5
./file6
./file7
pearl.253> vi `!!|awk 'NR==1'`

这里的最后一行将在 vi 中打开 file1。

You almost did it!!

pearl.251> cat file1
a b c d e f pearl.252> find . -name "file*"
./file1
./file2
./file3
./file4
./file5
./file6
./file7
pearl.253> vi `!!|awk 'NR==1'`

the last line overe here will open the file1 in vi.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文