Jay's Tech Bites Logo

Terminal Hacks for Developers: Custom Functions and Aliases to Boost Productivity (Zsh, Bash, Fish)

Master Terminal Customizations: When to Use Aliases vs. Functions in Zsh, Bash, and Fish

Jay McBride
  • Jay McBride
  • 4 min read
Orange Tips and Tricks Button on Computer Keyboard. Internet Concept.
Photo by Tashatuvango from Getty Images (Canva)

The terminal is a developer’s most powerful tool, but using it efficiently requires customization. By leveraging custom functions and aliases, you can automate repetitive tasks and streamline your workflow, whether you’re using Zsh, Bash, or Fish. These customizations save you time and let you focus on what matters most: coding.

In this post, we’ll explore both aliases and functions, explain the differences, and provide examples across Zsh, Bash, and Fish to help you work smarter, not harder.


When to Use a Function vs. an Alias

  • Alias: Best for simple commands or command replacements. Aliases are a good fit when you want to shorten a long command or a sequence of basic commands into a single word.

  • Function: Use a function when you need to handle more complex logic, such as loops, conditionals, or arguments. Functions are ideal when you need flexibility or want to reuse logic with slight variations (e.g., using different parameters).


1. Simple Navigation Shortcuts

Navigating to directories you use frequently is a common task, and a simple alias is perfect for shortening these repetitive commands.

Use an alias to quickly navigate to a project folder:

Zsh & Bash:

alias blog="cd ~/projects/my-blog"

Fish:

alias blog="cd ~/projects/my-blog"

When to use a function: If your navigation involves additional steps, such as initializing a process or checking conditions, a function would be more appropriate.


2. Project Scaffolding with a Single Command (Function)

Creating new projects involves repetitive steps: creating directories, initializing Git, creating files, and more. For this multi-step process, a function is ideal because it handles complex logic and parameters.

Zsh & Bash:

function new_project() {
    mkdir $1 && cd $1
    git init
    echo "# $1" >> README.md
    touch .env
    npm init -y
    code .
}

Fish:

function new_project
    mkdir $argv[1]
    cd $argv[1]
    git init
    echo "# $argv[1]" >> README.md
    touch .env
    npm init -y
    code .
end

Here, the function uses an argument $1 (or $argv[1] in Fish) to dynamically create projects with different names.


3. On-Demand Code Formatting (Function)

Since code formatting involves different tools for different languages, a function that accepts an argument is the best solution. This allows you to format JavaScript or Python code with a single command, depending on the parameter you pass.

Zsh & Bash:

function format_code() {
    case "$1" in
    "js")
        npx eslint . --fix && npx prettier --write .
        ;;
    "py")
        black . && flake8 .
        ;;
    *)
        echo "Usage: format_code [js|py]"
        ;;
    esac
}

Fish:

function format_code
    switch $argv[1]
        case 'js'
            npx eslint . --fix
            npx prettier --write .
        case 'py'
            black .
            flake8 .
        case '*'
            echo "Usage: format_code [js|py]"
    end
end

Here, a function works well because it accepts parameters (js or py) and executes different logic based on the input.


4. Refreshing the Terminal After Configuration Changes (Alias)

Reloading the terminal configuration file is a simple, repetitive task, so an alias works perfectly here.

Zsh:

alias reload="source ~/.zshrc"

Bash:

alias reload="source ~/.bashrc"

Fish:

alias reload="source ~/.config/fish/config.fish"

This alias quickly reloads your shell configuration after you make changes, without needing complex logic.


5. Searching Files Across Projects (Function or Alias)

If your file search is simple and doesn’t require complex behavior, you can use an alias. However, if you want to add more functionality—such as using different search criteria based on arguments—a function would be better.

Alias (for simple file searches):

alias findfile="find ~/projects -name '*$1*'"

Function (for more complex searches):

function find_file() {
    find ~/projects -name "*$1*"
}

Fish:

function find_file
    find ~/projects -name "*$argv[1]*"
end

This works well when you need a simple file search. However, if you need to add logic like searching different directories based on parameters, a function would be more effective.


Conclusion: Know When to Use Aliases and Functions

In your terminal workflow, knowing when to use an alias versus a function can significantly enhance your productivity. Aliases are best for short, simple commands, while functions shine when you need more flexibility or logic.

By customizing your terminal with the right balance of aliases and functions, you can automate tasks, streamline workflows, and focus on what matters most—coding.

What custom terminal functions or aliases do you use? Share your tips in the comments and let’s learn from each other!

Comment

comments powered by Disqus

Want more insights delivered right to your inbox?

Stay up-to-date with the latest in tech, development tips, and subscribe to my newsletter. Join the growing community of developers and tech enthusiasts today!

Sign up now and never miss a post!
Jay McBride

Written by : Jay McBride

Welcome to Jay’s Tech Bites! I’m Jay McBride, a tech enthusiast breaking down the latest trends in tech. Whether you're tech-savvy or just curious, I’ll keep it simple and interesting. Let’s explore the tech world together, one bite at a time.

Recommended for You

Chalkboard with the words Fix It written on it. Betwee the two words is a stop watch. In the foreground are flames hinting at the topic of a urgent hotfix.

The Ultimate Hotfix Guide for Developers Using Git: Patches, File Exports, and Cherry-Picking

Master Git Hotfix Techniques: Apply Critical Fixes Fast with Patches, File Exports, and Cherry-Picking.

6 min read

woman with robot advisor View more by Kittipong Jirasukhanont from PhonlamaiPhoto's Images

Machine Customers: How AI is Redefining Shopping—Is Your Business Ready?

Discover the rise of AI-powered machines that make purchases for us.

4 min read