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
Picking up aliases and functions #73
Comments
|
Hey Peter, It sure would be handy. If you want to contribute that feature I will Bash can Separately from that I don't know how to better educate the user to make
|
|
A few things: I like this answer which explains how to get the bash I think it may be cool to allow function templating in an includes directory for fish and zsh, but this begs the question of whether those functions are then really part of a shell package and should be distributed some other way. The annoying thing is that (to the best of my knowledge) there is no such thing as a package manager for shell packages, let alone a shell-agnostic shell package manager. I feel like any alias or function we would put in a .envrc file should really just be a function available to the whole system that takes configuration from variables that direnv (or anything else) could set. Is there a use case we can think of that falls outside of this? If I'm correct, then the only directive we need to add to dotenv is the 'source' directive, which should be to our shell packages as bundler is to ruby packages, but without the update and install functionality because, again, we don't actually have a package manager or package repository. |
|
Hey @winmillwill , I think the idea was that in the context of a project you might want to add some shortcuts as functions or aliases that are really specific. Let's say you have a ruby project, you could set That being said I think that in most cases aliases and functions can be replaced by proper bash scripts that's references using the |
|
Some of these tickets are also placeholders for some ideas, like here I'm not super convinced it's useful but I could be convinced otherwise. Ideas are best when simmered :) |
|
All of that makes sense. It's just a style point for me that something like I can't claim to understand the indirection you discussed about running all On Mon, Dec 2, 2013 at 6:45 AM, zimbatm notifications@github.com wrote:
|
|
Yeah there's so many ways to do the same things but only few are good :)
When you run It's a bit complex but that way the .envrc is always executed in bash and you don't have to worry about shell compatibility. Any function or others defined in the sub-process don't leak into the current environment (which coincidentally also prevents this feature). |
|
I'm going to close this one since there hasn't been much interest and I haven't missed it much myself. But if there is demand, please open up another issue. Even better if you can suggest ways to implement it. |
|
I would use this feature. At the very least support for |
|
+1! I could definitely use this. I've been using my own little |
|
+1 for |
|
Unfortunately BASH_FUNC_* environment variables cause issues for other shell so we had to ignore them. See #157 If you want to maintain all your aliases in the .envrc you could add something like that in your main ~/.direnvrc file: export_function() {
local name=$1
local alias_dir=$PWD/.direnv/aliases
mkdir -p "$alias_dir"
PATH_add "$alias_dir"
local target="$alias_dir/$name"
if declare -f "$name" >/dev/null; then
echo "#!/usr/bin/env bash" > "$target"
declare -f "$name" >> "$target" 2>/dev/null
echo "$name" >> "$target"
chmod +x "$target"
fi
}then in your .envrc you can write something like: woot() {
echo woot
}
export_function wootAn executable file is then automatically generate on each .envrc load inside of the project's .direnv/aliases folder and made available on the PATH. I suppose a similar |
|
It seems that the complexity is mostly related to function exports. Exporting aliases seems easier, at least on bash and ksh, where the |
👍 I could definitely use this feature. Generally speaking, exporting aliases is a per project setting in the majority of cases; they would be rarely useful as global settings. This feature will allow me to declutter a lot of junk-aliases and hacks from my global namespace, hence greatly speeding up the bootstrap of all my virtual consoles (which could took quite a toll on terminal multiplexers with a very large number of connections). |
|
@wbolster it seems easy on the surface but I don't know how if it will work for all shells. It also means to re-write the whole context serialisation to include the aliases. @Dr-Terrible it's a bit of a hack but you can add this to your ~/.direnvrc: # Example: export_alias zz "ls -la"
export_alias() {
local name=$1
shift
local alias_dir=$PWD/.direnv/aliases
local target="$alias_dir/$name"
mkdir -p "$alias_dir"
PATH_add "$alias_dir"
echo "#!/usr/bin/env bash -e" > "$target"
echo "$@" >> "$target"
chmod +x "$target"
} |
|
I've prototyped another version of In addition, a function Only zsh version is prototyped by now, I guess it could be ported to bash. I imagine that $ direnv export zsh
_direnv_notify_preunload '/path-to/prev-dir' '/path-to/next-dir'
_direnv_log_status 'loading /path-to/next-dir'
_direnv_log_diff '+VAR1' '~VAR2'
export VAR1=value; export VAR2=value;
_direnv_notify_postreload '/path-to/prev-dir' '/path-to/next-dir'That would even allow for better customization of log messages (#68, #104, #219). |
|
We should decide whether |
|
Hi, i did some modification with this. export_alias() {
local name=$1
shift
local alias_dir=$PWD/.direnv/aliases/$(pwd)
local target="$alias_dir/$name"
mkdir -p "$alias_dir"
PATH_add "$alias_dir"
echo "#!/usr/bin/env bash" > "$target"
echo "$@ \"\$@\"" >> "$target"
chmod +x "$target"
}How can I make direnv remove the pwd's aliase directory when eval so that it won't conflict with the new one? |
|
You could alias_dir=$PWD/.direnv/aliases
rm -rf "$alias_dir"
export_alias() {
local name=$1
shift
local target="$alias_dir/$name"
mkdir -p "$alias_dir"
PATH_add "$alias_dir"
echo "#!/usr/bin/env bash" > "$target"
echo "$@ \"\$@\"" >> "$target"
chmod +x "$target"
} |
|
@zimbatm thanks! |
should be set in the |
|
Hmm, isn't |
|
for those of you using zsh you might like to try this technique: |
|
I would like to suggest a minor enhancement for above export_function() {
local name=$1
local alias_dir=$PWD/.direnv/aliases
mkdir -p "$alias_dir"
PATH_add "$alias_dir"
local target="$alias_dir/$name"
if declare -f "$name" >/dev/null; then
echo "#!$SHELL" > "$target"
declare -f "$name" >> "$target" 2>/dev/null
# Notice that we add shell variables to the function trigger.
echo "$name \$*" >> "$target"
chmod +x "$target"
fi
}So, the exported function could pickup shell variables, and use user default shell. |
|
I got aliases working for fish shell using a lib function and a modified hook. It could easily be extended to support other shell. Overview:
I added this to my And I'm using this for my fish hook. This an update from the hook I submitted in PR #732 but the alias logic is in the __direnv_update function. |
|
I like the postload and preunload hook idea. Does that exist already? If so, it can be used to cover many scenarios. For example, something like: The above is for |
|
FYI, I've come up with a solution that works with In In In |
I noticed this adds multiple instances of the alias path to the PATH for every export_alias you have, so I tweaked it to check the PATH... export_alias() {
local name=$1
shift
local alias_dir=$PWD/.direnv/aliases
local target="$alias_dir/$name"
mkdir -p "$alias_dir"
if ! [[ ":$PATH:" == *":$alias_dir:"* ]]; then
PATH_add "$alias_dir"
fi
echo "#!/usr/bin/env bash -e" > "$target"
echo "$@" >> "$target"
chmod +x "$target"
} |
|
Is there any update on this? What is the current recommended solution? |
|
After years of using direnv, this must be the feature I miss the most. Having simple aliases and functions to simplify tedious development tasks is so nice, for example: Are there any known plugins for direnv that allows it to export functions and aliases for ZSH? Is the plugin API powerful enough that I could implement this myself? |
|
Hi, I'm using fish shell together with nix-direnv (nix flake). I wonder if the work around mentioned by @wderezin still work since there is no such command |
@sandangel I just upgraded to v2.31.0 and my work around is still working for me. Can you provide a reference to where the export command was deprecated so I can research this? |
|
I just saw there is no such |
|
@sandangel Yeah, it's not documented, but it definitely still exists. |
|
I have a public repo you can look through. I use it so I can easily install my different shell environments and scripts on any system I use. https://github.com/wderezin/cli-tools But in summary: I have a functions directory that the hook lives in. Then during fish init I add the functions directory to the path, and init the hook. Links to the code parts:
Hope that helps. |
|
@wderezin hmm, it doesn't work for me. I run the export_alias tf "terraform"in |
For those who rely on an alias to add arguments at the end # without direnv
alias greet="echo hello"
# with direnv
export_alias greet 'echo hello $@'PS: I tried adding it into the script first |
|
Thanks everyone for their helpful notes on workarounds. Especially #73 (comment) (and all previous iterations) were extremely helpful. In my case I need to alias an existing command to add some extra flags to it, which caused an infinite loop. However, explicitly setting PATH back to the old path in export_alias worked for me: export_alias() {
local name=$1
shift
local alias_dir=$PWD/.direnv/aliases
local target="$alias_dir/$name"
local oldpath="$PATH"
mkdir -p "$alias_dir"
if ! [[ ":$PATH:" == *":$alias_dir:"* ]]; then
PATH_add "$alias_dir"
fi
echo "#!/usr/bin/env bash" > "$target"
echo "PATH=$oldpath" >> "$target"
echo "$@" >> "$target"
chmod +x "$target"
}export_alias ansible-playbook 'ansible-playbook -e ara_playbook_labels=$(whoami) $@'A native solution would still be great, but hopefully this little extra note helps others :) |
Hello, In case you are using Otherwise, you will have this kind of error when using Windows path: Kind regards, |
|
as this is still open after centuries.... devenv handles this quite simply easily integrates with direnv for per repo/folder dev shell setups |
AFAICT, devenv's |
Turns out direnv does not support aliases and functions: direnv/direnv#73
|
@lindhe unless you actually found a working alternative, posting messages like this is just noise for people watching this issue |
|
It might be useful for the devs to know that this drives people away from their (otherwise excellent) software. |
|
Sorry for the noise, but I want to write a plugin that solves this. I basically want to do what @zimbatm's suggestions/hacks does, but transparently and automatically. I can use the The goal is: Source the plugin script in my However, I'm not sure how to only find functions defined during the execution of Does anybody have any good suggestions? Is there a hook I can use that executes at the start of |
@zimbatm, what about keeping all aliases in |
|
@giner , that's more-or-less what I do. More specifically, I define the env var |
Just come across a case where someone was expecting functions and aliases to be picked up from the source script.
I realise what a pain this is. Any way we can reasonably make that work?
The text was updated successfully, but these errors were encountered: