Create an “I’m feeling lucky” command in Obsidian

This makes use of the Templater plugin’s Startup Templates functionality.

The following gist can be created and placed in a location in your vault outside of the scope of your usual notes, eg Utilities/Startup. When you reload or restart Obsidian, Templater will execute the startup script which will add the app:feelingLucky command to Obsidian.

The command looks for files modified more than 14 days ago to surface. It uses Dataview for the notes query and because I’m also using GitHub - zachmueller/spaced-everything, I’m using se-last-reviewed for the date comparison, but you could just as easily use modified if you have that in your frontmatter, or p.mtime for the filesystem modified time. Spaced Everything has a command to Open next review item based on the SuperMemo SM-2 algorithm , but I also wanted a completely randomised option for fun that only drew from the spaced pool.

%% NOTE: Remove code fences. They were added for gist rendering purposes. %%

<%*
dv = app.plugins.plugins.dataview.api;
function lucky() {
        const threshold = dv.date('now') - dv.duration('14 days');
        const list = dv.pages().where(p => {
                if (p["se-last-reviewed"]) {
                        const lastReviewed = p["se-last-reviewed"].toString().slice(0, 10);
                        return dv.date(lastReviewed) < threshold;
                } else {
                        return false;
                }
        });
        const file = list[Math.floor(Math.random() * list.length)].file;
        app.workspace.getLeaf(true).openFile(app.vault.getAbstractFileByPath(file.path));
}
  
app.commands.addCommand({  
        id: 'app:feelingLucky',  
        name: "I'm feeling lucky",  
        callback: lucky,  
        hotkeys: []  
});  
console.log("[I'm feeling lucky] added to commands");  
%>
view raw lucky.md hosted with ❤ by GitHub

You can run this as you would other Obsidian commands, or you can add it to your Ribbon with GitHub - phibr0/obsidian-commander which will persist the button between restarts.

Random note plugins

Other random note plugins exist, but they don’t do what I want, I don’t want to turn this into a plugin, and if you use this you can customise it as you like.

Published February 26, 2025