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

  • Of course I didn't think to search here until after I'd written an attempt:

    open thearchive://match/"$(ls $HOME/whereever | shuf -n 1 | cut -d'.' -f1)"

    Though this relies on you having the GNU coreutils (brew install coreutils), which I recommend anyway.

    It also makes the (true for me) assumption that the only . in your filename is the extension, though you could substitute your favorite way of handling that if that's not the case.

  • Thanks for sharing this one-liner! Very timely: I'm experimenting with adding a random note prompt to my weekly review template.

    Author at Zettelkasten.de • https://christiantietze.de/

  • I use a set of Python functions daily for getting random notes. I am currently using it to help with refactoring large notes. It gets outputted to Bear, in which I do my journaling. This is what today's looks like:

    These live links launch The Archive and open the target note.

    I think I win! My code is 148 lines. Try and top that @Thai.
    Unless we use golf rules, where the lowest number determines the winner, I lose big time, and you can't be beaten.

    If you are mildly curious, here are the details. woodenzen/randomize

    Will Simpson
    I must keep doing my best even though I'm a failure. My peak cognition is behind me. One day soon I will read my last book, write my last note, eat my last meal, and kiss my sweetie for the last time.
    kestrelcreek.com

  • Hey, zettelnauts; I have been studying Python and specifically lambda functions. I see where I can apply new knowledge to this question. I've rewritten the former behemoth shrinking it to just 15 lines including linting and comments. (Only 9 lines of actual code.) The speed is super fast, allowing you to specify the number of random files to print in the output. The output can be modified easily.

    Output

    If you are mildly curious, here are the details.GIT Hub woodenzen/randomizeV2

    Will Simpson
    I must keep doing my best even though I'm a failure. My peak cognition is behind me. One day soon I will read my last book, write my last note, eat my last meal, and kiss my sweetie for the last time.
    kestrelcreek.com

Sign In or Register to comment.