Custom Command / Macro for Replacing Certain Phrase (for Matlab/Octave syntax highlighting)

Things I have tried

I have tried searching Obsidian and reddit forum, as thought one of the hotkey plugins might have what I am looking for, but no success yet.

What I’m trying to do

I am documenting some MATLAB code using codeblocks (```) for script, with images and further explanation between each block.

The ‘Editor Syntax Highlight’ plugin, supports Octave highlighting in edit mode, but sadly not in display view. The built-in syntax highlighting is the opposite showing highlighting using ```matlab in display view but not edit mode. Neither of them are amazing (presumably due to MATLAB being less open / popular than python etc), but it is still fairly helpful so a workaround would be working with ```octave and then replacing all blocks to ```matlab at the end.

Question: Is there a way I can create a custom command or key shortcut to find and replace all occurences of a particular word. In my case, a command that replaces ```octave with ```matlab for all occurences in the file?

Thanks for any help!

I imagine this is possible with a Templater script, which lets you execute arbitrary Javascript in the app. Something like: get the note text, replace oldString in note text with newString, write new note text to the note.

Use the Hotkeys for Templates plugin to bind such a template to a keyboard shortcut.

In the meantime, Obsidian does have a conventional find-and-replace feature.

1 Like

Thanks @ryanjamurphy. I had a look at Templater and believe this is the way to go for now.

Sadly, I have never touched Javascript. I tried the below with no success.

oct2mat.js (js file in TemplaterJS folder)

function oct2mat(tp) {
    string=tp.file.content;
    string.replace(```octave, ```matlab);
    string.replace(``` octave, ``` matlab);
    return string;
}

oct2mat_syn (Template in tempate folder)

<% tp.user.oct2mat(tp) %>
<% tp.file.cursor(1) %>
<% tp.file.cursor_append(string) %>

I will also need to delete existing content before replacing with updated, but couldn’t even get this first step working. Console returns:
Template parsing error, aborting. missing ) after argument list

If anyone here can assist that would be appreciated! Thanks!

You’re actually pretty close, but you still need to use Obsidian API functions to manipulate the contents of the doc—return won’t do it because Templater doesn’t actually do anything with the returned output.

I don’t have the time to edit your code right this second, but I’ve done something similar with this. You might be able to use what’s in there to fix what you’ve done already.

Tried playing around and looking at various options. Turns out that the simple example I started with no longer works. Example found here: https://publish.obsidian.md/shabegom/Publish/How+To+Use+Templater++JS+Scripts

This did originally give the notice when I ran it originally yesterday. However now I receive the same error: Template parsing error, aborting. missing ) after argument list

So turns out I have a more fundamental issue to deal with first. I have setup Templates folder and TemplaterJS folder, with the .js files matching correct code.

Having searched about a few others have experienced this due to not have filenames correct / folders setup, but I can’t get it working.

No need to save things as .js files anywhere with Templater.

I haven’t tested the following, but it should look more like:

<%*
var selectedText = await this.app.workspace.activeLeaf.view.editor.getSelection();
selectedText = string.replace(“```octave”, “```matlab”);
selectedText = string.replace(“``` octave”, “``` matlab”);
this.app.workspace.activeLeaf.view.editor.replaceSelection(selectedText);
%>

Save that as a note in your Templater folder, including the opening <%* and closing %>.

1 Like

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