DEV Community

Discussion on: Simple note taking from the command line

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
 
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
 
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
 
ben profile image
Ben Halpern

Good call.

Collapse
 
ricardomol profile image
Ricardo Molina

Excellent Jonathan! Really nice addition.