2

I'm trying to shift to fish from zsh. As I've seen, load time of fish increases if I use alias. So I'd like to convert all of my alias into functions, but the thing is creating a function script for every alias is a bit hectic. So how do I add those alias in a single function script?

2 Answers 2

6

First of all, the alias command in Fish is simply a shortcut to create a function with a little syntactic sugar. E.g.

> alias ls "ls -lh"
> functions ls
# Defined via `source`
function ls --description 'alias ls ls -lh'
 command ls -lh $argv;
end

Perhaps it's the overhead of the functions being created/loaded that is creating your slowdown. In that case, putting them all into a monolithic file that is (still) loaded at startup likely isn't going to help.

There are several simple solutions that don't even involve modifying your config.fish:

  • For each alias, simply funcsave <aliasname>. This will save the function to ~/.config/fish/functions. This is really the right way to do it, IMHO, since functions in that directory are loaded on demand, not at startup. This will give you the fastest shell start-up time.

    For example:

    > alias ls "ls -lh"
    > funcsave ls
    > ls -n ~/.config/fish/functions/
    -rw-r--r-- 1 1000 100  95 Aug 18 14:30 ls.fish
    

    Again, this file isn't even touched at startup -- It's "lazy loaded" when needed. When you call ls for the first time in a session, Fish will execute the ls function in ~/.config/fish/functions/ls.fish.

    Also, it's only necessary to funcsave on existing aliases. When defining a new alias, just use alias -s to automatically create the lazy-load function file in ~/.config/fish/functions.

  • Edit/Update: You mentioned wanting this to be as easy as possible, so here's a quick command-line that can automate the saving of all of your alias functions. Once run, you should see the corresponding file for each alias in ~/.config/fish/functions and you can remove the existing aliases from your startup.

    for f in (alias | awk '{print $2}'); funcsave $f; end
    
  • If you really do want to create one monolithic file for your alias functions, and you don't mind the increased startup time (regardless of how much or little it is), then I still wouldn't modify ~/.config/fish/config.fish. Just create that file in ~/.config/fish/conf.d and it will be read as part of the startup automatically.

    Whenever some instructions say to modify your config.fish, consider using a file in conf.d instead. It allows you to more easily see (and modify) what each part of your startup config is doing, without mucking through a single config.fish.

4

Based this Stack Overflow example by ridiculous_fish, I would suggest creating a file under ~/.config/fish/ with the functions defined in them, then add a line to source that file in your ~/.config/fish/config.fish file.

Example contents of ~/.config/fish/all-my-functions.fish:

function example-function1
  ls -l $argv
end

function example-function2
  ls -a $argv
end

# continues as needed ...

and the line for ~/.config/fish/config.fish:

source ~/.config/fish/all-my-functions.fish

Alternatively, you could define all of the functions directly into your config.fish file.

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .