Zettelkasten Forum


Open a random note

Friends who use readwise.io have said one thing they appreciate is the email they get with a randomly selected note that they have made. I thought, it would be great to help me process notes that I have hoarded if I added a way to open a note at random and deal with it.

I created a bash script

`
cd /path/to/data
a=( * )
filename=( "${a[RANDOM%${#a[@]}]"1"}" )

filename=$(basename -- "$filename")
filename="${filename%.*}"

filename=${filename// /%20}

open thearchive://match/$filename
`

I cobbled it together from various sources. I sure a real bash person could reduce it to one long line with pipes, but I'm not that person.

Anyway, I attached this to a keyword in Alfred. Now, whenever I have a couple of minutes, I run the script and think about the note I am presented with. Linking, tidying the words, checking external links, that sort of thing.

I've only had it going for a day, but it feels a worthwhile addition.

Comments

  • I have a similar shell function:

    # shell function to place in your ~/.bashrc
    # customize the zettelkasten path, glob, and editor variables
    function randzettel () {
        local zettelkasten="$HOME/Documents/notes"
        cd "$zettelkasten"
        local glob="??????????????.wiki"    # pattern matching note file names
        local editor=$(which gvim)
        local rnum=$(( RANDOM % $(eval "ls $glob" | wc -l) - 1 ))
        $editor $(eval "ls $glob" | sed -n ${rnum}p) &
        cd -
    } >/dev/null 2>&1
    

    :wq

  • After posting my last comment I got to looking at my function more closely and realized it had some edge case issues and that it would not work properly in my current shell (zsh). So I rewrote it:

    # shell function to place in your ~/.bashrc or ~/.zshrc
    # customize the zettelkasten path, glob, and editor variables
    function randzettel () {
        local zettelkasten="$HOME/Documents/notes"
        cd "$zettelkasten"
        local flist=( ??????????????.wiki )
        local maxrand=32767
        local seed=$(( ${#flist[@]} - 1 )) # 1 less than number of files
        local editor=$(which gvim)
        if [[ $SHELL =~ zsh ]]; then
            # range: 1 to last index
            local index=$(( RANDOM * seed / maxrand + 1 ))
        elif [[ $SHELL =~ bash ]]; then
            # range: 0 to last index
            local index=$(( RANDOM * seed / maxrand ))
        fi
        $editor ${flist[$index]} &
        cd -
    } >/dev/null 2>&1
    

    :wq

  • Yours is much more flexible than mine, obviously. I had thought of opening in my editor rather than in The Archive, but as my main purpose is to enable me to do more processing of notes, it seemed better to do it in The Archive directly.

  • I do this sometimes too! The directory with my notes only has notes, so when I want a random note I tend to keep it simple and

    vim $(ls -1 | shuf -n 1)
    

    from the note directory

  • I forgot about that command! :)

    :wq

Sign In or Register to comment.