3

I have this command (for detailed info see Shortcut to google selected text):

sh -c 'firefox -remote "openURL(http://www.google.com/search?q=$(xsel),new-tab)"'

But when I select a text which ends with ) it parses it as a closing parenthesis.

Is there a workaround this? (like escaping it somehow?)

2
  • Does this not work? sh -c 'firefox -remote "openURL(http://www.google.com/search?q='"$(xsel)"',new-tab)"'
    – mikeserv
    Aug 7 '14 at 7:58
  • No, I get not parsable if the string ends with ) . John1024's solution escapes the characters and it works because firefox actually wants URL encoding (it's a win-win situation). Aug 7 '14 at 8:19
4

You need to do URL encoding on the string returned by xsel. Using python:

sh -c 'firefox -remote "openURL(http://www.google.com/search?q=$(python -c "import urllib, sys; print urllib.quote(sys.argv[1])" "$(xsel)"),new-tab)"'

Using Perl:

sh -c 'firefox -remote "openURL(http://www.google.com/search?q=$(perl -MURI::Escape -e '\''print uri_escape($ARGV[0]);'\'' "$(xsel)"),new-tab)"'

Explanation

Observe what happens when we ask firefox to perform a google search on foo():

$ firefox -remote 'openURL(http://www.google.com/search?q=Foo(),new-tab)'
Error: Failed to send command: 500 command not parseable

To make it "parseable," we need to URL-encode the characters. For example, here is what Foo() looks like when encoded:

$ python -c "import urllib, sys; print urllib.quote(sys.argv[1])"  "Foo()"
Foo%28%29
4
  • Both work! Let me get this straight: they work cause they translate symbols like ) into &#41 and terminal command won't get corrupted? Aug 7 '14 at 7:17
  • 1
    @user2692669 Yes. firefox -remove option expects an internet-safe URL. So, the parens need to be removed before firefox sees them.
    – John1024
    Aug 7 '14 at 7:33
  • -remove or -remote? Either-way I get what you want to say. thanks. Aug 7 '14 at 7:41
  • @user2692669 Yes, that was a typo. Sorry.
    – John1024
    Aug 7 '14 at 7:43

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.