I would like to define some aliases in fish. Apparently it should be possible to define them in

~/.config/fish/functions

but they don't get auto loaded when I restart the shell. Any ideas?

share|improve this question
    
The answer down below with only a couple votes is, in my opinion correct. Autoloading in ./config/fish/functions only seems to work if there is one function per file. – mc_electron Aug 11 '15 at 13:52
up vote 177 down vote accepted

Just use alias. Here's a basic example:

# Define alias in shell
alias rmi "rm -i"

# Define alias in config file
alias rmi="rm -i"

# This is equivalent to entering the following function:

function rmi
    rm -i $argv
end

# Then, to save it across terminal sessions:

funcsave rmi

Interested people might like to find out more about fish aliases in the official manual.

share|improve this answer
2  
Also note that in fish an alias is just a shellscript wrapper around a function. – user456584 May 6 '13 at 21:09
6  
e.g., alias b="bundle exec" – Peter Ehrlich Nov 25 '13 at 23:17
    
@user456584 ...and that's to bad, because you can't do alias local "set -l" ;-( – nus Apr 18 '14 at 13:43
3  
This isn't correct- better to create the alias with alias or function, then funcsave it. – Lucretiel Oct 26 '15 at 14:57
4  
Better mention funcsave rmi afterwards. – lk_vc Mar 14 '16 at 6:55

This is how I define a new function foo, run it, and save it persistently.

sthorne@pearl~> function foo
                    echo 'foo was here'
                end
sthorne@pearl~> foo
foo was here
sthorne@pearl~> funcsave foo
share|improve this answer
    
Thanks Jerub. For some reason after funcsave it gave me an error: troff: fatal error: can't open `/usr/share/fish/man/save_function.1': No such file or directory – armandino May 4 '10 at 5:41
5  
You can also edit functions using funced. E.g. funced foo. – Dennis May 22 '13 at 14:56
11  
Running funcsave {some_function_name} will save it in ~/.config/fish/functions/{some_function_name}.fish – Hjulle Aug 22 '14 at 8:48

For posterity, fish aliases are just functions:

$ alias foo="echo bar"
$ type foo
foo is a function with definition
function foo
    echo bar $argv; 
end

To remove it

$ unalias foo
/usr/bin/unalias: line 2: unalias: foo: not found
$ functions -e foo
$ type foo
type: Could not find “foo”
share|improve this answer
5  
After you create the function, you can do funcsave foo. This will save the function into ~/.config/fish/functions/foo.fish, where fish will automatically load it the first time it is called from a new session. – Lucretiel Oct 26 '15 at 14:58
    
alias foo="echo bar" was by far the easiest solution – Ronnie Jan 26 '16 at 18:33
  1. if there is not config.fish in ~/.config/fish/, make it.
  2. there you can write your function .function name command end
share|improve this answer

Save your files as ~/.config/fish/functions/{some_function_name}.fish and they should get autoloaded when you start fish.

share|improve this answer

To properly load functions from ~/.config/fish/functions

You may set only ONE function inside file and name file the same as function name + add .fish extension.

This way changing file contents reload functions in opened terminals (note some delay may occur ~1-5s)

That way if you edit either by commandline

function name; function_content; end

then

funcsave name

you have user defined functions in console and custom made in the same order.

share|improve this answer

make a function in ~/.config/fish/functions called mkalias.fish and put this in

function mkalias --argument key value
  echo alias $key=$value
  alias $key=$value
  funcsave $key
end

and this will create aliases automatically.

share|improve this answer

fish starts by executing commands in ~/.config/fish/config.fish. You can create it if it does not exist.

step1. make configuration file (like .bashrc)

config.fish

step2. just write your alias like this;

alias rm="rm -i"

share|improve this answer

Open vim .config/fish/conf.d/omf.fish, bind your bash defines and restart the fish terminal.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.