DEV Community

Discussion on: Simple note taking from the command line

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
}