This is a bit pie-in-the-sky but I've learned not to make assumptions about the capabilities of anything, since I'm frequently amazed by hidden features buried in UNIX/Linux tools, so here goes.

I often find myself running commands where I need to locate a file on a system and then once I've eyeballed the location for sanity, I need to do something with the path.

Take this workflow, for instance:

$ which foo.sh
$ vim `which foo.sh`

That second command takes me 1-2 entire seconds to type. It's madness. I do it so often it's starting to feel like boilerplate that I should be able to factor out.

What I'd love is fantasy shell magic that would let me do the following:

$ which foo.sh
$ which foo.sh |! vim

This second case is far superior IMO. I find it much easier to type "|! vim" after the up-arrow than I do seeking the start of the line, typing vim backtick, then seeking the end of the line and typing a final backtick.

Imagine the pipe-bang operator doing something similar to "| xargs,vim" but in a way that preserves the terminal for vim and as also something much shorter to type. Pipe-bang is of course a syntax error in bash and it cannot be aliased, so the thing I want to do seems impossible.

Or is it? I suspect this is a case of TMTOWTDI but I'm having a mental block.

It doesn't have to be literally pipe-bang either, but something close is the goal.


This should also work:

which foo.sh | xargs vim

  • or, in zsh: vim =foo.sh – sendmoreinfo Nov 7 '10 at 22:27

I do the same thing, often (usually with which to find a binary, then rpm -qf to see which package provides it)! My route is better than what you do, but slower than what you want; I offer it as a half-way house.

$ which foo.sh
$ vim `!!`

On most keyboards, the backtick and the 1 key are pretty close, too, so it's fairly fast to type.

  • facepalm I don't know why sticking !! inside of backticks never occured to me, especially after all of those times I've been burned putting ! inside double-quotes. Let me try this for a day and see how I feel :) – mbac32768 Oct 26 '10 at 14:25
  • <large grin> don't worry about it, i definitely have those lapses, too. as a fix, i hope it works out for you. – MadHatter Oct 26 '10 at 14:35

Give this (vim previous) a try:

$ alias vp='vim $(fc -s)'
$ which foo.sh
$ vp

The fc -s re-executes the previous command with a substitution (null in this case).

You can put the alias in your ~/.bashrc file. I have mine in an aliases file which is sourced by my ~/.bashrc.

  • Nice find, but is unfortunately not an answer to this question. – mbac32768 Dec 7 '11 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.