SilentVoid13 / Templater

A template plugin for obsidian
https://silentvoid13.github.io/Templater
GNU Affero General Public License v3.0
3.23k stars 194 forks source link

tp.file.cursor(1) not working #397

Open Lorena1908 opened 3 years ago

Lorena1908 commented 3 years ago

Plugin informations (please complete the following information):

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.

SilentVoid13 commented 3 years ago

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

poacher2k commented 3 years ago

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

vitaly commented 3 years ago

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.

SimplGy commented 2 years ago

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?)
bjornfiles commented 2 years ago

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!

odyslam commented 2 years ago

It defaults to file name now

jonnymauser76 commented 2 years ago

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

shabegom commented 2 years ago

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

zubayrali commented 2 years ago

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.

merlinuwe commented 2 years ago

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

AB1908 commented 2 years ago

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!

shabegom commented 2 years ago

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

fringemonkey commented 2 years ago

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

apricot13 commented 1 year ago

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

- 
korniychuk commented 1 year ago

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.

gsommer commented 1 year ago

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
welpdx commented 1 year ago

@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.
gsommer commented 1 year ago

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?

welpdx commented 1 year ago

@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

gsommer commented 1 year ago

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.

welpdx commented 1 year ago

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

Robertullrey commented 1 year ago

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

mmusson-lktn commented 1 year ago

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

fredcallaway commented 1 year ago

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!

welpdx commented 1 year ago

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

fredcallaway commented 1 year ago

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 😛

geryxyz commented 1 year ago

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.

cs-mshah commented 1 year ago

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.

bryanwhiting commented 1 year ago

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) %>
rben01 commented 1 year ago

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.

danielo515 commented 1 year ago

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

Ok, I have to admit that this was being very confusing to me. I was expecting for all the appearances to be replaced with a space or subtle placeholder, and then to be able to jump to them with just tab. Seeing the template "unexpanded" made me thought it was not working.

On a sidenote, is there a way to use just tab, and if there is no place to jump, execute the normal tab in obsidian? Thanks

chewblacka commented 1 year ago

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.

I'm also getting this exact same behaviour. It was working fine until I added some code in my template to prompt for the title name of the new page and then rename. That part works as intended, but now instead of jumping to cursor(0) it highlights and stays at the title. If as you suggested I press [tab] then it goes to the correct location.

brunops commented 1 year ago

This doesn't work for me at all, I think I tried all possible combinations already .cursor(), .cursor(1), .cursor(2), etc.

What I tried: Confirmed that the setting is enabled, that templater works (the "hello" message does show up), ensure the command is triggered from templater and not the core templates

What happens in all cases is that the cursor command line doesn't show up, and the cursor never jumps to that line, not even when triggering the jump cursor command. Obsidian 1.3.7, templater 1.16.0

ps: I'm new to templater, so it's possible I'm missing something very basic, please let me know!

image

sanjarcode commented 1 year ago

The following works without problems:

  1. You want a single cursor - <% tp.file.cursor() %> is enough
  2. You want multiple cursor (and navigate between them) - <% tp.file.cursor(1) %> and others with their order number.

  In settings:

  1. Templater Settings > Automatic Jump on Cursor. Enable it.
    • Fixes the raw cursor code being printed
  2. Obsidian settings > HotKeys > Search for 'next cursor'.
    • This key is used for next cursor navigation.
    • Unfortunately just 'Tab' can't be set in macos. My default key is 'Option + Tab'. image
    • When you add a multi-cursor template. Initially, raw code is still printed for 2nd and remaining cursor, but the raw code will go away when you navigate to a cursor.

Code: \/templates/w2h.md

## Why
<% tp.file.cursor(1) %>

## How
<% tp.file.cursor(2) %>

## What
<% tp.file.cursor(3) %>
brunops commented 1 year ago

@sanjarcode I'm unsure if your reply was to my previous message, but if it was, it unfortunately doesn't work for me.

What does seem to work is inserting the template through the core templates command, and then pressing "option + Tab" (not templateR, which means the template code is visible, exactly like the first part of the image I shared above). That makes the cursor work, but nothing else in the templater code does.

If I insert the template with templateR, then I get the image on the right. Templater executes, no template code, but cursor simply at the end of the file (and pressing "option + Tab" does nothing).

A video might be easier to understand what I mean:

https://github.com/SilentVoid13/Templater/assets/1735844/53787ebb-58ab-44ec-9934-16ccd120712a

Fertion commented 12 months ago

It helped me with the same problem:

<% tp.file.cursor() %><%* app.workspace.activeLeaf.view.editor?.focus(); %>

tobalsan commented 5 months ago

Anyone succeeded in having the cursor placed directly where <% tp.file.cursor() %> is without having to press Tab?

Obsidian default behavior upon new note creation is to focus the title, and you only have to press tab/enter once to get to the cursor, but this defeats the whole purpose of <% tp.file.cursor() %>, especially when you have already renamed the file automatically via Templater.

connorhvnsen commented 2 months ago

Seeing the exact same issues. <% tp.file.cursor() %> doesn't focus cursor after file creation. Instead, title is focused and specified cursor location is toggled after hitting tab.