Skip to content

Instantly share code, notes, and snippets.

@mustakimali
Last active July 1, 2025 19:54
Show Gist options
  • Save mustakimali/d79e61ebda5ced4b92f9a89fa25c922a to your computer and use it in GitHub Desktop.
Save mustakimali/d79e61ebda5ced4b92f9a89fa25c922a to your computer and use it in GitHub Desktop.
mkcd - Make Directory and Change Directory

mkcd - Make Directory and Change Directory

A simple shell utility that creates a directory and immediately changes into it.

What it does

The mkcd function combines mkdir -p and cd into a single command:

  • Creates the specified directory (and any parent directories if needed)
  • Changes into the newly created directory

Installation

Quick Install (One-liner)

curl -sSL https://gist.githubusercontent.com/mustakimali/d79e61ebda5ced4b92f9a89fa25c922a/raw/install-mkcd.sh | bash

Manual Install

Run the installer script:

./install-mkcd.sh

The installer will:

  • Detect your shell (bash, zsh, or fish)
  • Add the mkcd function to your shell configuration file
  • Support multiple shells: bash, zsh, and fish

Supported Shells

  • Bash: Adds function to ~/.bashrc or ~/.bash_profile
  • Zsh: Adds function to ~/.zshrc
  • Fish: Adds function to ~/.config/fish/config.fish

Usage

mkcd <directory>

Examples

# Create and enter a new project directory
mkcd my-new-project

# Create nested directories and enter the deepest one
mkcd projects/web/my-app

# Works with spaces (use quotes)
mkcd "My Project Folder"

After Installation

To start using mkcd immediately:

source ~/.bashrc    # for bash
source ~/.zshrc     # for zsh
# or restart your terminal

Uninstalling

To remove the mkcd function, manually delete the added lines from your shell configuration file.

#!/bin/bash
# mkcd installer - adds mkcd function to your shell
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Detect shell
detect_shell() {
if [ -n "$ZSH_VERSION" ]; then
echo "zsh"
elif [ -n "$BASH_VERSION" ]; then
echo "bash"
elif [ -n "$FISH_VERSION" ]; then
echo "fish"
else
# Fallback to checking $SHELL
case "$SHELL" in
*/zsh) echo "zsh" ;;
*/bash) echo "bash" ;;
*/fish) echo "fish" ;;
*) echo "unknown" ;;
esac
fi
}
# Get config file for shell
get_config_file() {
local shell_type="$1"
case "$shell_type" in
bash)
if [ -f "$HOME/.bashrc" ]; then
echo "$HOME/.bashrc"
else
echo "$HOME/.bash_profile"
fi
;;
zsh)
echo "$HOME/.zshrc"
;;
fish)
echo "$HOME/.config/fish/config.fish"
;;
*)
echo ""
;;
esac
}
# Generate function for shell
generate_function() {
local shell_type="$1"
case "$shell_type" in
bash|zsh)
cat << 'EOF'
# mkcd - create directory and cd into it
mkcd() {
if [ $# -eq 0 ]; then
echo "Usage: mkcd <directory>"
return 1
fi
mkdir -p "$1" && cd "$1"
}
EOF
;;
fish)
cat << 'EOF'
# mkcd - create directory and cd into it
function mkcd
if test (count $argv) -eq 0
echo "Usage: mkcd <directory>"
return 1
end
mkdir -p $argv[1] && cd $argv[1]
end
EOF
;;
esac
}
main() {
echo -e "${GREEN}Installing mkcd function...${NC}"
local shell_type=$(detect_shell)
echo "Detected shell: $shell_type"
if [ "$shell_type" = "unknown" ]; then
echo -e "${RED}Error: Unsupported shell${NC}"
echo "This installer supports bash, zsh, and fish"
exit 1
fi
local config_file=$(get_config_file "$shell_type")
if [ -z "$config_file" ]; then
echo -e "${RED}Error: Could not determine config file${NC}"
exit 1
fi
# Create config directory if it doesn't exist (for fish)
if [ "$shell_type" = "fish" ]; then
mkdir -p "$(dirname "$config_file")"
fi
# Check if mkcd already exists
if grep -q "mkcd()" "$config_file" 2>/dev/null || grep -q "function mkcd" "$config_file" 2>/dev/null; then
echo -e "${YELLOW}mkcd function already exists in $config_file${NC}"
echo "Remove it manually if you want to reinstall"
exit 0
fi
# Add function to config file
echo "Adding mkcd function to $config_file"
generate_function "$shell_type" >> "$config_file"
echo -e "${GREEN}✓ mkcd function installed successfully!${NC}"
echo
echo "To use it immediately, run:"
echo " source $config_file"
echo
echo "Or restart your terminal."
echo
echo "Usage: mkcd <directory>"
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment