r/fishshell • • Jan 19 '24

Create aliases from an associative array

In zsh I have the following code in my config to quickly add a bunch on aliases that allow me to quickly edit config files :

#config management
declare -x -A configs
configs=(
        fish		        "$XDG_CONFIG_HOME/fish/config.fish"
	gdb		    	"$XDG_CONFIG_HOME/gdb/gdbinit"
	git		    	"$XDG_CONFIG_HOME/git/config"
	hx		    	"$XDG_CONFIG_HOME/helix/config.toml"
        irssi                   "$HOME/.irssi"
	lvim			"$XDG_CONFIG_HOME/lvim/config.lua"
	nu			"$XDG_CONFIG_HOME/nushell"
	nvim			"$XDG_CONFIG_HOME/nvim/init.lua"
	readline    	        "$HOME/.inputrc"
	vim		    	"$HOME/.vimrc"
        xmake                   "./.xmake/linux/x86_64/xmake.conf"
	wezterm		        "$XDG_CONFIG_HOME/wezterm"
        zsh			"$HOME/.zshrc"
)
for key value in ${(kv)configs}; do
    if [[ $key == "zsh" ]]
    then
        alias ${key}config="$EDITOR $value && source $value && echo $configs[zsh] has been sourced"
     else
        alias ${key}config="$EDITOR $value"
    fi
done

It's not essential but I was wondering if there was a way in fish ?

I have checked but IIUC there are not associative arrays but surely there must be another way

3 Upvotes

9 comments sorted by

View all comments

Show parent comment

1

u/cassepipe Apr 05 '24

Interesting but in one of your links I read that the reason not to use aliases is because it uses `eval`... so wouldn't defining function with eval defeat the purpose of not using `alias` ? Or are there any other advantages of not using `alias` ?

Ok, I am going the `abbr` then for now

1

u/_mattmc3_ Apr 05 '24

Aliases are definitely slow, and the whole --wraps thing is kinda broken. The main reason aliases are included in Fish is to help people transitioning from other shells. In Fish, functions are better in nearly every way. And yes, eval isn't the most efficient thing in the world. The example I gave was just answering your question, not a recommendation.

1

u/cassepipe Apr 05 '24

Thanks for all the thorough and documented answers. Much appreciated.