Streamlining Your Terminal Workflow: The Power of “Cd-Create”
If you spend any time working in a command-line interface—whether on Linux, macOS, or Windows—you are likely familiar with the standard routine of setting up a new project workspace. Usually, it looks something like this:
mkdir my-new-project
cd my-new-project
While typing those few extra characters only takes a couple of seconds, efficiency philosophy dictates that repetitive micro-tasks are prime candidates for automation. Enter “cd-create”—the developer workflow practice of combining directory creation and navigation into a single, seamless motion.
Why the Separation Exists
To understand why a combined command isn’t built into operating systems by default, it helps to look at how terminals handle tasks:
mkdiris a standalone binary executable file responsible for creating directories on your file system.cd(Change Directory) is a shell builtin command because it needs to alter the state of the shell environment itself.
Because they operate differently under the hood, standard operating systems treat them as two distinct steps. However, you don’t have to live with the redundancy.
How to Implement Your Own “Cd-Create” Shortcut
You can easily bridge the gap by writing a tiny shell function and adding it to your terminal configuration file (such as ~/.bashrc, ~/.zshrc, or your PowerShell profile).
1. The Classic Bash / Zsh Function (mkcd)
Add this short snippet to your shell configuration file:
mkcd() {
mkdir -p -- "$1" && cd -P -- "$1"
}
mkdir -pensures that the system creates the directory (and any necessary parent directories) without throwing errors if it already exists.&&ensures that thecdcommand only executes if the directory was successfully created.cd -Pnavigates to the physical directory path, resolving any symbolic links accurately.
2. The PowerShell Alternative
If you are working in Windows PowerShell, you can achieve the exact same behavior by adding a function to your PowerShell profile ($PROFILE):
function mkcd {
param([Parameter(Mandatory=$true)][string]$Path)
New-Item -ItemType Directory -Force -Path $Path | Out-Null
Set-Location $Path
}
Benefits of a Unified Workflow
Adopting a cd-create mindset offers several immediate advantages for power users:
- Eliminating Typos: Typing a complex project name twice in a row leaves room for accidental spelling discrepancies between the creation line and the navigation line. Doing it in one go removes that risk.
- Reduced Context Switching: Conceptually, your intent is singular: “Take me into a new workspace called X.” Aligning your keystrokes with that mental model keeps your workflow fluid.
- Speeding Up Development: When spinning up test environments, quick scripts, or experimental folders all day long, eliminating redundant commands saves precious time and keeps your terminal clean.
By adding a simple custom function to your environment, you can turn a two-step chore into a frictionless, single-command habit.
Leave a Reply