Templater
Templater copied to clipboard
tp.file.cursor(1) not working
Plugin informations (please complete the following information):
- OS: Windows 10
- Templater version: 1.9.9
- Obsidian version: v0.12.15
Describe the bug
When I insert a template that has <%tp.file.cursor(1)%>, this is not replaced by the cursor
Expected behavior This code should be replaced by the cursor
Screenshots Here is a screen recording
https://user-images.githubusercontent.com/80294221/135774090-4dcde689-d07f-4d4c-8a06-cb3d84e2978a.mp4
Extra information:
I noticed this happened because of the latest version: 1.9.9, because when I downgraded to the previous version (1.9.8) it worked properly.
Hey @Lorena1908, the automatic cursor jump is now disabled by default. You can enable it back in the settings of Templater.
Thank you for the great extension @SilentVoid13 !
That the automatic cursor jump needs to be enabled separately might be something worth mentioning in the docs. Being new to the extension, it puzzled me greatly that omitting the () printed out the function body, while having the () just did nothing. Even if the automatic cursor jump is disabled, I would expect to see the code block disappear, but I guess there exists some good reasons why this isn't so :)
well, it's ok for it to be configurable, but I think it's really not a good idea to add a setting with the default opposite of the current behaviour. and a dozen bug reports for the same issue is an obvious result.
It was confusing to me that it looks like it doesn't parse the entire code block. (eg: it prints the text of <% tp.file.cursor(1) %> in the output)
suggest instead to:
- replace with blank string in the output
- log a console warning/info if the setting is off
- turn the setting on by default (unless there's some downside to this?)
Whether it was broken before or not, it's definitely broken now for me using latest stable versions of plugin (1.12.0) and Obsidian (0.15.9) on Mac. Double-checked that I still have the setting set to on, but it's just showing the string and not replacing it. Please fix!
It defaults to file name now
Can confirm defaulting to file name for me after update to 1.14.1 for me this morning. Hope there is a fix for it
Obsidian's default behaviour has changed which is why this command no longer works as expected.
This is not working in v0.15.9, the hotkey "Templater: Jump to next cursor location" IS WORKING but not the automatic jump. Hoping there is a fix for this.
It costs me half an hour to figgle around and find this.
For me <% tp.file.cursor(1) %> does not work at all, but I'm new with templater. I need a precise guide (preferable in the docs).
Sorry to hear. It's still broken last I checked and it'll be a while before it gets fixed. If anyone wants to take a stab at it, I'm happy to guide them!
Also happy to merge a warning in the docs that this isn't working as expected right now.
It costs me half an hour to figgle around and find this.
For me <% tp.file.cursor(1) %> does not work at all, but I'm new with templater. I need a precise guide (preferable in the docs).
came here to say this. exact same situation
just to add that it does work when the note is created with templaters own cmd + p methods - but I was using the quickAdd plugin to create my templates and it doesn't work with that out of the box so if your using the quickadd plugin (or I assume another) enabling trigger template on new file creation will fix it.

my template
---
title: '<%tp.file.title%>'
tags:
visibility: private
created: <%tp.date.now("Y-MM-DDTHH:mm:ssZZ")%>
---
# <%tp.file.title%>
<%tp.file.cursor(0)%>
## Links
-
## Reference
-
For me, it's become work after I switched on the toggle "Automatic jump to cursor" in the plugin settings. Thanks, @apricot13 for the screenshot.
Now the plugin behaves next way:
- It shows for a moment "<% tp.file.cursor() %>" text, and then it is replaced with an empty string and the cursor is placed there.
- If I call "Undo" I can see the deleted "<% tp.file.cursor() %>" text.
Not very pretty, however, works. Better than nothing.
It does not work at all for me even with all the mentioned settings activated and the newest version of the plugin and Obsidian 1.1.9.
The cursor always stays at the start of the file in front of the file name, the line with tp.file.cursor(0) is highlighted and tp.file.cursor(1) is not replaced at all, but I can jump to it using the hotkey and it is replaced then.
@gsommer I think that is the intended behaviour. tp.file.cursor(0) is replaced first so that cursor is there. Then if you want to navigate to the tp.file.cursor(1), you use the hotkey.
From the documentation:
Sets the cursor to this location after the template has been inserted.
You can navigate between the different tp.file.cursor using the configured hotkey in obsidian settings.
order: The order of the different cursors jump, e.g. it will jump from 1 to 2 to 3, and so on. If you specify multiple tp.file.cursor with the same order, the editor will switch to multi-cursor.
But the actual cursor is not at cursor(0), so that I can start entering text there. It is at the beginning of the first line of the file.
The line where I positioned cursor(0) is highlighted, but when I start entering text it is entered at the beginning of the file and not at the first cursor position. There is not any usefulness in this.
If I cannot define the position where I want to enter text after the template has been expanded, what is the point of having a cursor?
@gsommer Oh I see. I thought the tp.file.cursor(0) was replaced and the cursor was it its place and it was just cursor(1) that was not replaced.
Questions:
- How is the file generated with the template? Are you creating a new file and letting the Folder Template take over? Or are you using the Templater Insert Modal (this usually has no problems)
- If you are creating a new file and the cursor is at the title, what happens after you press enter? Does the cursor go where tp.file.cursor(0) is?
Thanks
Edit: I tried all the different ways that one could use Templater to use tp.file.cursor(), here are my results. What method are you using to run the function? and are the results the same as mine or different? Thank you
The file is generated with "New Note".
Yes, if I press enter the curser jumps to the first cursor position. The second cursor shows as <% tp.file.corsor(1) %> until I press the hotkey.
With the Insert Modal the cursor is at the right place after inserting, but this is not how I want to open a note. Anyway it is an acceptable workaround for now. Thank you.
I don't like that the templater source code shows after creating the note. But I guess there is no other way to do this.
@gsommer
Although tp.file.cursor is not working the way you are hoping for, I am glad to hear that it is working as intended by the developer as described in the manual.
Maybe we will see changes to how it works in the future.
Still not working. Obsidian 1.1.16, Templater 1.16.0. "Template parsing error, aborting."
<% tp.file.cursor(1) %>
Using Templater insert mode. Both command-p and "insert modal".
I discovered this is caused by the editor option Advanced / Auto convert HTML. When I turn that off, exit settings, then turn it back on, the cursor positions as expected without displaying the <% tp.file.cursor(1) %>.
I'm finding that <% tp.file.cursor(1) %> works fine but then <% tp.file.cursor(2) %> does not. If I start with 0, then 1 doesn't work. But if I have multiple of the same number, it correctly creates two cursors. Odd!
@fredcallaway pics/gif or it didn't happen XD
Ack sorry I figured it and forgot to report back. When I saw the <% tp.file.cursor(2) %> in the file I assumed it wasn’t parsed. Maybe worth mentioning that this is expected behavior in the docs for other naïve users like me 😛
Ack sorry I figured it and forgot to report back. When I saw the
<% tp.file.cursor(2) %>in the file I assumed it wasn’t parsed. Maybe worth mentioning that this is expected behavior in the docs for other naïve users like me 😛
IMHO, this should be mentioned in the documentation. I am a software engineer, and even I needed clarification on this.
This doesn't work when we insert a template from the sidebar. I believe this is because the active context is no more the editor. It works fine when we add a shortcut and keep the cursor in the editor. The sidebar template selection should fix the <% tp.file.cursor(1) %> behaviour.
What I noticed is that it works for me, but Obsidian now defaults to putting the cursor on the document title by highlighting the title. All I need to do now is hit "Tab" and it jumps to where it should be.
Potential solution
Template
_TEMPLATE_TITLE
entry1:: <% tp.file.cursor(1) %>
entry2:: <% tp.file.cursor(2) %>
will be rendered as
Untitled <--- HIGHLIGHTED /Active context
entry1::
entry2:: <% tp.file.cursor(2) %>
Hit "tab" and:
Untitled
entry1:: |
entry2:: <% tp.file.cursor(2) %>
I'm finding that
<% tp.file.cursor(1) %>works fine but then<% tp.file.cursor(2) %>does not. If I start with 0, then 1 doesn't work. But if I have multiple of the same number, it correctly creates two cursors. Odd!
FYI multiple cursors is the expected behavior:
order: The order of the different cursors jump, e.g. it will jump from 1 to 2 to 3, and so on. If you specify multipletp.file.cursorwith the same order, the editor will switch to multi-cursor.