13

I'm fairly new to Zsh and Bash.

I wish to set up custom commands, for example desktop which would bring me to my desktop. Adding the following lines to the .bash_profile file would work With Bash.

desktop() {
  cd /Users/me/Desktop
}

It doesn't seem to work with Zsh.

Is it just a matter of where I place the code, or am I going about this all wrong?

3 Answers 3

10

Edit the ~/.bashrc file. Find the alias section and add the following
alias desktop='cd /path/to/desktop'

Doing so, you'll declare some aliases. You can make any custom alias you want !
(ie. i have alias ne='emacs -nw' and everytime i run
ne "some file"
it opens the file in shell based emacs.)

Whenever you'll write desktop, it'll bring you there :-) (theoretically)

6
  • You may need to reboot for the changes to be effective. Jan 20, 2012 at 16:55
  • I appreciate the info, but it would seem I do not have a ~/.bashrc file. Is this because I'm using zsh or oh-my-zsh? I was able to get the code in my question working by placing it in the ~/.oh-my-zsh/oh-my-zsh.sh file, but when I use the same method to open a file, I get a cd:cd:10: not a directory:. So first, is it ok for me to place the code in the oh-my-zsh.sh file? And second, why would I be getting the cd:cd:10 alert? Jan 20, 2012 at 18:03
  • 1
    I figured out why I was getting the cd:cd:10 alert, so the only question that stands is concerning placement. I found that the code also works in ~/.zshrc which seems for apt. Is it ok to be placed here? And what about the difference between my code and your code? Are they doing two different things? Or is mine wrong, and yours right? Jan 20, 2012 at 18:13
  • my code works for bash. I am not familiar with zsh but I have found this : acm.uiuc.edu/workshops/zsh/alias.html I hope it will help. ~/.zshrc is used when you run zsh, while ~/.bashrc is for bash. I don't know oh-my-zsh at all. You can place the code line in ~/.zshrc if you want to run your alias through zsh. Your code is hum, I wouldn't say incorrect but incomplete, and it's much harder to get it to work the way you try to attain. With the code line I provided you, you're using some shell built in scripts. Jan 20, 2012 at 18:54
  • Thanks a bunch. I've got it working now without a hitch. You should really check out oh-my-zsh. Very cool stuff! Jan 20, 2012 at 19:06
19

With zsh you should edit ~/.zshrc (create it if it doesn't exist -> example template https://github.com/robbyrussell/oh-my-zsh/blob/master/templates/zshrc.zsh-template)

so:

nano ~/.zshrc

and add the alias you want, e.g.

alias my_project_runserver='cd my_project_path; . venv/bin/activate; ./manage.py runserver'

Using the alias above I go to my project folder, activate virtualenv and run django server

;

separates commands, so with one alias you can execute multiple commands

and for changes to take effect execute:

. ~/.zshrc

1
  • How do you update the file so that you can use the command right away? I had to close the terminal and reopen it to use it Sep 2, 2020 at 7:56
2

Create a global z shell script executable

It is better to create your own script file that is globally available and independent from the shell configuration.

1.Create a text file with the following content (name the file 'desktop'):

#!/usr/bin/env zsh
cd /Users/me/Desktop
#or any shell commands you want

2.Save the file to the path

/usr/local/bin 

3.Run the following command to make the script executable with the right permissions:

cd /usr/local/bin 
sudo chmod -x ./desktop
Sudo chmod 755 desktop

4.Restart the terminal.

1
  • Might want to use chmod u+x to give execution permissions to the file for yourself. u+x gives you, the user, execute permissions. I am not sure why he used -x? Probably don't need to use sudo either, in most situations...
    – Wade
    Aug 13, 2023 at 21:01

You must log in to answer this question.

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