Template cursor placement

Use case or problem

I use a few LaTeX environment templates which often involve wrapping \begin{} and \end{} around what I am currently writing. Inserting a template always places the cursor at the end of the inserted text, and you can’t template around a current selection. I would like this functionality. I am sure there are a bunch of other use cases for this as well

Proposed solution

The {{@}} template tag will get special status such that:

  • {{@}} is replaced by the current selection
  • The cursor is placed immediately after the {{@}} tag after templating.

With no selection, this would simply have the effect of the cursor being placed at {{@}} within the inserted template.

With a selection, it is possible to implement templates that ‘wrap’ some preexisting content.

For example, to mimic the behaviour of the existing CTRL+B boldening feature, use the following template:

**{{@}}**

Or add a shortcut for putting the selection into inline math:

${{@}}$

To put the current selection into an align environment (or if no selection this effectively wraps the environment around the cursor):

\begin{align}
{{@}}
\end{align}

Combined with a template hotkey feature, this could even be used to implement some core functionality like turning things bold, italics, inserting links etc. This would have the benefit of allowing users to override that behaviour as well, such that people preferring __ style boldening to ** could implement that themselves.

Alternative implementation

Have {{@}} insert selection and {{!}} place the cursor. If multi-cursor support is then provided in the future, multiple cursors could be placed using a template.

For example, inserting a footnote in a clever way that uses the selection as the ‘key’:

[^{{@}}]

[^{{@}}]: {{!}}
1 Like

You could use either

  • the Templater plugin (it has multiple cursor insert points like <% tp.file.cursor(1) %>, <% tp.file.cursor(2) %>, …), or
  • the Text Snippets plugin, which has a configurable “cursor end position mark”. (Example: kbd : <kbd>$end$</kbd>)

For multi-line inserts, I’d currently prefer Templater, since Text Snippets still seems to have a little bug here.

Your example in Templater:

\begin{align}
<% tp.file.cursor(1) %>
\end{align}
2 Likes

Thank you so much for pointing me in the direction of Templater! I wasn’t aware that this could actually be achieved using that plugin.

I still think this change could make sense in the core Template plugin, but I am very glad to know that a community plugin can be used to get this already!

1 Like

This feature request is just what I was looking for, and happened to be opened just two days ago :slight_smile: . Good to know that there is a community plugin for this.

I agree to @JeppeKlitgaard that this could be beneficial also in the core plugin.

@Moonbase59
I just wanna say thank you for comment about Text Snippets. It fits perfect for my need and I didn’t know about the plug-in :slight_smile:

i am trying to use <% tp.file.cursor(1) %> and it just copies the code when i use it. i am doing in according to documentation but i thought maybe it is not uptodate or something, maybe it is a plugin capability with another plugin. i am so confused

Thanks for this hint.

I wanted to be able to select a bit of text and then wrap it in a url scheme using the selected text as the alias and the end of the url. Taking a chance with Templater and your snippet, I did this:

[<% tp.file.cursor(1) %>](https://www.example.com/search= <% tp.file.cursor(1) %>)

While my select and trigger did not work, inserting the template did display 2 editing cursors, and when I typed in the text I wanted, both cursors worked to show it. (Kind of like multi cursor editing in VS Code or Android Studio for those developers out there).

Ultimately, I was able to create a QuickAdd Capture with a bit of custom code that does what I was hoping: select a bit of text and use that selection to build the url.

DionV

sorry to jump in , but i want to do something similar. bringing the cursor to where i can select a heading in the link. in the code you send here looks like tp.file.cursor actually is supposed to act like tp.file.select that i am using.

the current for of my template:
<% tp.file.selection() %> [[<% tp.file.selection() %> - en-US | 📖]]

what i am trying and fails:
<% tp.file.selection() %> [[<% tp.file.selection() %> - en-US<% tp.file.cursor(1) %> | 📖]]