Quick navigation through text (by some hotkey if possible)

Things I have tried

I’ve searched as much as possible trying different words to find a way to quickly navigate through text to predetermined “anchors” (a string of characters I’d be unlikely to use in actual writing).

I’ve been unable to find any relevant information due to discussion about markdown anchors and a lack of demand for something like I’m describing as far as I can tell.

What I’m trying to do

I’m a mathematics student and I use obsidian for my own personal life but also to write up and store my lecture notes. When doing this, I make use of a snippets plugin to expand things like ‘pmatrix’ to the latex
$$
\begin{pmatrix}
& \
&
\end{pmatrix}
$$
This helps speed up my typing of notes but it means once I expand, despite being able to place where my cursor initially goes (in front of the first & in this example), I can’t navigate to the other 3 spaces I’d like to input the matrix data to without clicking or a lot of clunky arrow navigation. I was hoping I could find some way of putting some kind of “jump anchor” such that when I press tab or some other hotkey of choice, it jumps to that character/string of characters. This would allow much quicker navigation when using snippet expanding with variables that need filling in.

If anyone has used texmaker or texstudio, the circles that appear after tab expanding things like \frac to \frac{•}{•} are what I’m trying to describe.

In addition, it would be useful outside of the latex usecase described above as if somebody is filling out templated notes frequently, being able to jump within the notes to the next input space would be handy.

Expected behaviour:
Ideally this “jump anchor” wouldn’t show up in edit mode and would only exist to be jumpable to (though this isn’t the biggest issue).
Furthermore, in terms of the order of navigation, I would assume you would jump to the next “jump anchor” in order of text with a loop to the start if there is none left in the remainder of the document.

Please do let me know if there is anything like what I’m describing out there! If not I’ll happily make this a feature request.

Hi @mayurankv ! I’ve just started learning javascript and this was a fun little learning experience for me. Here is a script that will do that for you.

Script: Jump to next text anchor
<%*
// OPTIONS
const anchor = "$";
const wrap = true;

// CODE
const editor = app.workspace.activeLeaf.view.editor;
const edPos = editor.getCursor();

const content = await app.vault.read(app.workspace.getActiveFile())
const lines = content.split("\n");

let l = edPos.line;
let ch = lines[l].indexOf(anchor, edPos.ch);
if (ch==-1) {
	for (l++; l < lines.length; l++) {
		ch = lines[l].indexOf(anchor);
		if (ch!=-1) break;
	}
}
if (wrap && l == lines.length) {
	console.log("Wrapping around to beginning to find anchor.")
	for (l=0; l <= edPos.line; l++) {
		ch = lines[l].indexOf(anchor);
		if (ch!=-1) break;
	}
}
if (l<lines.length) editor.setCursor(l, ch+anchor.length);
%>

How to implement

  1. Install two plugins: Templater & Hotkeys for templates
  2. Create a file in your template folder (specified in Templater settings)
  3. Copy and paste the above script into that file (don’t forget the <%* and %> tags)
  4. Enable that template in “Hotkeys for templates” settings
  5. Assign a hotkey to the command via Obsidian settings > Hotkeys

You could also set it up as a macro command in QuickAdd. Similar idea, but a bit more complicated with QuickAdd.

Not offended in the slightest if you don’t use my code, or if you or anyone has a way to make it better!

Demo:

jump-to-anchor

1 Like

Thank you! This works a dream! One thing that would be amazing would be for the anchor to be selected rather than the cursor positioned afterwards (so could then type straight away). Would that be possible? What would I need to change to do that?

I’m not familiar with javascript so I wasn’t able to find an alternative to the .setCursor function with selection.

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