As developers, we spend a significant amount of time in the terminal. Streamlining our workflow can lead to significant productivity gains. One of the most powerful ways to do this is by creating custom commands in your .bashrc file. This allows you to chain together existing commands, create shortcuts for frequently used operations, and tailor your command-line experience to your specific needs.
What is .bashrc?
The .bashrc file is a script that runs every time you open a new interactive terminal session. This makes it the perfect place to define custom functions and aliases that will be available whenever you need them. It’s located in your home directory (~/.bashrc).
Creating Your First Custom Command: mkcd
Let’s start with a classic example: a command that creates a new directory and then immediately changes into it. We’ll call it mkcd.
Without a custom command, you would type:
mkdir my-new-project
cd my-new-project
With our mkcd function, you can do this in one step:
mkcd my-new-project
Here’s how to add it to your .bashrc file:
Open
.bashrcin your favorite text editor:nano ~/.bashrcAdd the following code to the end of the file:
mkcd() { mkdir "$1" cd "$1" }Let’s break this down:
mkcd(): This defines a new function namedmkcd.mkdir "$1": This command creates a new directory."$1"is a special variable that represents the first argument you pass to the function (in this case, the directory name). We enclose it in double quotes to handle names with spaces.cd "$1": This command changes the current directory to the one you just created.
Save and close the file.
“Source” your
.bashrcto apply the changes without logging out:source ~/.bashrc
Now you can use your new mkcd command!
A More Advanced Example: chatg
Let’s look at a more specific and powerful command. This one is designed to streamline the process of starting a new chat session with the Gemini CLI in a dedicated directory.
chatg() {
mkdir "/home/ali/chat/$1"
cd "/home/ali/chat/$1"
clear
gemini
}
Here’s what this chatg function does:
mkdir "/home/ali/chat/$1": Creates a new directory inside/home/ali/chat/. The name of the new directory is determined by the first argument passed to thechatgcommand.cd "/home/ali/chat/$1": Changes the current directory to the newly created one.clear: Clears the terminal screen for a clean workspace.gemini: Starts the Gemini CLI tool.
To use it, you would simply type:
chatg my-new-chat
This single command automates four separate steps, saving you time and effort every time you start a new session.
Why Create Custom Commands?
- Efficiency: Automate repetitive tasks and reduce the number of keystrokes.
- Convenience: Create memorable shortcuts for complex command sequences.
- Customization: Tailor your command-line environment to your personal workflow.
- Power: Combine existing tools in new and creative ways to build powerful new commands.
Start exploring your own workflow and see what repetitive tasks you can automate. Your .bashrc is a powerful tool for a more efficient and enjoyable command-line experience.
Written by Ali Mobini, a developer exploring system architecture, embedded systems, and intelligent automation.