Templater - Move cursor to beginning of the document after file rename

I need a template that can:

  • Add the date to the beginning of the file name followed by a hyphen (2024-09-02)
  • Make the cursor jump to the first line in the document

I have tried:

<%*
let filename = tp.file.title
if ( filename.startsWith("Untitled") ) {
  await tp.file.rename(filename)
  tp.file.rename(tp.date.now("YYYY-MM-DD")+[" - "])
}
%>
<%*
tp.file.cursor(0)
%>

But I can’t get it to move the cursor to the beginning of the document.

Please advise.

here is the solution :slightly_smiling_face:

<%*
const date = tp.date.now("YYYY-MM-DD");
await tp.file.rename(`${date}-${tp.file.title}`);
const editor = app.workspace.activeLeaf.view.sourceMode.cmEditor;
editor.setCursor({ line: 0, ch: 0 });
%>
1 Like

Thanks. Did you perhaps copy-paste this result? Because this doesn’t work and it produces wrong title.

Untitled design(1)

That works perfectly (in my local vault). Thanks for sharing. :tada:

1 Like

If I right click a folder and select “New note” Obsidian makes a new note, templater inserts the date, but the cursor is stuck in the title right before the date. Not sure why this happens. Also, if I right click the folder and select New note from template I get a “Template parsing error”.

you can put the script in the note template and it will work well :slightly_smiling_face:

Yes, I have made a template, pasted your code. When I run it it does not work. Also, I am not sure what you did in your GIF it looks like you are using a completely different method. You are replacing the template whilst I am trying to make a folder template.

Well, here’s the final solution :slightly_smiling_face:

<%*
const date = tp.date.now("YYYY-MM-DD");
const newFileName = `${date}-${tp.file.title}`;

// Check if the file already exists
const existingFile = app.vault.getAbstractFileByPath(newFileName + ".md");
if (!existingFile) {
    await tp.file.rename(newFileName);

    // Wait for the editor to be ready
    await new Promise(resolve => setTimeout(resolve, 100));

    const editor = app.workspace.activeLeaf.view.sourceMode.cmEditor;
    editor.setCursor({ line: 0, ch: 0 });
} else {
    new Notice("File with the same name already exists!");
}
%>

I am not sure why but it still does not move the cursor automatically and it also adds “Untitled” after the date.

How do you create a new note ? I tried it with QuickAdd and worked well !

I am right-clicking a folder and selecting New note. I am also using this as a folder template.

Here is the ultimate solution :dizzy:

<%* 
// Your file renaming logic here
const newFileName = `${tp.date.now("YYYY-MM-DD")}-${await tp.system.prompt("Enter the Note Name")}`;
await tp.file.rename(newFileName);

// Move cursor to the beginning of the note
tp.hooks.on_all_templates_executed(async () => {
    const activeLeaf = app.workspace.activeLeaf;
    if (activeLeaf) {
        const editor = activeLeaf.view.editor;
        editor.setCursor(0, 0);
        editor.focus();
    }
});
-%>

I tried my solution in a folder template as you do, and it worked.