Create a simple command to make a new file at root and place cursor on specific line

What I’m trying to do

Hi all, I’m trying to create a very simple template that lets me make a new file at root (/), lets me name it, creates the first line of the file with “# filename” then places the cursor automatically on line 3 ready for me to type.

Things I have tried

I’m not sure if I’m overcomplicating the problem. I’ve tried making the below templater script to create a new file:

<%* let filename = await tp.system.prompt("Filename: "); await tp.file.create_new(tp.file.find_tfile("NewFile"), filename, 1, app.vault.getAbstractFileByPath("/")); %>

The “NewFile” template it calls is simply this:

# <%tp.file.title%>

<%tp.file.cursor(0)%>

This appears to ALMOST work. It prompts me for a filename, makes a new file in root. The only thing it doesn’t do is automatically place my cursor at the tp.file.cursor location. Instead my cursor is placed up in the top bar of Obsidian renaming the file. I have to press enter before it jumps down to the cursor location. Is there any way to make it automatically go to the cursor? (I have ‘Automatic jump to cursor’ on in templater settings)

Alternatively, is there a simpler method to achieve what I’m trying to do?

1 Like

:scream:

I live in Templater, and I never knew that setting was there. I was always wondering why I couldn’t get the cursor command to work!

Sounds like you don’t really live there, just pay rent. And it’s a box room.
:laughing:

Refer my original post. I have this setting enabled already and it doesn’t work. The cursor still stays in the rename area using the script I posted.

Well, it works for me.

I have a “work note” template which includes (“extends”) the general “note” template – this one does all sorts of manipulations: renames the note if necessary, moves it to a specific folder if needed, applies the general YAML section, etc. Once that work is done, the “work note” adds more specifics and has the <% tp.file.cursor() %> interpolation command at the position I want it to be. Templater finishes the job by jumping to that anchor if the setting I posted earlier is activated.

If that does, for whatever reason, not work, I can still trigger the jump with the Templater: Jump to next cursor location command either via the command pallet or the shortcut I defined.

The cursor must be active in the note leaf for all this to pan out. Why your cursor is in the (re-) name field is unclear to me.

I doubt that the following will have an effect, but try to change the open_new: boolean argument to true instead of 1.

How do you trigger your original template? The one with the prompt for new file name.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.