1

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
/path/to/something/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
/path/to/something/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.

3 Answers 3

2

This should also work:

which foo.sh | xargs vim

1
  • or, in zsh: vim =foo.sh Nov 7, 2010 at 22:27
1

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
/path/to/something/foo.sh
$ vim `!!`

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

2
  • 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, 2010 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, 2010 at 14:35
1

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.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .