mrjohannchang / zsh-interactive-cd

Fish like interactive tab completion for cd in zsh
Mozilla Public License 2.0
314 stars 37 forks source link

How to apply the same functionality to its aliases? #6

Open vimkim opened 6 years ago

vimkim commented 6 years ago

for example, if I have an alias cl: alias cl='cd $@; ls -a' how can I apply the same functionality to 'cl', i.e. fzf pops up when pressing after $ cl?

doronbehar commented 6 years ago

Usually when a command needs to be completed with the same completion just like a different command, you use compdef _cd cl but it doesn't work since is not a completion plugin but a zle widget. As far as I understand the code, the widget is defined with check for cd hard-coded when the widget is defined. Try this:

zic-completion-cl () {
    setopt localoptions noshwordsplit noksh_arrays noposixbuiltins
    local tokens cmd
    tokens=(${(z)LBUFFER}) 
    cmd=${tokens[1]} 
    if [[ "$LBUFFER" =~ "^\ *c[ld]$" ]]
    then
        zle ${__zic_default_completion:-expand-or-complete}
    elif [[ "$cmd" =~ "c[ld]" ]]
    then
        _zic_complete ${tokens[2,${#tokens}]/#\~/$HOME}
    else
        zle ${__zic_default_completion:-expand-or-complete}
    fi
}
zle -N zic-completion-cl
bindkey "^I" zic-completion-cl

I have just created a sister function to zic-completion like the origin and I replaced the hard-coded conditions that check the current command line arguments against cd so cl will be checked as well.

Having this kind of customization ability built in with the code should require more work but you can use this as a workaround in the meantime..

balta2ar commented 5 years ago

@doronbehar Is there an easy way to generalize this and replace zsh's completion menu with a menu implemented on top of fzf for any command that uses zsh menu? fzf is so great that I'd like to use it in every command that can display a menu with candidates. Is that archievable in zsh?

doronbehar commented 5 years ago

Well TBH, I stopped using zsh-interactive-cd after I realized what it does and after I figured out how to produce it's functionality it along with what the features the other ZSH related files that https://github.com/junegunn/fzf offers (key-bindings.zsh and completion.zsh) under it's own domain.

Thanks for being interested in this subject. To be frank, I'm really proud of my ZSH fzf related configuration and that's why I wrote an article about it in my website: https://doronbehar.com/articles/ZSH-FZF-completion/ .

balta2ar commented 5 years ago

Yes, my question is not really related to this repo specifically, but it's related to the topic of the issue.

I've read your post. Franky, I have shallow understanding of how zsh (and zle) works and I may be wrong, but you seem to be doing pretty much similar things which are described in fzf wiki, e.g.: https://github.com/junegunn/fzf/wiki/Examples-(completion)#zsh-complete-hg-updatehg-merge

With this approach the downsides are:

  1. you need to have a wrapper for each command that you want to power up with fzf
  2. it gets uglier when you want to support subcommands (e.g. hg up, hg merge)
  3. I guess it gets even worse than that if you want to support flags/arguments (e.g. hg diff --rev <cursor>)

I was wondering whether fzf could be plugged everywhere transparently? What I mean is that interactive menu completion in zsh can be enabled as follows:

zstyle ':completion:*' menu select interactive

I wish there were some alternative with fzf, e.g. menu select fuzzyinteractive which would call fzf or exhibit similar visual behavior without any additional bindings but with the default TAB. I'm afraid that to do that, one would need to go down the rabbit hole and implement it in zsh (in C).

doronbehar commented 5 years ago

I guess you are right. I think that this kind of integration might be worth the effort to be merged into the main ZSH repository. But I assume that it would require a lot of work to make it safe and configureable even for users of other fuzzy selection programs such as https://github.com/jhawthorn/fzy for example or other alternatives.

Just for the sake of clearance for everyone who may read this discussion: I think the main reason a fuzzy selector might be even more usable if used from within the completion system of ZSH, is that currently, using the widgets mechanism of ZSH, the interpretation of what available options there are for completion is done separately from the completion system. And considering how much effort has been put to make command completions extremely useful (take git's completion for example) it would be insane to re-implement it in separate functions binded to widgets that they collect the available options of completion and just launch fzf (and not the _complete function for example). Therefor it would be much better to make the existing completion functions launch fzf if available and if it is configured with zstyle.