Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Lorena1908 opened this issue Oct 3, 2021 · 37 comments
Open

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

Lorena1908 opened this issue Oct 3, 2021 · 37 comments
Labels
bug Something isn't working

Comments

@Lorena1908
Copy link

Lorena1908 commented Oct 3, 2021

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

Obsidian.-.Obsidian.v0.12.15.2021-10-03.19-51-48.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 Lorena1908 added the bug Something isn't working label Oct 3, 2021
@SilentVoid13
Copy link
Owner

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

@poacher2k
Copy link

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
Copy link

vitaly commented Oct 12, 2021

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
Copy link

SimplGy commented Jan 4, 2022

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
Copy link

bjornfiles commented Aug 30, 2022

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
Copy link

odyslam commented Sep 4, 2022

It defaults to file name now

@jonnymauser76
Copy link

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
Copy link
Collaborator

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

@zubayrrr
Copy link

zubayrrr commented Oct 7, 2022

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
Copy link

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
Copy link
Collaborator

AB1908 commented Oct 11, 2022

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
Copy link
Collaborator

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

@fringemonkey
Copy link

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
Copy link

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
Copy link

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
Copy link

gsommer commented Jan 10, 2023

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
Copy link
Contributor

welpdx commented Feb 4, 2023

@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
Copy link

gsommer commented Feb 4, 2023

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
Copy link
Contributor

welpdx commented Feb 5, 2023

@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
Copy link

gsommer commented Feb 5, 2023

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
Copy link
Contributor

welpdx commented Feb 5, 2023

@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
Copy link

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
Copy link

mmusson-lktn commented Mar 30, 2023

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
Copy link

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
Copy link
Contributor

welpdx commented Apr 14, 2023

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

@fredcallaway
Copy link

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
Copy link

geryxyz commented Apr 25, 2023

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
Copy link

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
Copy link

bryanwhiting commented Jul 14, 2023

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
Copy link

rben01 commented Jul 30, 2023

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
Copy link

@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
Copy link

chewblacka commented Aug 10, 2023

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
Copy link

brunops commented Sep 8, 2023

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
Copy link

sanjarcode commented Sep 10, 2023

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
Copy link

brunops commented Sep 10, 2023

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

Screen.Recording.2023-09-10.at.3.35.50.PM.mov

@Fertion
Copy link

Fertion commented Oct 26, 2023

It helped me with the same problem:

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

@tobalsan
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests