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

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

Open
vimkim opened this issue Sep 22, 2018 · 5 comments
Open

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

vimkim opened this issue Sep 22, 2018 · 5 comments

Comments

@vimkim
Copy link

vimkim commented Sep 22, 2018

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
Copy link
Contributor

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
Copy link

@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
Copy link
Contributor

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
Copy link

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
Copy link
Contributor

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.

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

4 participants