Get current file content in Obsidian

If you are creating a plugin for Obsidian, and you want to get the currently opened file/note and its content, use this code…

const noteFile = this.app.workspace.getActiveFile(); // Currently Open Note
if(!noteFile.name) return; // Nothing Open

// Read the currently open note file. We are reading it off the HDD - we are NOT accessing the editor to do this.
let text = await this.app.vault.read(noteFile); 
Author: Binny V A
A philosopher programmer who specializes in backend development and stoicism.

1 thought on “Get current file content in Obsidian

  1. Hello

    I’m creating some templates in Obsidian using the Templater plugin and I have a question. Would you help me?

    Maybe I’m doing something more complex than my level of knowledge anyway…

    Objective: insert a template that takes the name of the current file, create a new file with the same name (with extra information inserted by the Templater’s tp.system.prompt function) and call a new template that will extract some metadata from the current name. After that, it renames the file that was created.

    It is in this step of renaming that I have problems.

    To create the new file (I couldn’t call the template directly in the tp.file.create_new function), then, I pass the template content directly through the Obsidian api (await app.vault.read(fTemplate)). This works for creating the new file, manipulating the metadata, etc. However, it does not run tp.file.rename.

    Would you have any suggestions? Or could you tell me where to find a more complete documentation of the Obsidian api (in a version for beginners, if possible lol)?

Leave a Reply

Your email address will not be published. Required fields are marked *