Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mac how to save alias in computer

I created an alias on terminal, such as

alias hw="cd Desktop/2015hw"

but after I closed the terminal and do hw, there is an error message said there is no hw command.

I was wondering if we can save the alias on the computer. Also, if we have stored the alias on the computer, how would we check it? By check it, I mean like list all alias we have stored. Thanks.

like image 278
Big_t boy Avatar asked Apr 22 '15 07:04

Big_t boy


3 Answers

Add the command to your .bashrc file

echo "alias hw='cd Desktop/2015hw'" >> ~/.bashrc

Keep in mind that your alias will only work when you are in your home (as you are using a relative path)

echo "alias hw='cd ~/Desktop/2015hw'" >> ~/.bashrc

Execute the saved alias by sourcing the file

source ~/.bashrc
like image 123
Matteo Avatar answered Oct 20 '22 13:10

Matteo


When making an alias with alias, the alias is only valid until you close the terminal window. If you open another terminal window, the alias will no longer be present.

You can make an alias be valid for all terminal windows by placing it in your ~/.bash_profile or ~/.bashrc files (bash is the name of the terminal which Mac OS X ships with by default.)

Since Mac OS X does not load .bashrc by default, I would actually do:

echo "alias hw='cd ~/Desktop/2015hw'" >> ~/.bash_profile

When you want to delete the alias, you can open your ~/.bash_profile or ~/.bashrc files and delete it manually.

To open them, do open -a TextEdit ~/.bash_profile or open -a TextEdit ~/.bashrc

Finally, to list all your current aliases, simply type alias in Terminal.

like image 38
m81 Avatar answered Oct 20 '22 12:10

m81


your can start with type

ls -la

and then if you use bash typing

nano .bash_profile

or if you use zsh typing

nano .zshrc

then make alias save with ctrl + o for activating use source ~/.bash_profile or

source ~/.zsh
like image 1
fathurzero Avatar answered Oct 20 '22 11:10

fathurzero