Stephen Gilmore

✍️ Quick markdown notes

Programming December 12th, 2023 3 minute read.

I love Markdown for notes. I also have a Markdown editor problem. I've tried Obsidian (too much), Typora ($$, too slow), and iaWriter ($$). And really didn't care for any of them for one reason or another. I kept wanting to come back to the simplicity of Visual Studio Code since I use that as a text editor for everything else.

The Bash script

Using ChatGPT, I was able to create a bash script that: 1. Accepts one or more arguments to use in a file name. 2. Creates a date stamp in the format yymmdd. 3. Generates a filename with the date stamp and arguments. 4. Creates a file with the generated filename. 5. Writes a heading to the file. 6. Opens the file in Visual Studio Code.

So for example, ./new_note.sh newNote would create a note named 231213 newNote.md.

Don't forget to make the script executable by running chmod +x new_note.sh

#!/bin/bash

# This bash script creates a new markdown file.
# The filename is a date stamp followed by at least one argument.
# The created file looks like "yymmdd filename arguments.md"

# Check if the filename is provided as an argument
if [ -z "$1" ]; then
    echo "Usage: $0 filename [additional parts...]"
    exit 1
fi

# Get the current date in yyyy-mm-dd format
current_date=$(date +%y%m%d)

# Initialize a filename with the date prefix
filename="${current_date}"

# Loop through all arguments and append them to the filename
for arg in "$@"; do
    filename="${filename} ${arg}"
done

# Append .md to filename
filename="${filename}.md"

# Get the directory where the script is located
script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"

# Create the file in the same directory as the script
touch "$script_dir/$filename"
echo "File '$filename' created in $script_dir"

# Write a heading to the file
echo "# ${filename}" >> "$script_dir/$filename"

# Open the file in Visual Studio Code
code --goto "$script_dir/$filename:9999999"

Create an alias for quick access

Having to type of navigate to this bash script is a bit tedious. Here's how I set up an alias so I can create a new note from anywhere.

Open up and edit the .zshrc file.

nano ~/.zshrc

Add the following line

alias nn="/path/to/file/Notes/new_note.sh"

Save and exit. Type logout in the terminal window to reload the .zshrc config.

code: command not found error

When running the script, you might get an error that looks like:

line 37: code: command not found

The solution: 1. Open Visual Studio Code 2. Open the Command Pallette with CMD+Shift+P 3. Select Shell Command: Install 'code' command in PATH