Skip to content

tilde characters in scripted tab completions are escaped (sometimes) #4570

Closed

Description

@msteed
msteed@bones:~> fish --version
fish, version 2.7.0
msteed@bones:~> echo $version
2.7.0
msteed@bones:~> uname -a
Linux bones 4.13.12-1-ARCH #1 SMP PREEMPT Wed Nov 8 11:54:06 CET 2017 x86_64 GNU/Linux
msteed@bones:~> echo $TERM
tmux-256color
msteed@bones:~>

The problem:

msteed@bones:~> sh -c 'env HOME=$(mktemp -d) fish'
Welcome to fish, the friendly interactive shell
msteed@bones /h/msteed> cd
msteed@bones ~> mkdir -p a/b
msteed@bones ~> cd a/b
msteed@bones ~/a/b> cd
msteed@bones ~> cdh a<TAB>

When I hit Tab, fish supplies this completion:

msteed@bones ~> cdh \~/a/b

If I accept that, fish (correctly) complains:

cd: The directory “~/a/b” does not exist

If I hit Tab with an empty directory name, i.e., cdh <TAB>, fish lists the completions with the ~ characters escaped, but selecting a completion un-escapes the string so the command line is formatted correctly.

Edit: this may be the problem reported in #4566

Activity

  1. added
    bug
    on Dec 3, 2017
  2. added this to the fish-future milestone on Dec 3, 2017
  3. yig commented on Oct 28, 2020

    @yig

    I just ran into this. Does anyone have any pointers for how I might fix this (and make a pull request)?

  4. faho commented on Oct 28, 2020

    @faho
    Member

    @yig: This is a bit of a tough one.

    The problem is, basically, that fish always assumes scripted completions shouldn't contain anything that expands, so if a completion prints $VAR that should be escaped to \$VAR, or, as in this case, if it prints ~/something that it should escape the ~. There's an exception for if the current token already starts with a ~, but I'm not sure why and if that is intentional.

    That seems to be the right thing in most circumstances, and I don't think changing the default is workable (e.g. is there a case where git expects an argument starting with ~?).

    Or it might be possible to just not escape ~?

    Or maybe expand the ~ to the value of $HOME?

    It's possible that this could be done with an option to complete - complete --unescaped that would then have to be used in the cdh completions (and everywhere else where it's applicable).

    But really, this requires looking at what completions commonly need and figuring out the best way to satisfy that, while not breaking the existing completion scripts.


    In the meantime, I'm just going to remove the ~ shortening from the cdh completions unless the current token starts with ~. It's only cosmetic anyway, and in that case it's preferable to have something that works.

  5. changed the title [-]tilde characters in cdh tab completions are escaped (sometimes)[/-] [+]tilde characters in scripted tab completions are escaped (sometimes)[/+] on Jul 14, 2021
  6. kidonng commented on Oct 6, 2021

    @kidonng
    Contributor

    Here is a little workaround I come up with:

    function _complete
        if ! commandline -P
            set -l token (commandline -t)
    
            if string match -q -- "\~*" $token
                commandline -rt (string replace \\ "" $token)
                return
            end
        end
    
        commandline -f complete
    end
    
    
    bind \t _complete

    Now use tilde in your completions, and when fish completes leading \~, just press Tab again and it be replaced with a working one.

  7. cstyles commented on Nov 13, 2021

    @cstyles
    Contributor

    I ran into this issue with a custom completion that uses __fish_complete_directories. I can reliably reproduce it with unzip whose completion file uses both __fish_complete_directories and __fish_complete_suffix. For example, if I have a file in my home directory called archive.zip and I run this:

    unzip ~/zip<TAB>

    I would expect it to complete to unzip ~/archive.zip but it actually completes to unzip \~/archive.zip. Similarly, this...

    unzip -d ~/Dw<TAB>

    ...completes to unzip -d \~/Downloads/.

    Completing something that doesn't require replacement (e.g., unzip ~/arc<TAB>) works fine.


    It's possible that this could be done with an option to complete - complete --unescaped that would then have to be used in the cdh completions (and everywhere else where it's applicable).

    I opened #8441 to do this.

  8. locked as resolved and limited conversation to collaborators on Nov 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcompletions

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions