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

Add support for alias commands #1354

Closed
gvariable opened this issue Jul 28, 2023 · 6 comments
Closed

Add support for alias commands #1354

gvariable opened this issue Jul 28, 2023 · 6 comments

Comments

@gvariable
Copy link

Issue Description

I would like to propose adding support for alias commands in fd. Alias commands would allow users to define and use custom shortcuts for commonly used search patterns or command-line options without having to retype them each time. Unfortunately, an error occurs stating Command not found when attempting to type a command such as fd -x ll.

Proposed Solution

Alias commands can be obtained by executing the alias command. Here is an illustrative example output retrieved from my MacBook:

ll='ls -lh'
ls=lsd
lsa='ls -lah'
-='cd -'
sudo='nocorrect sudo'
typst=~/Downloads/typst/bin/typst
cat=bat
grep='rg --color=auto'
gtv='git tag | sort -V'  # complicated case
gtl='gtl(){ git tag --sort=-v:refname -n --list "${1}*" }; noglob gtl' # very complicated case
...

Although the alias can be complex, allowing the aliasing of multiple commands and even incorporating some shell features, the commonly used cases are often quite simple. These involve setting default options or command substitutions, such as ll='ls -l' or cat='bat'.

The most straightforward approach would be to utilize a hashmap data structure to create a mapping for aliases, thereby storing the aliases for simple cases. When a user enters a command, the program would first check the hashmap for a matching command. If a match is found, it would construct the corresponding Command and its associated args, and then execute it accordingly.

I believe that adding alias support to fd would greatly enhance its usability and provide a more refined and enhanced experience for users. Thank you for considering this feature request.

@tavianator
Copy link
Collaborator

Alias commands can be obtained by executing the alias command

True, but only from within a shell, there is no alias command:

$ type alias
alias is a shell builtin
$ command alias
zsh: command not found: alias
$ zsh -c alias # You have to invoke the shell to run the builtin
run-help=man
which-command=whence
$ zsh -ic alias # -i (interactive) is probably necessary to read the .*shrc that defines your aliases
ll='command ls -l --color=auto -v'
...

So fd could try to parse aliases if it knew what shell to use, but you could just invoke it directly:

$ fd png -x ll
[fd error]: Command not found: ll
$ fd png -x zsh -ic 'll "$1"' zsh
-rw-r----- 1 tavianator tavianator 10183 Oct 10  2022 ./doc/logo.png

Running an interactive shell is expensive so you probably want to use -X instead:

$ fd md -X zsh -ic 'll "$@"' zsh
-rw-r----- 1 tavianator tavianator 26976 May  3 08:54 ./CHANGELOG.md
-rw-r----- 1 tavianator tavianator  1639 May  3 08:54 ./CONTRIBUTING.md
-rw-r----- 1 tavianator tavianator 25176 May  3 08:54 ./README.md
-rw-r----- 1 tavianator tavianator  2543 Nov 25  2022 ./doc/release-checklist.md

@gvariable
Copy link
Author

gvariable commented Jul 30, 2023

Thank you for your advice on how to use aliases in the current version of fd. It has been very helpful!

So fd could try to parse aliases if it knew what shell to use

To my understanding, the SHELL environment variable represents the login shell. Matching its value might be helpful.
Unfortunately, there are situations where even if my login shell is SHELL-A, if I invoke a SHELL-B subprocess, the SHELL environment variable will not change. However, I believe this is a rare occurrence.

@tmccombs
Copy link
Collaborator

The bigger problem is that fd would have to know how to list the aliases for various shells (it is different for different shells) and how to parse the format of the output, which is again different for different shells. And then there are the aliases themselves. Different shells have different syntax for quoting/escaping that fd would have to understand. And what about aliases that use more complicated syntax? And we probably don't want to start an interactive terminal every time fd runs to parse aliases.

Perhaps a better option would be to have separate options for using am interactive shell to run the command. But even then, I don't think it is worth the effort, since there is a workaround. Maybe we should just add more documentation about it?

FWIW, fd is not unique in having this problem. Aliases also don't work with sudo, xargs, find, parallel, doas, etc. I don't know of any other program that does parse aliases like this.

@tavianator
Copy link
Collaborator

For the record I really don't think fd should parse aliases. But a more convenient version of -x $SHELL -c '<command> "$@"' $SHELL might be useful, e.g. --exec-shell ll or something

@sharkdp
Copy link
Owner

sharkdp commented Oct 21, 2023

For the record I really don't think fd should parse aliases. But a more convenient version of -x $SHELL -c '<command> "$@"' $SHELL might be useful, e.g. --exec-shell ll or something

I like that idea 👍

@tmccombs
Copy link
Collaborator

I created an issue for an --exec-shell option in #1406

I'm going to close this in favor of that, unless someone comments otherwise.

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