Reputation: 51463
I'm on OSX and I need to put something like this, alias blah="/usr/bin/blah"
in a config file but I don't know where the config file is.
Upvotes: 273
Views: 395883
Reputation: 77175
You can add an alias
or a function
in your startup script file.
The default shell is bash. Usually the startup script file is .bashrc
, .bash_login
or .profile
file in your home directory.
Since these files are hidden you will have to do an ls -a
to list them. If you don't have one you can create one.
If I remember correctly, when I had bought my Mac, the .bash_login
file wasn't there. I had to create it for myself so that I could put prompt info
, alias
, functions
, etc. in it.
Here are the steps if you would like to create one:
cd ~/
to go to your home foldertouch .bash_profile
to create your new file..bash_profile
with your favorite editor (or you can just type open -e .bash_profile
to open it in TextEdit.. .bash_profile
to reload .bash_profile
and update any alias you add.Upvotes: 337
Reputation: 417
You can do a few simple steps:
open terminal
sudo nano /.bash_profile
add your aliases, as example:
# some aliases
alias ll='ls -alF'
alias la='ls -A'
alias eb="sudo nano ~/.bash_profile && source ~/.bash_profile"
#docker aliases
alias d='docker'
alias dc='docker-compose'
alias dnax="docker rm $(docker ps -aq)"
#git aliases
alias g='git'
alias new="git checkout -b"
alias last="git log -2"
alias gg='git status'
alias lg="git log --pretty=format:'%h was %an, %ar, message: %s' --graph"
alias nah="git reset --hard && git clean -df"
alias squash="git rebase -i HEAD~2"
source /.bash_profile
Done. Use and enjoy!
Upvotes: 21
Reputation: 15857
Apple switched their default shell to zsh, so the config files include ~/.zshenv
and ~/.zshrc
. This is just like ~/.bashrc
, but for zsh. Just edit the file and add what you need; it should be sourced every time you open a new terminal window:
nano ~/.zshenv
alias py=python
Then do ctrl+x, y, then enter to save.
This file seems to be executed no matter what (login, non-login, or script), so seems better than the ~/.zshrc
file.
The default shell is bash, and you can edit the file ~/.bash_profile
and add aliases:
nano ~/.bash_profile
alias py=python
Then ctrl+x, y, and enter to save. See this post for more on these configs. It's a little better to set it up with your alias in ~/.bashrc
, then source ~/.bashrc
from ~/.bash_profile
. In ~/.bash_profile
it would then look like:
source ~/.bashrc
Upvotes: 48
Reputation: 1035
I just open zshrc with sublime, and edit it.
subl .zshrc
And add this on sublime:
alias blah="/usr/bin/blah"
Run this command in terminal:
source ~/.zshrc
Done.
Upvotes: 101
Reputation: 12461
I need to run the Postgres database and created an alias for the purpose. The work through is provided below:
$ nano ~/.bash_profile
# in the bash_profile, insert the following texts:
alias pgst="pg_ctl -D /usr/local/var/postgres start"
alias pgsp="pg_ctl -D /usr/local/var/postgres stop"
$ source ~/.bash_profile
### This will start the Postgres server
$ pgst
### This will stop the Postgres server
$ pgsp
Upvotes: 3
Reputation: 5967
To create a permanent alias shortcut, put it in .bash_profile file and point .bashrc file to .bash_profile file. Follow these steps (I am creating an alias command called bnode to run babel transpiler on ES6 code):
I hope this helps! Good luck!
Upvotes: 0
Reputation: 857
For macOS Catalina Users:
Step 1: create or update .zshrc file
vi ~/.zshrc
Step 2: Add your alias line
alias blah="/usr/bin/blah"
Step 3: Source .zshrc
source ~/.zshrc
Step 4: Check you're alias, by typing alias on the command prompt
alias
Upvotes: 13
Reputation: 4691
create a bash_profile at your user root - ex
/user/username/.bash_profile
open file
vim ~/.bash_profile
add alias as ex. (save and exit)
alias mydir="cd ~/Documents/dirname/anotherdir"
in new terminal just type mydir - it should open
/user/username/Documents/dirname/anotherdir
Upvotes: 1
Reputation: 13751
1) Go to teminal. open ~/.bashrc
. Add if not exists
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
2) open ~/.bash_aliases
. If not exists: touch ~/.bash_aliases && open ~/.bash_aliases
3) To add new alias rather
- edit .bash_aliases
file and restart terminal or print source ~/.bash_aliases
- print echo "alias clr='clear'" >> ~/.bash_aliases && source ~/.bash_aliases
where your alias is alias clr='clear'
.
4) Add line source ~/.bash_aliases
to ~/.bash_profile
file. It needs to load aliases in each init of terminal.
Upvotes: 1
Reputation: 5022
The config file for scripts and programs is ~/.bashrc
and the config file that gets loaded when you use Terminal is ~/.bash_login
.
I think the best way is to just have everything in ~/.bashrc
.
For your specific question just enter (this will overwrite any existing ~/.bashrc):
echo "alias blah=\"/usr/bin/blah\"" >>~/.bashrc
into the Terminal and a ~/.bashrc
file will be created with your new alises. After that just edit the file to add new aliases, functions, settings etc.
Upvotes: 7
Reputation: 2948
Create alias at bottom of the file
alias alias_name='command to do'
eg: alias cdDesktop='cd /Desktop'
Save the file
source .bashrc
source ~/.bashrc
Open terminal (Ctrl+Alt+T) & type cdDesktop & press enter
Upvotes: 6
Reputation: 169
cd /etc
sudo vi bashrc
Add the following like:
alias ll="ls -lrt"
Finally restart Terminal.
Upvotes: 9
Reputation: 40424
In my .bashrc
file the following lines were there by default:
# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
Hence, in my platform .bash_aliases
is the file used for aliases by default (and the one I use). I'm not an OS X user, but I guess that if you open your .bashrc
file, you'll be able to identify what's the file commonly used for aliases in your platform.
Upvotes: 21
Reputation: 706
On OS X you want to use ~/.bash_profile. This is because by default Terminal.app opens a login shell for each new window.
See more about the different configuration files and when they are used here: What's the difference between .bashrc, .bash_profile, and .environment?
and in relation to OSX here: About .bash_profile, .bashrc, and where should alias be written in?
Upvotes: 34
Reputation: 140487
If you put blah="/usr/bin/blah"
in your ~/.bashrc
then you can use $blah
in your login shell as a substitute for typing /usr/bin/blah
Upvotes: 4