Templater icon indicating copy to clipboard operation
Templater copied to clipboard

tp.file.cursor(1) not working

Open Lorena1908 opened this issue 4 years ago • 39 comments

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.

Lorena1908 avatar Oct 03 '21 22:10 Lorena1908

Hey @Lorena1908, the automatic cursor jump is now disabled by default. You can enable it back in the settings of Templater.

SilentVoid13 avatar Oct 04 '21 08:10 SilentVoid13

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 :)

poacher2k avatar Oct 04 '21 13:10 poacher2k

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.

vitaly avatar Oct 12 '21 07:10 vitaly

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:

  1. replace with blank string in the output
  2. log a console warning/info if the setting is off
  3. turn the setting on by default (unless there's some downside to this?)

SimplGy avatar Jan 04 '22 05:01 SimplGy

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!

bjornfiles avatar Aug 30 '22 20:08 bjornfiles

It defaults to file name now

odyslam avatar Sep 04 '22 09:09 odyslam

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

jonnymauser76 avatar Sep 15 '22 13:09 jonnymauser76

Obsidian's default behaviour has changed which is why this command no longer works as expected.

shabegom avatar Sep 17 '22 14:09 shabegom

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.

zubayrrr avatar Oct 07 '22 03:10 zubayrrr

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).

merlinuwe avatar Oct 11 '22 15:10 merlinuwe

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!

AB1908 avatar Oct 11 '22 15:10 AB1908

Also happy to merge a warning in the docs that this isn't working as expected right now.

shabegom avatar Oct 12 '22 03:10 shabegom

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

fringemonkey avatar Oct 21 '22 19:10 fringemonkey

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.

image

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

- 

apricot13 avatar Nov 01 '22 16:11 apricot13

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:

  1. It shows for a moment "<% tp.file.cursor() %>" text, and then it is replaced with an empty string and the cursor is placed there.
  2. If I call "Undo" I can see the deleted "<% tp.file.cursor() %>" text.

Not very pretty, however, works. Better than nothing.

korniychuk avatar Jan 02 '23 08:01 korniychuk

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.

2023-01-10_16h59_52

gsommer avatar Jan 10 '23 17:01 gsommer

@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.

Arguments

  • 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.

welpdx avatar Feb 04 '23 21:02 welpdx

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 avatar Feb 04 '23 22:02 gsommer

@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:

  1. 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)
  2. 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

welpdx avatar Feb 05 '23 05:02 welpdx

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 avatar Feb 05 '23 11:02 gsommer

@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.

welpdx avatar Feb 05 '23 14:02 welpdx

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".

Robertullrey avatar Mar 27 '23 23:03 Robertullrey

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) %>.

mmusson-lktn avatar Mar 30 '23 18:03 mmusson-lktn

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 avatar Apr 10 '23 16:04 fredcallaway

@fredcallaway pics/gif or it didn't happen XD

welpdx avatar Apr 14 '23 02:04 welpdx

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 😛

fredcallaway avatar Apr 14 '23 02:04 fredcallaway

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.

geryxyz avatar Apr 25 '23 09:04 geryxyz

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.

cs-mshah avatar May 25 '23 07:05 cs-mshah

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) %>

bryanwhiting avatar Jul 14 '23 20:07 bryanwhiting

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 multiple tp.file.cursor with the same order, the editor will switch to multi-cursor.

rben01 avatar Jul 30 '23 15:07 rben01