How to create a alias in fish shell

6.6k views Asked by At

I try to create an alias for ls (should basically just map to ls -lah) I've tried the following code, but it's not working:

function ls

ls -lah

end

funcsave ls

but when I call it I get this message:

The function 'ls' calls itself immediately, which would result in an infinite loop. in function 'ls' called on standard input

3

There are 3 answers

2
keramzyt On BEST ANSWER

If you need to make alias of ls, then the above answers are OK. However, fish already has a command for ls -lah, which is la.

0
glenn jackman On

You need the command keyword. Also, pass the function's arguments to ls

function ls
    command ls -lah $argv
end
4
oranja On

What you're looking for is the command command.

I would also recommend to pass any arguments (stored in $argv) to the aliased command.

So your example should be:

function ls
  command ls -lah $argv
end

And to do all this with a simple command, you can simply use the alias command.

alias ls "command ls -lah"

Note that usually aliases will not get you the nice auto-complete suggestions that contribute to _fish_'s friendliness. This specific case is an exception because the function and the original command have the same way, but otherwise, here are two ways to get completions anyway:
  • You can use the complete command to tell fish that your alias uses the same completions as the aliased command.
    The balias plugin serves as an alternative to alias and does just that.

  • fish also offers an abbr command. It works slightly different and will actually expand the abbreviated command to the full command in the command line, and then fish will have no problem giving you all the auto-completion suggestions that it knows.