编辑前一个命令输出的第 n 行的快速方法
我经常发现自己在做这样的工作流程:(
$ 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
方式 1:不将确切的文件传递给 vim,而是将整个输出传递给 vim。在 vim 中选择文件
当前您正在分两步操作:
1 - 启动 find/grep...cmd
2 - vim !!....
如果您确定要使用 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:
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
评论后修改。不确定它是否“方便”。
Modified after your comment. Not sure if it's "convenient" though..
也许这就是您正在寻找的?
Maybe this is what you're looking for?
如果你想要交互式评论,也许你可以使用这样的东西:
If you want an interactive review maybe you can use something like this:
你几乎做到了!
这里的最后一行将在
vi
中打开 file1。You almost did it!!
the last line overe here will open the file1 in
vi
.