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

grep drop-down auto-complete is not behaving properly when pattern is not defined yet #896

Closed
Pouya-moh opened this issue Jun 2, 2015 · 5 comments

Comments

@Pouya-moh
Copy link

I switched to prezto just today so I'm not sure if this behavior is a bug or it's by design. When I do grep - and tab I have

~ >>> grep -
-- pattern --
-- option --
--after-context    -A     -- specify lines of trailing context
--basic-regexp     -G     -- use basic regular expression
--rest of arguments

I expect with double tab the cursor jumps to the options and I be able to navigate options with arrow keys. But this doesn't work. Instead if I do grep 'my pattern' - and double tab, now it behaves the way that I expect. Note that in this case there is no empty group for -- pattern --.

I'm used to provide the options first, and then the pattern. Is there any work around?

@sorin-ionescu
Copy link
Owner

Check modules/completion/init.zsh for a line that may be causing it.

@zmwangx
Copy link
Contributor

zmwangx commented Jun 3, 2015

I see nothing unexpected here. If you take a vanilla Zsh without Prezto, run autoload -Uz compinit && compinit -i to initialize the completion system, and do grep -<TAB><TAB>, you'll basically get the same effect, except that you might be prompted for whether to list completions, and you probably won't see the descriptions -- pattern -- and -- option --.

If you look at the definition of _grep), the basic arguments to _arguments are (L12-15)

  arguments=(
    '(-e --regexp -f --file)1: :_guard "^--*" pattern'
    '*:files:_files'
  )

Along with a whole lot of optspecs.

So, as you can see, '(-e --regexp -f --file)1: :_guard "^--*" pattern' tells you that your grep pattern:

  • is the first positional argument (1 means first and first only);
  • is incompatible with the -e, --regexp, -f, and --file options;
  • its message is empty (the blank in : :); in fact, the description is added by _guard. And
  • its associated action is _guard "^--*" pattern.

And

_guard "^--*" pattern

insists that the pattern matches "^--*", i.e., doesn't start with --. Now:

  • If you type grep -<TAB>, - matches "^--*" so it is considered a valid pattern, hence you still get -- pattern --; but _guard can't give you suggestions, so you can't tab complete or cycle.
  • If you type grep --<TAB>, then -- matches "^--*" and _guard returns a nonzero status immediately, so you are only left with -- options --, where _arguments knows all the options and will let you cycle through with tab.
  • If you type grep 'whatever patten' -, as I said before the pattern is the first and first only argument; since the slot is already consumed, all you are left with are the -- options --.

More information in man zshcompsys or the HTML doc.


Now it's a separate and perhaps interesting question as to why the pattern in _guard is written as "^--*", as opposed to "^-*". Honestly I can't think of a reason why a pattern should be allowed to begin with - (before --, which terminates the completion of options due to the -S flag to _arguments). Maybe it's a bug, maybe it's for compatibility with some paranoid grep implementation, or maybe I have a blind spot. Anyway, I don't think this is a Prezto problem, and we should probably ask on Zsh's IRC or mailing list.

@zmwangx
Copy link
Contributor

zmwangx commented Jun 3, 2015

By the way, here's a screencast demonstrating essentially the same behavior with or without Prezto:

asciicast

@Pouya-moh
Copy link
Author

Interesting. I migrated from oh-my-zsh to prezto so perhaps I'm used to a different behavior.

@sorin-ionescu
Copy link
Owner

Thank you, @zmwangx.

0xbzho pushed a commit to 0xbzho/asciinema.org-2015-06 that referenced this issue Mar 1, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants