DEV Community

Ricardo Molina
Ricardo Molina

Posted on • Edited on

Simple note taking from the command line

This is a simple function that can be called from the command line, just like if it was a regular command. It is a quick way of taking notes and storing them in a text file.

Just add the following lines to your ~/.bashrc:

# My function to take quick notes on useful commands
notes() {
    echo $1 >> $HOME/notes.md
}
Enter fullscreen mode Exit fullscreen mode

It can be used like this:

$ notes "my_command -which -I -want -to remember"
Enter fullscreen mode Exit fullscreen mode

You can read your notes like this:

$ more ~/notes.md
Enter fullscreen mode Exit fullscreen mode

Top comments (28)

Collapse
 
konstantinstadler profile image
Konstantin Stadler

And here a version with timestamps (setup for vimwiki):

note() {
  if [ ! -z "$1" ]; then
    echo $(date +"%Y%m%d-%H%M%S") $@  >> $HOME/notes/TempNotes.wiki
  else
    echo $(date +"%Y%m%d-%H%M%S") "$(cat)"  >> $HOME/notes/TempNotes.wiki
  fi
}
Collapse
 
miere profile image
Miere Teixeira

Awesome, @konstantin Stadler!

Believe me or not, I was about to make the same suggestion you did!
\o

@Ricardo Molina,
Pretty useful idea. Thanks a lot!

Collapse
 
aghost7 profile image
Jonathan Boudreau • Edited

Nice idea! Something a bit more elaborate (just for fun):

notes() {
  if [ ! -z "$1" ]; then
    # Using the "$@" here will take all parameters passed into
    # this function so we can place everything into our file.
    echo "$@" >> "$HOME/notes.md"
  else
    # If no arguments were passed we will take stdin and place
    # it into our notes instead.
    cat - >> "$HOME/notes.md"
  fi
}
Enter fullscreen mode Exit fullscreen mode

With this function we can use the heredoc syntax to write multi-line notes:

notes <<NOTE
This is a very long note
because sometimes I like
to write explanations of
my commands and such.
NOTE
Enter fullscreen mode Exit fullscreen mode

You also don't need to quote your notes anymore:

notes my_command -which -I -want -to remember
Enter fullscreen mode Exit fullscreen mode
Collapse
 
ben profile image
Ben Halpern

Good call.

Collapse
 
ricardomol profile image
Ricardo Molina

Hi Jonathan, I just tried your code and I was suprised to find that when I call the notescommand without parameters, it doesn't show the content of the notes.md file. Instead it expects me to introduce the note content itself, and it doesn't write it to file till I do Ctrl + C. Hmm... I'll find out how to fix this.

Collapse
 
michaelmior profile image
Michael Mior

That looks like it's the intended behaviour to make it easy to write multi-line notes. If you'd rather have it just show the notes, then change the second branch of the if statement to just cat "$HOME/notes.md.

Thread Thread
 
ricardomol profile image
Ricardo Molina

You are absolutely right Michael. I misread the comment and thought that, in absence of paramenters, the expected behavior was to display the file content. My apologies for the mistake Jonathan.

Collapse
 
ricardomol profile image
Ricardo Molina

Excellent Jonathan! Really nice addition.

Collapse
 
math2001 profile image
Mathieu PATUREL
# If no arguments were passed we will take stdout and place
# it into our notes instead.
cat - >> "$HOME/notes.md"

You mean stdin here, right?

Collapse
 
aghost7 profile image
Jonathan Boudreau

Yea, sorry I guess I was thinking from a different perspective.

Collapse
 
gkchestertron profile image
John Fellman

I love this - and ran away with it a bit - here is a version that also allows editing in vim and making the notes distributable with git. Also lets you serve a pretty version with grip if you have it.

#!/bin/bash

note() {
  # start a new heading and append from stdout
  if [ ! -z "$1" ]; then
    clear
    echo "" >> "$HOME/notes/notes.md"
    echo "## $@" >> "$HOME/notes/notes.md"
    cat "$HOME/notes/notes.md"
    cat - >> "$HOME/notes/notes.md"
    clear
    cat "$HOME/notes/notes.md"

  # append to file from stdout
  else
    clear
    cat "$HOME/notes/notes.md"
    cat - >> "$HOME/notes/notes.md"
    clear
    cat "$HOME/notes/notes.md"
  fi
}

notes() {
  cur_dir=$(pwd)

  if [ ! -z "$1" ]; then
    # commit and push if args are exactly "push"
    if [ "$1" = "push" ]; then
      cd "$HOME/notes"
      git add -A
      git commit -m "update notes"
      git push
      cd $cur_dir

    # pull from github if args are exactly "pull"
    elif [ "$1" = "pull" ]; then
      cd "$HOME/notes"
      git pull
      cd $cur_dir

    # serve with grip if args are exactly "serve"
    elif [ "$1" = "serve" ]; then
      open "http://localhost:6419"
      grip "$HOME/notes/notes.md"

    # open vim to search of the args
    else
      escaped=$(echo "$@" | sed s/\ /\\\\\ /g)
      echo $escaped
      vim +/"$escaped" "$HOME/notes/notes.md"
    fi

  # just open the notes
  else
    vim + "$HOME/notes/notes.md"
  fi
}
Collapse
 
adamthiede profile image
Adam Thiede

Nice! I created a similar thing for PowerShell just a while back.

function noteit {
  Param(
    [Parameter(Mandatory=$true)]
    [string]$data
  )

  set-variable curdatetime $(get-date -format yyyy-MM-dd_HH_mm)
  set-variable title $notes\$curdatetime.txt

  if(Test-Path -LiteralPath $title) {
    #echo $curdatetime >> $title
    echo $data >> $title
    echo "note appended at $title"
  }
  else {
    touch $title
    echo $curdatetime > $title
    echo $data >> $title
  echo "note created at $title"
  }
}


``

Collapse
 
rrampage profile image
Raunak Ramakrishnan

A minimal tool I use for command line notes is jrnl.
Installing it is as simple as pip install jrnl
A note can be created by jrnl today at 3pm : Subject. Content.
It is especially useful when debugging, for revisiting assumptions.

Collapse
 
crux profile image
dirk lüsebrink

a plain text note file is the very best idea you can ever have. Just looked it up, i started mine april 2006, so with more than 10 yrs in it now. Think about it, that was a time when lots of URL web based note taking services where all en vouge, all the craze. Most of them long gone. My plain text notes still there. But while we're talking, as i'm working from the command line, i prefer jotting my notes directly from inside vim. It's easy to have it open in a screen pane on its own all the time. Editing features, especially multi-line, are much more robust when working from vim. I also had this notes alias once, but found i hardly any improvments in convenience. What i'm working on though is sharing the notes txt file to smartphone mobile view, for distributed view/edits and to build a browser plugin so i can append urls and comments directly from chrome. But that is a story for another time,

Collapse
 
thorstenhirsch profile image
Thorsten Hirsch

Simple & beautiful, even more so with your additions, Jonathan.

However my single notes file was growing bigger and bigger. One day I had to split it into topic-based files. And I wanted to share the notes between multiple devices. Meanwhile I got rid of my files in favour of Google Notes with one note for each topic. But of course I miss the shell interface.

One of my colleagues has also taken a web based approach for his notes including a workflow from Evernote to Trello, thus migrating notes into tasks. I am tempted to do the same, but with Asana as the destination for my notes. If only I could re-establish the console as the starting point once again...?

Collapse
 
cannuhlar profile image
Can Nuhlar

I recently wrote a python script to do this sort of thing. You can create gists or retrieve gists with it. It takes input from stdin so I guess you can use it to take notes too. Here check it out: github.com/CanNuhlar/CLI-Gist-Client

Collapse
 
pmcgowan profile image
p-mcgowan

Awesome work - clean and simple. I had the same idea a while back, and I made a massive, over-complicated bash script which handles multiple file categories, searching and sorting, as well as adding, editing, and removing lines or strings. More of a make-work hobby project but it was fun to make.

Feel free to check it out github.com/p-mcgowan/cmdPlanner and suggestions are always welcome.

Collapse
 
xochilpili profile image
xOCh • Edited
notes(){
   if [ ! -z "$1" ]; then
       echo "[" $(date +"%Y%m%d %H:%M:%S"@$(hostname)"]" "@" >> "$HOME/notes.md";
   else
       if [ ! -f "$HOME/notes.md" ]; then
           echo "No notes!";
           #touch "$HOME/notes.md";
           exit 0;
       else
           cat - >> "$HOME/notes.md";
           # or cat "$HOME/notes.md"; for show all notes :P
       fi
   fi
}