Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unnecessary tilde expansions supersede directory and file completions #741

Open
aaronkollasch opened this issue Jul 12, 2024 · 9 comments
Open
Labels
bug Something isn't working

Comments

@aaronkollasch
Copy link
Contributor

Environment

% typeset -p1 VENDOR OSTYPE ZSH_PATCHLEVEL _autocomplete__funcfiletrace
typeset VENDOR=apple
typeset OSTYPE=darwin23.0
typeset ZSH_PATCHLEVEL=zsh-5.9-0-g73d3173
typeset -a _autocomplete__funcfiletrace=(
  /Users/aaron/.dotfiles/zsh/repos/zsh-autocomplete/zsh-autocomplete.plugin.zsh:4
  /Users/aaron/.dotfiles/zsh/config.d/autocomplete.zsh:28
  /Users/aaron/.zshrc:21
  zsh:0
)
% git -C ~autocomplete log --oneline -n1
cfc3fd9 (HEAD, origin/main, origin/HEAD) Fix grammar mistakes in Readme
  • Operating system: macOS Sonoma 14.5
  • Terminal emulator: iTerm2.app

Steps to reproduce

% cd $(mktemp -d)
% git clone --depth 1 -- https://github.com/marlonrichert/zsh-autocomplete.git
<output>
% > .zshrc <<EOF
setopt interactivecomments transientrprompt
PS1='%# '
PS2=
RPS2='%^'
source $PWD/zsh-autocomplete/zsh-autocomplete.plugin.zsh
mkdir -p aaa_1/bbb aaa_2
touch aaa_1/test_1.txt aaa_1/test_2.txt aaa_2/test_1.txt aaa_2/test_2.txt
zstyle ':autocomplete:*' insert-unambiguous yes
zstyle ':autocomplete:*' widget-style menu-select
bindkey '\t' menu-select "$terminfo[kcbt]" menu-select
bindkey -M menuselect '\t' menu-select "$terminfo[kcbt]" reverse-menu-complete
EOF
% env -i HOME=$PWD PATH=$PATH TERM=$TERM ${TERMINFO:+TERMINFO=$TERMINFO} zsh -d
% ls ~/a
expansion
/var/folders/1g/c3dw8nsj7gs0d1dlsb3gcw340000gn/T/tmp.pcKQSBX21Z/a
directory
aaa_1/                           aaa_2/

So the first suggestion is an expansion of ~ followed by the portion of the subdirectory. Because the expansion takes priority, it takes two tabs to get to the first directory completion instead of one. The problem repeats, as if I accept ~/aaa_1 by typing '/', the new first completion is again the tilde expansion, and it's the full path of aaa_1, the folder I've already entered!

% ls ~/aaa_1/
expansion
/var/folders/1g/c3dw8nsj7gs0d1dlsb3gcw340000gn/T/tmp.pcKQSBX21Z/aaa_1/
directory
bbb/
file
test_1.txt                          test_2.txt

This means that to efficiently complete ~/* paths, I must first expand the ~, which clutters up the terminal and requires extra keystrokes.

Contents of ~autocomplete-log/YYYY-MM-DD.log (click to expand)

Potential solution

Using bisection, I traced the issue to commit 0643189.

The solution I came up with was to expand the first path component of $word if it begins with ~, before comparing with the full $expansion.

  expansion="${(b)$( eval print -r -- $word )}" 2> /dev/null ||
      return 1
+ if [[ $word == \~* ]]; then
+    word="${(b)$(eval print -r -- ${(M)word#*/})}${(b)word#*/}" 2> /dev/null ||
+         return 1
+ fi
  [[ $expansion == $word ]] &&
      return 1

It feels a bit hack-y but I'm not sure if there's a better way, such as directly disabling tilde expansion in eval or something.

Note that due to the way I used patterns here, completing ~ still suggests the expansion into $HOME as the first result, it just goes away for ~/ or ~/a... and beyond. Seems like a useful middle ground for my purposes.

See this commit for the diff: aaronkollasch@da40b54

@aaronkollasch
Copy link
Contributor Author

Note that there is a related issue involving expansion suggestions for backslash-escaped words/paths as well as quoted words. This interferes with completing paths containing spaces, as the first suggestion is always to remove the escapes (which would break the path).

@thearrow
Copy link

thearrow commented Aug 2, 2024

Your fix works for me as well - was getting annoyed by this. Thanks!

@4rtemis-4rrow
Copy link

@aaronkollasch I was about to open an issue for that, I was just checking if there is already an issue for this?

is there any temporary fix? a lot of my paths contain spaces, so it's getting very annoying

@aaronkollasch
Copy link
Contributor Author

@4rtemis-4rrow Here's a temporary fix for the spaces issue - It's not perfect in covering all the edge cases that could happen with quotations and escapes, but it does reduce the annoyance level: aaronkollasch@d53d90d

@jace
Copy link

jace commented Dec 11, 2024

Looks like expansions can be disabled in config as per #759:

# Leave out _expand
zstyle ':completion:*' completer _complete _complete:-fuzzy _correct _approximate _ignored

@aaronkollasch
Copy link
Contributor Author

I find expansions useful enough that I want to keep it, but that is another solution if you don't.

@poopsicles
Copy link

@aaronkollasch kinda off-tangent, but what else, apart from ~, gets expanded?

i switched to your fork for a bit because i didn't want to miss out on them,,,but i just tried this and....it looks fine...maybe i'm just not noticing something i use is gone..

@aaronkollasch
Copy link
Contributor Author

@poopsicles The other things are expanding glob expressions (*) into multiple filenames and expanding expressions such as parameters. You can search https://zsh.sourceforge.io/Guide/zshguide06.html for a description of _expand, though keep in mind that zsh-autocomplete replaced it in 0643189.
I definitely don't use it as much as the main completion categories, so it's easy to see how it wouldn't be missed... but I find it occasionally useful.

@poopsicles
Copy link

@aaronkollasch yesssss, globs how could i forget lolll they're so useful, i use them all the time

thanks! (both for your fork with a workaround and the explanation)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants