Mastering the “Cd-Create” Workflow: Combine Directory Creation and Navigation
If you spend any time working in a command-line terminal, you have likely typed this sequence a million times:
mkdir my-new-project
cd my-new-project
While it only takes a few seconds, repeating these two steps—first creating a folder with mkdir and then immediately changing into it with cd—is a textbook example of redundant developer friction.
Enter “cd-create” (often implemented as a custom mkcd function): the ultimate terminal habit that merges folder creation and navigation into a single, seamless motion.
Why the Friction Exists
By design, Unix shells separate these concerns:
cdis a shell builtin command responsible for changing the current working directory.mkdiris an independent binary executable file used to build directories.
Because they operate independently, the shell doesn’t natively bundle them into a single cd-create command out of the box. However, because developers love optimizing their workflows, bridging this gap takes only a few lines of configuration.
How to Build Your Own “Cd-Create” Shortcut
You can easily bake a shortcut directly into your terminal environment (whether you use Bash or Zsh).
Open your shell configuration file (such as ~/.bashrc, ~/.zshrc, or ~/.config/fish/config.fish) and add a tiny helper function:
mkcd() {
mkdir -p -- "$1" && cd -P -- "$1"
}
What makes this snippet tick:
mkdir -p -- "$1": The-pflag tells the terminal to create the directory and any necessary parent directories along the path without throwing errors if they already exist. The--safely handles folder names that might start with hyphens.&&: This ensures the second command only runs if the first command succeeds. No broken pipes or accidental misfires.cd -P -- "$1": Instantly teleports you into your newly created workspace.
Level Up Your Terminal Game
Once you adopt a cd-create mindset, you can expand the concept to suit your daily needs:
- Project Scaffolding: Write a slightly larger function that not only creates and enters a folder, but also kicks off a template repository or runs
git init. - Instant Cleanup: Combine creation with automated file generation so you can spin up an isolated test environment instantly.
By eliminating minor redundancies, you keep your focus where it belongs: writing great code instead of fighting repetitive keystrokes.
Leave a Reply