DEV Community

Raunak Ramakrishnan
Raunak Ramakrishnan

Posted on • Edited on

4 2

Bash function to add TILs from the command line

Here's a snippet I use in my .bashrc file to quickly write down and review TILs (Today I learned):

TIL_FILE="$HOME/my-notes-repo/til.md"
til () 
{ 
    # checks if the function has been called without any argument
    if [[ -z $1 ]]; then
        # opens the file in my editor, setting the cursor to the last line
        # useful to review latest entries or to write a longer entry
        vim + "$TIL_FILE";
    else
        # adds a line with today's date, a TAB and all arguments supplied ("$@")
        echo -e "- $( date '+%F' ):\t$@" >> "$TIL_FILE";
    fi
}
Enter fullscreen mode Exit fullscreen mode

How to use:

  • til to open the file. I use this to review what I learned today or if I need to write a longer, multiline entry
  • til CONTENT to append a line to the file
    • e.g til grep --line-buffered to immediately print especially when tailing files will add this: - 2020-05-23: grep --line-buffered to immediately print especially when tailing files

Explanation on the function

In case you missed the comments in the function, here's an explanation of what the various lines do:

  • if [[ -z $1 ]]; checks if the function has been called without any arguments
  • vim + "$TIL_FILE" opens the file in vim (my preferred editor), setting the cursor to the last line of the file.
  • echo -e "- $tdate:\t$@" >> "$TIL_FILE"; adds a line with today's date, a TAB and all arguments supplied ("$@") to til

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (1)

Collapse
 
moopet profile image
Ben Sinclair

That's neat.

I might suggest using the EDITOR variable to pick up the user's preferred editor, and fall back to good ol' Vim if it's not set:

${EDITOR:-vim} + "$TIL_FILE"

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay