[Hotkeys] Assigning "longer sequence of the key combinations"

What I’m trying to do

Assign a “longer sequence of the key combinations”. Straightforward question:

How can I assign this sequence to some key combination? Ctrl + E + Ctrl + Home + Ctrl + E + Ctrl + Home (Windows)

I don’t have a separate heading for the note title, so it bothers me when I click Ctrl + Home that it doesn’t jump to the top of the note but to the top of the editing area. Which I understand, but I still want to go with my way :slight_smile:

Things I have tried

I tried a few plugins that promised to assign multiple key combinations, but none worked, or I didn’t know how.

So, I’m looking for ideas, plugins, or a solution. If not, I’ll suggest a feature request, which is an option to enable/disable this :stuck_out_tongue_winking_eye:

Cheers, Marko :nerd_face:

Make a Templater or other js script joining app.commands.executeCommandById() instances. But you’d need to await them or separate them with sleep so they don’t get executed at the same time.
Then you’d bind CTRL+HOME to this command.

1 Like

Not sure how to achieve this.

I made a user script, but I don’t know how to bind a key. I tried to call this function in one template, but it also created a new note. Or I’m barking into the wrong part of Templater :slight_smile:

Still new in Templater … ok, also in Obsidian :smile:

Cheers, Marko :nerd_face:

You are not showing what you cooked up, either.

Then you need to register the Templater file – if you picked Templater (but I meant .md script not .js) – in this section:

Okay, so the duplication of CTRL+E + CTRL+HOME is necessitated because you cannot go home in Reading mode, correct?
Then I don’t know…do you have Properties set to Visible? (I don’t.) Do you want to look at that if you do have it set to Visible…?

Then…even if you have a top level heading, you cannot (normally) go higher than the top of the editor, below the YAML (but as I said, I have Properties hidden and I’m mostly in Live Preview).
So if you absolutely must go to the very first line, that would be line zero that you can get to in Source Mode.

Using CodeScript Toolkit plugin (as my go-to plugin to make scripts, as I explained in that thread), I created a script that switches to source mode to be able to go to the absolute top possible and then switches to Reading mode (if that’s what’s required or you want editing mode?):

import { App, Plugin } from "obsidian";

const gotoFirstLine = async (app: App): Promise<void> => {
    const leaf = app.workspace.activeLeaf;
    if (!leaf) return;

    // Switch to source mode first
    const viewState = leaf.getViewState();
    viewState.state.mode = "source";
    viewState.state.source = true;
    await leaf.setViewState(viewState);

    // Wait for the state to fully apply
    await new Promise((resolve) => setTimeout(resolve, 100));

    // Get editor and move cursor to line 0
    const editor = leaf.view?.editor;
    if (editor) {
        editor.setCursor({ line: 0, ch: 0 });
    }

    // Switch to reading mode
    viewState.state.mode = "preview";
    await leaf.setViewState(viewState);
};

export class GotofirstlinePlugin extends Plugin {
    async onload() {
        this.addCommand({
            id: "go-to-first-line",
            name: "Go to first line",
            callback: async () => await gotoFirstLine(this.app),
        });
    }
}

// This function will work on both desktop and mobile
export async function invoke(app: App): Promise<void> {
    return gotoFirstLine(app);
}

Save this as Go to First Line.ts in the folder where you will store your CodeScript ts (TypeScript) files.

You can bind a hotkey of your choice to this script:

1 Like

@Yurcee, sorry for “disappearing”, but we probably have a time difference, and today I had a family trip :slight_smile:

Last night, I was tired, and yes, it was not enough or clear what and how I wanted. But actually, your previous post put me on the right path. Didn’t know about this plugin. Now, I was playing for an hour to figure it out.

Your script is working! But, due to a lack of information from my side, not as I wanted. I wanted this - “jump under the title” in Editing mode.

This is the TS that works for me now:

import { App, Editor } from "obsidian";

export async function customCtrlHome(app: App): Promise<void> {
  const activeLeaf = app.workspace.activeLeaf;
  if (!activeLeaf) {
    alert("activeLeaf not found!");
    return;
  }

  // Get the active editor
  const editor = activeLeaf.view?.editor as Editor;
  if (!editor) {
    alert("Editor not found!");
    return;
  }

  // Ensure the cursor is placed at the start
  editor.setCursor({ line: 0, ch: 0 });

  // Scroll slightly above the first line to reveal the title
  editor.scrollIntoView(
    { from: { line: 0, ch: 0 }, to: { line: 3, ch: 0 } },
    true
  );
}

// Register the function
export async function invoke(app: App): Promise<void> {
  return customCtrlHome(app);
}

The original script had 20 more lines :smile: as I was jumping on the top, at the beginning and so on. Then I found scrollIntoView(), which does exactly what I wanted. But before that, I setCursor() at the beginning of the note.

I marked your suggestion for the script as a solution, as … in my opinion … is the solution. The rest was to find which script :slight_smile:

Thanks for all your effort, and take care!

Cheers, Marko :nerd_face:

2 Likes

I don’t think so. I am on original Kornel David’s time, you are on Luka’s.

Approved!

I had an idea you’d be acing this. That’s why I added my bit.
Next time you’ll be all over Share&Showcase with .ts files. → Money. :stuck_out_tongue:

1 Like

OK, this is one of the best replies I ever heard! :smile: :ok_hand:

1 Like
1 Like

What kiddo he was, and look him now!

1 Like

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