Reputation: 2637
Question: Is there a Fish equivalent of Bash's alias substitution or what's the recommended best practice to keep the code clean and DRY?
Background: There's a very useful feature of aliases in Bash called alias substitution. It's mentioned briefly in the man page:
alias [-p] [name[=value] ...]
...
A trailing space in value causes the next word to be checked for alias substitution when the alias is expanded.
...
The power of this functionality may easily be conveyed with an example. Consider that many users define a grep alias. Here's mine:
# extended regex, skip binaries, devices, sockets, & dirs, colored, & line
# -buffered. use a non- canonical alias instead of GREP_OPTIONS which may wreck
# poorly written scripts
alias g='grep -EID skip -d skip --color=auto --line-buffered'
Similarly, many of those same users define an alias for xargs. Here's mine without alias substitution:
alias x='xargs -rd\\n' # \n delimited, don't run on empty in
And finally, here's how I might want to use it but it doesn't work:
$ find|x g foo
xargs: g: No such file or directory
This command fails because x is expanded to xargs and it can't find an executable called g
. There are a number of workarounds for this but I think most are awful. However, by just adding a trailing space, the shell will perform alias substitution on our behalf and the command will work as intended:
alias x='xargs -rd\\n ' # \n delimited, don't run on empty in, + expansion
# ^-- this space is for expanding a subsequent alias
Please keep in mind that this is just an example, not necessarily the actual use case.
Update 2015-05-06
I never found a Fishism solution but I felt the alternative was worth a comment. I took the approach of creating shell scripts in ~/bin
. The downsides are:
However, I felt the upsides were pretty huge:
Upvotes: 4
Views: 1578
Reputation: 3191
This isn't a Fish based solution -- but I suspect the fish answer is going to be it's not possible.
You could create your aliases as .fish
or .sh
scripts and symlink them to /usr/local/bin
-- this will give you the equivalent behaviour.
Upvotes: 3