Highlight Text Surround with Template

Hello everyone, I dont know if this is a thing or not. I’ve been using Templater for awhile now and LOVE IT. Once drawback i have is that when I Insert a template i can insert it wherever the cursor is.
Is there a way to highlight text and have a template surround the highlighted text?

This would be such a huge benefit for me!
For example: Here is 1 of my templates:

< span class=“fs12 orange”></ span>

If I can have some text and have this surround that text when I have it highlighted

< span class=“fs12 orange”>Text Highlighted Here< /span>

Thank you everyone.

1 Like

The Templater template below will surround the currently selected text with the desired HTML code to highlight that text.

tR = `<span class="fs12 orange">${tp.file.selection()}</span>`;

You can assign this to a keyboard shortcut to make it easy to trigger this action.

You could also make it a little more complicated by having the template ask what color the highlight should be (this assumes you’ve defined the various colors in your CSS).

const colors = ['orange','yellow','pink','blue','green'];
let color = await tp.system.suggester(colors,colors,false,'Color');
if (color !== null)
   tR = `<span class="fs12 ${color}">${tp.file.selection()}</span>`;

omg dude that sounds awesome!! I may want some more info from you! I didnt try this yet but I am going to this afternoon as soon as I get some free time.
Currently I have many templates setup all of them are like this:

<span class="fs12 red"></span>

Its for size and color. I have to set them specifically to have a font be what I choose. I would always have to then click in the middle to type the text.

Could you explain a little more about the complicated template you gave me.
What does this line mean.

let color = await tp.system.suggester(colors,colors,false,'Color');

Then what does the ‘tR’ mean that the line of text equals?

I’m very excited to give this a try. it looks like it could make my note taking experience so much easier

So I have not been successful at doing the simple Selection and surrounding my text.

I have a line of text selected in my note.
My Template looks like this:

tR = '<span class="blue">${tp.file.selection()}</span>';

I also tried this: as I was able to get the cursor to move to its position with one similar:

<span class="blue"><% tp.file.selection() %></span>

Once its selected I then insert my template and it just displays all the text in either template and removes the selected text.

Could you help me with this by chance?
thank you

Sorry, I was away on a business trip and not checking the forum.


The problem with your ‘My Template’ is that you used single quote characters when it needed to be backtick characters (on my QWERTY keyboard it’s the key to the left of the 1 key)

So instead of

tR = '<span class="blue">${tp.file.selection()}</span>';

it needs to be

tR = `<span class="blue">${tp.file.selection()}</span>`;


The tR is a Templater variable. Whatever gets assigned to it is the text that the template outputs. It can be used to build up more complicated outputs.


Breaking down the more complicated script:

const colors = ['orange','yellow','pink','blue','green'];

This line defines an array of strings that are the names of colors. The list is just an example. You can add or remove colors as works for you.

let color = await tp.system.suggester(colors,colors,false,'Color');

This line uses Templater’s suggester function to display a list of options from which the user can select one item. In this case, the the list of options will be the names of the colors which were defined in the previous line. Whichever color is selected will be assigned to the color variable.

if (color !== null)

When suggester displays the list of options it is possible for the user to abort without having picked one of the options by hitting the ESC key. If this happens, color will get a value of null. The test performed on this line means that the template will only make the change if the user has selected a color.

   tR = `<span class="fs12 ${color}">${tp.file.selection()}</span>`;

This line wraps the current selection with an HTML span. This span will have two classes: ‘fs12’ and whichever color was selected.


Thank you! I got it working but under a weird circumstance.

When I bring up my Shortcut to insert templates I have it set as Alt+Shift+I and I select a template it overwrites the selected text.

If I use Ctrl + P and insert the template it surrounds the text just like I want.

Is there a way I can use my normal insert templates option to do the Ctrl + P Insert?

By the way you explained it amazingly well, This is going to make my life so much easier with templater. This opened up many new ideas for me.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.