Skip to content

Instantly share code, notes, and snippets.

@adduc
Last active January 25, 2025 22:18
Show Gist options
  • Save adduc/75b5434b2079542f8e469d53f442336b to your computer and use it in GitHub Desktop.
Save adduc/75b5434b2079542f8e469d53f442336b to your computer and use it in GitHub Desktop.
Function to initialize new exercise

Bash function for new exercises

I find myself frequently creating one-off repos to test a concept or technology. To streamline this process, I have written a bash function and included it in my bashrc configuration.

When invoked, it creates a new directory, creates a mercurial repository preconfigured for git integration with a main bookmark activated, creates a README.md file, and then opens the directory in my editor of choice (VSCode).

#!/bin/bash
# Usage: new-exercise <slug> [<name>]
new-exercise() {
DIR=~/Documents/personal/exercises/$1
[ $# -eq 1 ] && TITLE=$1 || TITLE=$2
[ ! -d "$DIR" ] || { echo "Exercise $1 already exists" && return; }
mkdir -p $DIR \
&& cd $DIR \
&& echo "# $TITLE" > README.md \
&& touch .gitignore \
&& git init \
&& hg clone . $1 \
&& rm -rf .git \
&& mv $1/.hg .hg \
&& rm -rf $1 \
&& hg bookmark main \
&& code . \
&& cd -
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment