Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am attempting to open a file using firefox via the command line. The command I run is;

firefox -remote "openurl(file://`pwd`/filename.html)"

I would like to be able to autocomplete the name of the file which can sometimes be lengthy by hitting tab.

share|improve this question
    
The reason that it doesn't work are the double quotes! –  FSMaxB Mar 20 '13 at 13:57

1 Answer 1

up vote 1 down vote accepted

Well, it’s clunky, but you could type

sh -c 'firefox -remote "openurl(file://`pwd`/$1)"'  -  PeaTab

or

sh -c 'firefox -remote "openurl(file://$1/$2)"'  -  `pwd`  PeaTab

If you’re going to be doing this on a regular basis (without a lot of variation), you might want to put it into a script.

P.S. You could leave off one of the slashes after the file: because the pwd will provide one.


Edit:

How/why does this work?

The interactive shell that you’re running (let’s assume that it is bash and refer to it as such, for disambiguation) is the one that’s reading your command line input and doing things like autocomplete, and it requires that any command-line word that it autocompletes must look like a filename or pathname.  I’ve had the experience that it can handle

        $PWD/ScoTab

and

        `pwd`/ScoTab

but not

        $(pwd)/ScoTab

Go figure.  YMMV.  In any case, it cannot handle

"openurl(file://`pwd`/ScoTab

because that cannot possibly be interpreted as a filename or pathname (unless bash could parse the "openurl(… call, which it cannot).

The trick is to get it to do the autocomplete on an isolated filename and then glue the pieces together.  The only way to set $1, $2, etc., for your interactive shell is to type a command like set foo bar or set -- foo bar, and this is generally not done.  We need a sh on the command line so we can set its $1, $2, etc., by putting values on its command line.  So we use the bash to do the autocomplete, which provides command-line parameter(s) to sh, and then we use the sh command

'firefox -remote "openurl(file://$1/$2)"'

to interpret the $1 and $2, and then “glue the pieces together”.


The process of writing this made me realize another option:

set  `pwd`  ScoTab(do autocomplete) Enter

followed by

firefox -remote "openurl(file:/$1/$2)" Enter

As I indicated earlier, the first command above sets the positional parameters ($1 and $2) for the interactive shell (bash) and the second line uses them.  As I mentioned, people generally don’t set the positional parameters of their interactive shell, but there’s nothing wrong with it –– it’s just uncommon.  It should be noted that $1 and $2 will retain these values until you reset them, or you logout.

You should be able to combine the above two lines into one:

set `pwd` ScoTab(do autocomplete) ; firefox -remote "openurl(file:/$1/$2)" Enter

share|improve this answer
    
Why do I need to run the sh command? –  PeanutsMonkey Mar 23 '13 at 7:06
    
Because the interactive shell that you’re running (let’s assume that it is bash and refer to it as such, for disambiguation) is the one that’s reading your command line input and doing things like autocomplete, and it requires that any command-line word that it autocompletes must look like a filename or pathname. I don’t know off-hand whether it can handle `pwd`/Pea(Tab), but, as you have discovered, it certainly cannot handle "openurl(file://`pwd`/Pea(Tab). –  Scott Mar 23 '13 at 20:21
    
The trick is to get it to do the autocomplete on an isolated filename and then glue the pieces together. The only way to set $1, $2, etc., for your interactive shell is to type a command like set foo bar or set -- foo bar. We need a sh on the command line so we can set its $1, $2, etc., by putting values on its command line. So we use the bash to do the autocomplete, which provides a command-line parameter to sh, and then we use the sh command 'firefox -remote "openurl(file://$1/$2)"' to interpret the $1 and $2, and then “glue the pieces together”. –  Scott Mar 23 '13 at 20:21
    
@PeanutsMonkey: I updated my answer. –  Scott Mar 27 '13 at 22:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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