Templater
Templater copied to clipboard
sometimes can't jump to tp.file.cursor
Plugin information (please complete the following information):
- OS: windows 11
- Templater version: 1.16.0
- Obsidian version: 1.0.3
- Templater settings: tp.file.cursor
Describe the bug
tp.file.cursor
can't jump cursor to the position
cursor will jump to the begining of the template
The above bug doesn't appear in every note. In short notes (with little words), tp.file.cursor
works well, the bug will appear in bigger notes.
I don't know what triggers this bug, and I can't tell the word count limit, like over how many words will trigger the bug. Thus, I can't present a sample note.
Additional context
If any extra info is needed, I'm glad to present.
Does this happen on the latest version?
Yes, cursor will jump to before the first character of the template
But only happens in large files (contains a lot syntax like lists, tables, html, code blocks), I don't know why.
I couldn't reproduce it in new files
I wonder if this has to do with Obsidian not fully rendering large files on initial render, so the cursor will fallback to a default position if the position can't be found?
Does it only happen on large files when the tp.file.cursor
is far down in the file, or does it also happen on large files when the tp.file.cursor
should clearly be in view when first viewing the file?
I've been able to reliably replicate this issue.
Not working:
- If the file is created via a "new file creation" trigger, the cursor will not move and it will be in the title after the note opens. eg when right clicking a folder and clicking "New Note" in a folder that has a Template.
- If the current file has a template applied to it via the left toolbar Templater button, there will be no cursor in the note at all. I assume when clicking on the toolbar, focus never made its way back.
- Templates created via ctrl-p then going to "Create new note from template" using only the keyboard result in the title being highlighted, instead of the indicated cursor position (even if the title is set to something by the template).
Working:
- Templates created from a button using the Button plugin work just fine and position the cursor where you expect. Yay!
- Templates inserted via ctrl-p then going to "Insert Template modal" using only the keyboard work great!
- Templates applied via ctrl-p then going to "Replace templates in active file" using only the keyboard also are working no problem.
There are definitely more combinations to try, but this is what I have figured out so far.
Also linking in this older issue as it seems to be related: https://github.com/SilentVoid13/Templater/issues/397
@acook Thank you very much for your testing! I've decided to do test as well based on your finding to verify and to also learn about the issue.
Anyways, here's my results/findings:
Method | Image | Works? | Steps | Note | |
---|---|---|---|---|---|
1 | Obsidian Create New file | img![]() |
Yes (Conditionally). Click to see![]() |
1. Obsidian Create New File. 2. Let Folder Template do it's thing 3. Focus is at the title. Press enter after renaming. 4. <%tp.file.cursor(0)%> is replaced |
- Press Enter after editing Title - Require: Folder Template set up |
2 | Workspace Ribbon Button | img![]() |
Yes (Conditionally). Click to see![]() |
1. Click on Templater Ribbon Button. 2. Let Folder Template do it's thing 3. Click on Tab to focus. 4. <%tp.file.cursor(0)%> is replaced |
- Click on tab to focus to the jumped cursor - Require: Folder Template set up |
3 | Templater: Create New Note From Template | img![]() |
Yes. Same as #\1. Press Enter at Title |
See 1st row | - Press Enter after editing Title |
4 | Templater: Insert Template modal | img![]() |
Yes | ||
5 | Templater: Replace templates in active file | img![]() |
Yes | ||
Other | |||||
6 | Using Button Plugin |
So, for me on windows, it seems like tp.file.cursor(0)
does work as intended. For some you just need to press enter from the title or click the tab to focus in the document (albeit this one is a bit annoying).
Interesting! Thanks for looking into it.
I am on Linux/X11/Budgie, not sure if the cursor behaviour is any different on that setup or not.
- Confirmed, the cursor jumps appropriately after enter!
- Confirmed, if I click only the tab, the cursor does indeed jump to the correct location.
- As 1, after enter.
- :+1:
- :+1:
- :+1:
Looks like everything is the same, which is a relief.
I think the trouble is that Obsidian acts differently in each scenario, like the difference between 1 and 3 is that the title either has a cursor or has the title fully selected too, so it's harder to hook into I assume. This could be mentioned in documentation and I think that is all the fix most people would need. (it solves my use case at least)
Probably scenario 2 is the one that is the most vexsome for people, so that is the one I would vote gets attention when someone has time. It would involve telling Obsidian to change focus to the tab, not sure how troublesome that would be.
是的 v1.16.0
I repeated the same question
So this one's been vexing me for a bit, but I've run down what I think are some related causes.
- I believe it's partially related to 410c3a886e117e6a79849190aa0e9a9e64e56608 with the commit message:
fix(cursorjumper): fix issues with jump cursor stealing focus away from the 'rename file' text field The cursor jumper should not override the editor focus. This was interfering with the "create new file" flow. Focus was being stolen from the rename file field and the cursor moves to the first jump location preemptively.
- I would guess that this leads to the behavior some users experience where the cursor starts in the title bar when the template employs either
tp.file.move()
ortp.file.rename()
. Namely, my experience is:- Upon Templater completion, the cursor is in the note title field (the title I've provided).
- Pressing tab jumps me to
tp.file.cursor()
ortp.file.cursor(1)
, etc.- Note that my keybinding for the jump may not be standard--honestly can't remember 🙃
- My hypothesis is that the above commit, preventing the
cursorjumper
from stealing focus from the "'rename file' text field," is interfering with the way a user would prefer it to work in the case that they have already renamed the file. - Note, also, that I am employing Templater's Folder Templates functionality to auto-trigger the template on new file creation.
Possible Fix
My initial, naive thought is that a string comparison could be done to determine whether the file has been given a non-default name and use this to control whether the cursor starts in the title or at the first tp.file.cursor()
. E.g.,
if note.title != "Untitled"; then jump_to_first()...
Although, perhaps Obsidian provides a more clever way to do this.
Unfortunately, I am not experienced writing typescript/javascript nor am I very familiar with the Obsidian API, so it would take a lot for me to work out a fix (doesn't mean I won't eventually get motivated and give it a shot).
So... anyone up for looking into it? I'm happy to lend a hand if it's helpful.
Here's a minimal reproducer, in case that's helpful.
# New Template Note
<%*
let noteName = await tp.system.prompt("What is the name of the note?")
await tp.file.move("noteFolder/" + noteName)
let dueDate = await tp.user.parseDueDate_NL(tp)
let reminderDate = await tp.date.now("YYYY-MM-DD", -7, dueDate, "YYYY-MM-DD")
_%>
---
due_date: <% dueDate %>
date_added: <% tp.file.creation_date("YYYY-MM-DD") %>
---
- [ ] #task <% noteName %> ⏰ <% reminderDate %> 📅 <% dueDate %>
---
## Notes
- <% tp.file.cursor() %>