How to Create a Custom CD: Step-by-Step Guide

Written by

in

Streamlining the Terminal: The Philosophy and Practice of “CD Create”

If you spend any significant amount of time in a command-line interface, you likely perform a specific routine dozens of times a week. You want to start a new project, a new branch, or a scratch directory, so you type:

mkdir my-awesome-project
cd my-awesome-project

It is a small, frictionless habit—so ingrained that your fingers type it almost without thinking. Yet, from the perspective of developer ergonomics, it represents a classic micro-inefficiency: redundancy. You are typing the exact same directory name twice in a row, introducing unnecessary cognitive overhead and doubling your vulnerability to typos.

Enter the concept of “cd create”—the ubiquitous developer workflow pattern dedicated to collapsing directory creation and navigation into a single, seamless motion.


Why Don’t Shells Have This Out of the Box?

To understand why a native cdcreate command doesn’t ship out of the box with Bash, Zsh, or PowerShell, you have to look under the hood of how shells operate:

  1. cd is a shell builtin. Because changing the working directory modifies the state of the parent shell process itself, cd must be executed internally by the shell. If it were an external binary, it would run in a subshell, change its own directory, and terminate—leaving your terminal right where it started.
  2. mkdir is an external binary. It lives on your file system (usually at /bin/mkdir) and executes as a separate child process to create folders.

Because one is a structural built-in directive and the other is a standalone utility, bridging them requires a tiny bit of custom shell scripting glue.


Building Your Own “CD Create” Shortcut

Fortunately, customizing your shell environment to support a unified command takes less than a minute. Whether you use Bash or Zsh, you can open your configuration file (~/.bashrc, ~/.zshrc, or ~/.bash_profile) and drop in a clean, robust function.

The most popular implementation is often called mkcd (or mcd):

mkcd() {
  mkdir -p -- "$1" && cd -P -- "$1"
}

Dissecting the Code:

  • mkdir -p -- "$1": The -p flag tells mkdir to create parent directories as needed, meaning you can run mkcd projects/2026/client-a and it will build the entire nested tree. The -- tells the parser to stop processing flags, protecting you if your folder name happens to start with a dash.
  • &&: Ensures that the cd command executes only if the directory was successfully created. If mkdir fails (

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *