Post

How To Add Custom Alias Or Functions In your Shell

Learn how to add custom alias or functions in your shell to make your life easier.

How To Add Custom Alias Or Functions In your Shell

As a developer and blogger, I type a lot of commands in the terminal when I’m linting Swift code or writing a new post using Jekyll. And believe it or not—if you repeat something often enough, you will want to shorten it.

So here’s how to add custom aliases and functions to your Zsh shell on macOS.

Where to Put Them?

Edit your ~/.zshrc file, which is the config file that runs every time a new terminal session starts.

1
nano ~/.zshrc

Then scroll to the bottom (or organize it however you like) and add the following:


Custom Functions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Compress a PDF file to a smaller size
compressPdf() {
    local input_file=$1
    local output_file=$2
    local quality=$3
    gs -sDEVICE=pdfwrite -dNOPAUSE -dQUIET -dBATCH -dPDFSETTINGS=/${quality:-"screen"} -dCompatibilityLevel=1.4 -sOutputFile="$output_file" "$input_file"
}

# Create a new Jekyll post easily
function createPost() {
    local post_name=$1
    bundle exec jekyll post "$post_name"
}

# Build and open the blog in the browser
function buildAndOpenBlog(){
    bundle exec jekyll serve -l -o
}

Custom Aliases

1
2
3
4
5
6
7
8
# Git push with a quick message
alias updatePosts='git add . && git commit -m "update posts" && git push'

# Run Swift Format recursively in the current directory
alias swiftformat='swift-format -i -r .'

# Reload your shell config without restarting the terminal
alias reload='source ~/.zshrc'

Apply the Changes

After saving the file, run:

1
source ~/.zshrc

Or just use your new reload alias.

Result

Now you can simply run:

  • compressPdf input.pdf output.pdf screen
  • createPost "My Awesome Blog Post"
  • updatePosts
  • swiftformat

…and save tons of time daily.

Happy terminal hacking! ⚡️

If you have cool aliases or functions, let me know—I’d love to try them too!

☕ Support My Work

If you found this post helpful and want to support more content like this, you can buy me a coffee!

Your support helps me continue creating useful articles and tips for fellow developers. Thank you! 🙏

This post is licensed under CC BY 4.0 by the author.