Note-taking with Vim

Two vim posts in one day!

My task list at work has recently become so large (it’s probably well over a year’s worth of work now) that I now need to track my tasks somewhere other than in my head (documentation is always better than tribal knowledge anyways). I realy don’t like task tracking becuase most of the applications out there are just so heavy for what note-taking actually is. I use vim almost all day, every day though, so why not use that (plus it’s command line!)?

I spent about thirty minutes writing this up today. It’s inspired a bit by the LifeHacker article, Turn Your Command Line into a Fast and Simple Note Taking Tool (thanks Jack Mottram).

This will automagically give all of your notes a .wiki extension, telling vim to use the mediawiki text syntax highlighter (I use MediaWiki a lot to so I figured I’d use that syntax for markup). This can be found here. If you want to use something else like markdown, just change the $noteExt variable at the top to the extension associated with the highlighter you want.

This addition will give you six new commands.

  • note [NoteName]: Opens a note for editing or creates a new note. If no note is specified, opens the most recent note.

  • mknote NoteName "Note to append": Appends text to the requested note.

  • catnote [NoteName]: Prints the contents of the specified note.

  • lsnotes: Lists all notes by date modified

  • findnote SearchTerm: Searches all notes for the search term (case insensitive) and prints the results along with note title and line number on which the term was found.

  • mvnote OldName NewName: Renames a note

  • rmnote NoteName: Deletes the specified note.

Add the following to your .bash_profile (or .profile if you’re a ksh user)

export base=~/Documents/Notes
export noteExt=wiki
# This would be used for markdown
# export noteExt=md
note() {
  if [ ! -d $base ]; then
    mkdir -p $base
  fi
  # If note not specified, open most recent
  if [[ -z "$1" ]]; then
    vim $(ls -t $(find $base/ -type f) | head -n 1)
  else
    vim $base/$1.$noteExt
  fi
}

mknote() {
  echo $2 >> $base/$1.$noteExt
}

catnote() {
  # If note not specified, cat most recent
  if [[ -z "$1" ]]; then
    cat $(ls -t $(find $base/ -type f) | head -n 1)
  else
    cat $base/$1.$noteExt
  fi
}

lsnotes() {
  #ls -1 $base/ | sed "s/\(.*\).$noteExt/* \1/"
  echo
  echo -e "Last Modified\tName"
  ls -lt $base/ | tr -s ' ' | cut -d ' ' -f 6,7,8,9 | sed  "s/^\(\w\+\) \(\w\w\) \(\w\w:\w\w\) \(.*\).wiki/\1 \2 \3\t\4/"
  echo
}

findnote() {
  if [[ -n "$1" ]]; then
    contents="Note:Line:Text\n\n"
    contents=$contents$(find $base/ -type f | xargs grep -n -i "$1" | sed "s/.*\/\(.*\)\.$noteExt:\([0-9]\+\):\(.*\)/\1:\2:\3/")
    echo -e "$contents" | column -s ":" -t
  else
    echo "Please specify a search term."
  fi
}

mvnote() {
  mv $base/$1.$noteExt ~/Documents/Notes/$2.$noteExt
}

rmnote() {
  if [[ -n "$1" ]]; then
    rm $base/$1.$noteExt
  else
    echo "Please specify a note."
  fi
}

Category:Linux Category:Vim Category:Productivity