Twitter icon LinkedIn Icon Github Icon Stackoverflow Icon

Note-Taking with Vim

By: Roger Creasy

I am experimenting with note-taking/journaling using Vim. I use Vim for almost all other text-based activities, so, why not? I have used Evernote for many years, and have a ton of content there. But, I am looking for more security and control.

I added a function to my .aliases file, which is sourced by my .zshrc file (the function should work fine for Bash as well). This function can do several things:

  • Create a file named with the current date, write a date stamp to the top of the file, then open it in Vim.
  • Create a file with a name of your choosing, then open it in Vim
  • Search your note directory for all note files, and generate a preview of the currently selected (not opened) file
  • Search all of your notes for a text string

Here is the code, followed by a line-by-line explanation:

note() {
    cd /home/roger/Documents/notes
    if [ $# -gt 0 ]; then
        case $1 in
            -n)
                case "$2" in
                    "")
                       NAME=$(date +"%F")
                       TODAY=$(date +"%A-%b-%d-%Y")
                       touch $NAME
                       echo "$TODAY \n\n" >> $NAME
                       vim $NAME
                       ;;
                   *)
                       NAME=$2
                       touch $NAME
                       vim $NAME
                       ;;
            esac;;     
        -s)
            case "$2" in
                    "")
                         vim `fzf --preview="cat {}" --preview-window=right:70%:wrap | sort -k9`
                         ;;
                     *)
                         vim `grep -rnw '/home/roger/Documents/notes' -e $2 | cut -f1 -d":" | fzf --preview="cat {}" --preview-window=right:70%:wrap    `
}
            esac;;
        esac;;
    else                                                                                                                                           
        echo "a switch is required. -s for search, -n for  new"            
    fi                                                        
 }   

After adding the above I can type "note" followed by a switch, either -n (new) or -s (search) at the command line. With the -n switch I am given a file named with the current date, and date stamped with the same (formatted differently) at the top of the file. With the -s switch followed by search terms in double quotes, my notes are searched for the content within the quotes.

Here are what the lines in the function do:

  1. cd /home/roger/Documents/notes - change into the directory where my notes are stored. Set this to your desired location.
  2. if [ $# -gt 0 ]; then - if a switch is present, continue with the process, else, echo a warning
  3. case $1 in - determine which code to run based on the switch (the first input value)
  4.   -n) if the switch is -n, process the following code
  5.     case "$2" in - handle the second input
  6.       "") - if there is no second switch (it is blank), process the following code
  7.           NAME=$(date +"%F") - set a variable to the current date in the format YYY-MM-DD
  8.           TODAY=$(date +"%A-%b-%d-%Y") - set a variable to the current date in the format Day Name - Month Name - Day of month -year
  9.           touch $NAME - Create a file named with the variable $NAME set above
  10.           echo "$TODAY \n\n" >> $NAME - Write the variable set to $TODAY above and 2 blank lines to the new file
  11.           vim $NAME - Open the file in Vim
  12.           ;; - every case clause must be closed with 2 semicolons
  13.       *) - if the second switch contains anything, process the following code
  14.            NAME=$2 - set a variable = to the second input
  15.            touch $NAME - Create a file named with the variable $NAME set above
  16.            ;; - every case clause must be closed with 2 semicolons
  17.   -s) - We are back to our case for the first switch. If the first input value is -s, process the following code
  18.        case "$2" in - handle the second input
  19.            "") - if there is no second switch (it is blank), process the following code
  20.                vim `fzf --preview="cat {}" --preview-window=right:70%:wrap | sort -k9` - use Vim to open the selected file found using fuzzy find, divide into 2 windows with a preview on the right, sort the results on the 9th column (file creation date)
  21.                 ;; - every case clause must be closed with 2 semicolons
  22.            *) - if the second switch contains anything, process the following code
  23.                vim `grep -rnw '/home/roger/Documents/notes' -e $2 | cut -f1 -d":" | fzf --preview="cat {}" --preview-window=right:70%:wrap    ` - almost the same as the one above, but grep for the value of the second input
  24.                 ;; - every case clause must be closed with 2 semicolons
  25. the remaining lines close out the cases, handle the else explained above, and close the if
              

I hope this helps you. I do plan on expanding what I am doing with this functionality.

Publication Date: 2018-08-02 20:25:22