Create Your Markdown Files From Template

Markdown is incredibly useful, especially for text management. I use it extensively for almost anything related to text. Most of the time, I like to keep file structures the same across a project. Using a template file really helps with that. And since Markdown doesn’t have something like @date to automatically add the current date, we can just take care of that ourselves too.

To automate the generation of a markdown file from a template, you need template.md file and a bash file create.sh.

Markdown template

Basically, a template file is the same as a normal Markdown file, but without specific content. I like to add headings or empty tables that are unlikely to change, as well as meta-information such as the date and title. Here’s an example of a template file.

1
2
3
4
5
6
| Created  |   Title   | Tags |
| -------- | --------- | ---- |
| {{date}} | {{title}} | |


# Summary

Bash command

To automate the creation of a file, we can use the powerful hash command. Here’s an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# create.sh
# Check for input
if [ -z "$1" ]; then
echo "Usage: $0 \"your-title-here\""
exit 1
fi

# Title name gets from input
raw_title="$*"

# Format input "title with space" => "Title-With-Space"
formated_title=$(echo "$raw_title" | awk '{
for (i=1; i<=NF; i++) {
$i = toupper(substr($i,1,1)) tolower(substr($i,2))
}
print
}' | xargs | tr ' ' '-')

# Get the date
today=$(date +%F)

# Combine the file name
filename="${today}-${formated_title}.md"

# Uncomment if you don't need date or file name in your template
# cp template.md "$filename"

# Uncomment one if you are using date or file name or both in your template
# sed "s/{{date}}/$today/g" template.md > "$filename"
# sed "s/{{title}}/${raw_title//\//-}/g" template.md > "$filename"
# sed -e "s/{{title}}/${raw_title//\//-}/g" -e "s/{{date}}/$today/g" template.md > "$filename"

echo "$filename Created."

Depending on whether you want to handle variables in your template, you can uncomment the appropriate option.

Usage

In the terminal, simply use:

1
./create.sh "file name"

A file named YEAR-MONTH-DAY-File-Name.md will be created in the current folder, with the date and file name pre-filled.

If you got permission denied. Run this command

1
chmod +x create.sh
Author

X,qUe

Posted on

2025-04-12

Licensed under

Comments