14

In recent months, I find that if I copy the URL in Chrome and then Shift-Insert in the urxvt, the pasted URL is escaped.

For example, the original URL is:

http://example.com/?a=c

the pasted content is:

http://example.com/\?a\=c

But if I paste it into other places, such as in the web browser or in the vim, there is no escape.

What I use is Arch Linux, urxvt, zsh, oh-my-zsh.

1
  • You want that it will be never escaped at all when you post it in the shell?
    – phk
    Jul 2 '16 at 15:08
9

This is a Z Shell module known as "url-quote-magic" in action. It is trying to ensure that you end up with what you intended even though you completely ignored shell metacharacters and quoting. It detects when (in ZLE) an unquoted word looks like a URL, with a schema on the front, and changes the way that character self-insertion happens to the rest of the word.

If the paste operation had simply entered

http://example.com/?a=c
into the command-line editor, you would have ended up with a command that when run would have tried to perform filename expansion and fail to execute because no filenames matched. A well-known example by Vivek Verma is:

~$  mpv https://www.youtube.com/watch?v=HcgJRQWxKnw
zsh: no matches found: https://www.youtube.com/watch?v=HcgJRQWxKnw
~$  

Remember: The Z Shell has a lot of filename expansion characters — not only including ?, [, ], and *; but also = (command name search), < and > (number ranges), ~, ^, and #. And that's not even including the Korn shell compatibility mechanisms. See the zshexpn manual page for the quite lengthy details.

url-quote-magic determined that this wasn't a quoted word, recognized the http: schema prefix, and changed the ? and = into \? and \= so that they wouldn't invoke filename expansion.

So unless you actually want, for some reason, URLs that you've pasted or typed in (without your adding any enclosing single quotes, note) to be subject to all of the filename expansions and either fail to work or (in the rare extreme surprise case) produce unexpected matches, you should probably be glad that this automatic quoting of shell metacharacters in what you clearly think of as URLs is being done for you. ☺

3
  • Do you mean that it just displays http://...\?a\=c and helps zsh avoids expanding. If I do something like aria2c http://...\?a\=c in zsh, the actual command to run is still aria2c http://...?a=c?
    – 宇宙人
    Jul 2 '16 at 16:13
  • 1
    Hello from 5 years in the future. This doesn't consider if a URL is pasted inside quotes which breaks them.
    – Michael
    Apr 26 '21 at 1:44
  • *, ?, [...]. <->, (a|b)... (and x#, ^x, a~b... with extendedglob) is filename generation aka globbing (POSIX calls it pathname expansion). =cmd, ~user, ~12 is the one called filename expansion in zsh and is not the same as globbing. Aug 23 '21 at 12:05
0

The place to look is with oh-my-zsh and its use (or not) of the safe-paste plugin:

From these and similar reports, it seems that url-quote-magic was broken by changes to not use the bracketed-paste feature. You probably should open a bug report in the appropriate place.

0

The text is shown escaped, but the command does not get affected by those escapes. If you test that URL with any command like curl, wget, or even google-chrome, you will find that it will work without any issue.

1
  • 1
    No, not actually. Under following situation, the command gets affected. In some cases, the urls contain spaces (some urls even contain both CJK characters and spaces), so I must wrap the url with quotes and unescape all \?s back to ?s.
    – 宇宙人
    Nov 7 '16 at 2:00
0

Obviously not the cleanest way to fix this issue, but here's more or less a workaround to copy a string without auto escaping with zsh:

sudo apt-get install xclip
alias zhistory_paste='echo : `expr $(date +%s) + 1`:0\;`printf "$(xclip -o clipboard)\n"` >> ~/.zsh_history && fc -R'

Now, when you want to paste something, you can simply use zhistory_paste, then press the arrow_up key to yank your string.

0

While @JdeBP provided great explanation of what's happening, I don't seem to see here what can be done about this behaviour.

Simply enclosing the URL link with double quotes prevents zsh from escaping in upon paste in:

"http://example.com/?a=c"

pasted content:

"http://example.com/?a=c"
3
  • Welcome to the site, and thank you for your contribution. Please note however that the OP was about pasting a URL copied from the address bar of a web browser, so it would seem difficult (or at least very cumbersome) to apply this solution to that scenario. You may want to add a short note on this.
    – AdminBee
    Aug 23 '21 at 12:43
  • This requires quoting the text before pasting it - which I didn't realize when I first read your answer. I thought you were saying you could paste the text into the command line as long as you were pasting it inside quotes. For the record that doesn't work. :)
    – bradym
    Nov 25 '21 at 0:05
  • Yes, indeed the quoting needs to be added before pasting it. It didn't realize from the OP question that he wants to avoid doing that. I don't have an answer in that case :) Jan 6 at 11:58

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.