Skip to content

Make pasting literals easier #967

Closed
Closed

Description

@mrshu

I've just ran into this:

$ wget https://addons.mozilla.org/firefox/downloads/latest/161972/addon-161972-latest.xml?src=search
fish: No matches for wildcard 'https://addons.mozilla.org/firefox/downloads/latest/161972/addon-161972-latest.xml?src=search'.
wget https://addons.mozilla.org/firefox/downloads/latest/161972/addon-161972-latest.xml?src=search
     ^

It's obvious that '?' causes this but why is the caret at the beginning of the argument? Maybe it would be worth it finding the wildcard and putting the caret below that?

Activity

  1. ridiculousfish commented on Aug 21, 2013

    @ridiculousfish
    Member

    Good suggestion. This also trips up a lot of people who are accustomed to bash's behavior of passing literal wildcards through when they would not expand to anything. Maybe we can also add URL detection or something.

  2. mrshu commented on Aug 21, 2013

    @mrshu
    ContributorAuthor

    @ridiculousfish thanks for looking at this.

    If you were open to some solution which would be easy to implement I'd be all for trying to do that.

  3. ahti commented on Jun 19, 2014

    @ahti
    Contributor

    Although treating wildcards differently when they look like urls is introducing inconsistency, I still think it would be a reasonable thing to do, because it is, imho, what the user expects.

    When a user enters an url into their terminal, I think it is reasonable to assume that in 99.9% of cases, they don't want it to behave like a wildcard, but just like a plain old url.

  4. maxnordlund commented on Jan 28, 2015

    @maxnordlund
    Contributor

    I've recently encountered this problem as well. I've been trying to come up with a good solution that fits into the design philosophy of fish and after reading through some of the linked issues this seams indeed very hard.

    But then I thought, why not allow a user defined function to modify pasted text before it is inserted? Basically something that is called when the user pastes something, and it's output would be what's inserted. This would make any escaping clearly visible, and allow the user to apply any escaping she might desire.

    To make this easier fish should probably ship a couple of example implementations, but probably not actually enable any by default, since that might be too surprising for the user.


    If for example the clipboard contains the string https://www.example.com/wiki/index.php?page=About this could be one way of doing it:

    ~$ wget <C^p>
    ~$ wget https://www.example.com/wiki/index.php\?page=About

    Another way would be to always wrap any pasted text in quotes, and might be good default behaviour:

    ~$ wget <C^p>
    ~$ wget "https://www.example.com/wiki/index.php?page=About"
  5. zanchey commented on Jan 30, 2015

    @zanchey
    Member

    One of the problems is working out a way of detecting pasted text. Should fish treat all keypresses within a certain threshold as pasted? What about when your connection is briefly interrupted and your keystrokes queue up in mosh - as that will probably appear the same?

  6. ridiculousfish commented on Jan 30, 2015

    @ridiculousfish
    Member

    We could always enhance the terminal itself. It's sort of ridiculous how primitive the communication channel is from terminal to shell - we get signals like SIGWINCH, and not much else. But there have been various efforts to improve it - Final Term, etc.

  7. maxnordlund commented on Jan 30, 2015

    @maxnordlund
    Contributor

    I found a page about bracketed paste that seams to solve this nicely for most terminals, but for anything else I guess you just have to count characters entered during a short amount of time.

  8. zanchey commented on Jan 30, 2015

    @zanchey
    Member

    Ha. That's rad.

  9. ridiculousfish commented on Jan 30, 2015

    @ridiculousfish
    Member

    Wow, I've never heard of bracketed paste before! Nifty.

  10. zanchey commented on Nov 13, 2015

    @zanchey
    Member

    There's some notes on http://thejh.net/misc/website-terminal-copy-paste about implementing bracketed paste safely - importantly, escaping the "unbracket" escape sequence.

  11. changed the title [-]Wildcard problem with wget[/-] [+]Wildcard problem with URLs (wget)[/+] on Feb 21, 2016
  12. faho commented on Oct 19, 2016

    @faho
    Member

    Does this still seem like a good thing to do? Personally, I find the \cv binding for fish_clipboard_paste much more convenient than \cV (my terms paste) anyway.

  13. floam commented on Oct 19, 2016

    @floam
    Member

    Well, I want it and prefer to use my terminals paste a lot, and I think a lot of users can be helped.

    Couldn't we implement bracketed paste entirely with a few clever bind commands?

  14. 30 remaining items

  15. krader1961 commented on Mar 2, 2017

    @krader1961
    Contributor

    The following is in response to a question posed in PR #3871.

    Personally I find @GReagle's argument in issue #967 more compelling than doing magical escaping some times but not others. I guess an argument could be made to only magically escape question-marks since they seem to be, for all intents and purposes, the only character that causes problems. But what do you do if the user has typed wget ' before pasting? And there are probably similar scenarios that complicate doing the right thing. And why just strings that look like URLs? And how exactly would we do so in a robust manner? If we decide to do something about pasting strings with question-marks which don't represent a wildcard character I think we should just bite the bullet and behave like bash/zsh and pass the question-mark through literally without raising an error. We would still treat asterisks like we do today. That has its own problems if the question-mark, when treated as a wildcard, just happens to match a file. Unlikely but not impossible. This is why I am uneasy about adding another special-case rule. At the end of the day I vote for consistency. Which is to say we don't second-guess the user and require them to quote the pasted text if it has a question-mark or asterisk they don't want treated as a wildcard.

  16. maxnordlund commented on Mar 2, 2017

    @maxnordlund
    Contributor

    I think the point is that you paste rather then manually type in the input. When I do the latter it's easy to escape, but if I paste something it gets harder. Sure I can add quotes, but if there's already quotes/hash/dollar sign and whatnot in the string it quickly becomes tedious.

    As I said in #967 (comment) I think quoting should only happen in the argument position, so pasting a command works like now. However if already have wget/git remote add/… then it quotes the pasted text as it's given as an argument. That would be the least surprising behaviour I think.

    The alternative to sometimes ignore wildcards is worse IMO because it depends on the hidden state of the filesystem. Take the example of pasting markdown the **main** point …, that is recursively expanded and any main files get caught. Another example is git's commitish, which uses a lot of special symbols, git diff master@{yesterday}~4..HEAD^!.

    One could argue that those cases a few and far behind, but it would still be very surprising behaviour.


    A side note, all of ?/&/%/# regularly shows up in URLs, and have special meaning in fish.

  17. faho commented on Mar 2, 2017

    @faho
    Member

    The alternative to sometimes ignore wildcards is worse IMO because it depends on the hidden state of the filesystem.

    Of course, and I don't think @krader1961 was seriously suggesting that.

    A side note, all of ?/&/%/# regularly shows up in URLs, and have special meaning in fish.

    "%" and "#" are not an issue there because they only have special meaning at the beginning of a token, i.e. echo a#b prints "a#b".

    For some reason "&" is special everywhere - echo a&echo foo will run "echo a" in the background and "echo foo". Arguably, that's wrong, and I'll post an issue about it.

    So for URLs that leaves "?", which we've talked about removing.

    Take the example of pasting markdown the main point …, that is recursively expanded and any main files get caught.

    Yes, they do. The question here is what you want to happen when you paste **main**. Either you want to have the glob, or, when you think of it as markdown, you'll want to have '**main**' appear instead. Since fish doesn't know that you are currently looking at markdown, it has to guess. If it guesses "this is supposed to be code" and it was supposed to be markdown, then you'll need to go in and escape it - or you could have started quoting before the paste. If it guesses "this is supposed to be literal" and it was supposed to be code, then you'll need to go in and unescape it. The issue here is that you couldn't have started quoting before.


    One possible behavior I've thought about is this:

    • By default, paste as-is - this makes pasting code easy

    • If, when the paste starts, the current token has an unbalanced ', escape any pasted ' - this allows pasting non-code

    Obviously there are a few variants of this:

    • Only check if the token ends with a ', under the assumption that the user is going to deal with other things later

    • Also use " and either also only escape " (thereby letting $ through) or escape both " and $ (which would make this just cosmetically different from ')

    The question is if this is understandable enough. I'll try to code something up to play around with. I can tell you already that \' is going to be an issue.

  18. maxnordlund commented on Mar 2, 2017

    @maxnordlund
    Contributor

    I guess the only really nice solution is dependent on the type of argument for the current command. When the docopt branch lands and we can specify the argument type <URL>/<FILES>, this could be made smarter. Well, at least if it's possible to annotate those as to be escaped.

    But that's not viable now, and I like the escape-on-quote mechanic. If I already mark the stuff I'm about to paste as a string, it should be quite intuitive when it escapes quotes in that string. I also like the "/$ escape combo, coming from double quote languages I get bitten by pasting stuff with dollar signs, even though I didn't intend it to do that.

  19. faho commented on Mar 2, 2017

    @faho
    Member

    I guess the only really nice solution is dependent on the type of argument for the current command. When the docopt branch lands and we can specify the argument type /, this could be made smarter.

    I'd argue you don't want that, for a multitude of reasons:

    • The rule is much harder - it's "when fish thinks this is something that shouldn't be expanded"

    • Sometimes you want to actually have an argument expand into a URL

    • The docopt system won't have 100% coverage of every tool ever

    In short: Something that can correctly predict the future 50% of the time is really really impressive, but also infuriating.


    I've coded up a rough version of this both with bracketed-paste and fish_clipboard_paste.

    Here's the latter, since that's easier to test:

    function fish_clipboard_paste
        set -l data
        if type -q pbpaste
            set data (pbpaste)
        else if type -q xsel
            # Return if `xsel` failed.
            # That way any xsel error is printed (to show e.g. a non-functioning X connection),
            # but we don't print the redundant (and overly verbose for this) commandline error.
            # Also require non-empty contents to not clear the buffer.
            if not set data (xsel --clipboard)
                return 1
            end
        end
        # If the current token has an unmatched single-quote,
        # escape all single-quotes (and backslashes) in the paste,
        # in order to turn it into a single literal token.
        #
        # This eases pasting non-code (e.g. markdown or git commitishes).
        # TODO: This does not handle
        # single-quotes quoted in double-quotes.
    
        # Remove escaped single-quotes first so that detecting unbalanced ones is easier.
        if commandline -ct | string replace -r "(\\\\)*\\\'" "" \
        | string match -qr "^([^']*'[^']*'|[^']*)*'('[^']*'|[^']*)*\$"
            set data (string replace -ra "(['\\\])" '\\\\\\\$1' -- $data)
        end
        if test -n "$data"
            commandline -i -- "$data"
        end
    end

    So far, it seems to feel okay, though I've mostly been pasting weird stuff like just ' or \\\'

  20. changed the title [-]support bracketed-paste mode[/-] [+]Make pasting literals easier[/+] on Mar 2, 2017
  21. faho commented on Mar 2, 2017

    @faho
    Member

    I've now retitled this to be closer to the original issue - there's nothing we can or should make special about URLs, but we can fix pasting.

    I'll change my PR to not close this when merging.

    Just to be clear though: Bracketed paste is a necessity to fix this for terminal-side paste (i.e. not using fish_clipboard_paste) because without it we cannot distinguish between normal user input and pasting.

  22. modified the milestones: , next-major on Mar 16, 2017
  23. locked as resolved and limited conversation to collaborators on Apr 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions