Is there any way to make the line where the cursor is located back to the center/top of the screen?

What I’m trying to do

When I try to update a long note, I need to scroll up and down to read the relative contents. And consequently, after a few times, the line where the cursor is located may be scrolled outside the screen display. And it is not easy, in this circumstance, to figure out where the cursor is and which line you are updating now, even if the cursor does not move at all and is still in that line.

Things I have tried

I’d like to find a hotkey or something, so that the line where the cursor is can be seen on the center or top or bottom of the screen.

FYI, in Emacs this feature is known as Recentering and it is bounded to the hotkey C-l (ctrl-l)

C-l
Scroll the selected window so the current line is the center-most text line; on subsequent consecutive invocations, make the current line the top line, the bottom line, and so on in cyclic order. Possibly redisplay the screen too (recenter-top-bottom).

more information:
Recentering (GNU Emacs Manual).

Use the following QuickAdd user script!

  1. Create recenter.js in your vault.
// recenter.js
module.exports = ({ app }) => {
    const editor = app.workspace.activeEditor?.editor;
    if (!editor) return;
    editor.scrollIntoView(
        {
            from: editor.getCursor('from'),
            to: editor.getCursor('to')
        },
        true
    );
}
  1. Create a Macro choice using this user script (detailed instruction here):

Don’t forget to click the thunder icon to make this macro runnable as a standalone command:

  1. Set a hotkey for this macro.

You can find more information about the Editor interface of the Obsidian API here:

4 Likes

Ohhh, thanks!
This is really unexpected.
I don’t know that QuickAdd can be a JS code container!

It is convenient…though a little bit risky, as one can put any code there and invoked by the QuickAdd, right?

I will have a try!

1 Like

Right, so you shouldn’t actually run it until you understand what it does.
If you wish, you can delete the whole vault with your custom macro (though it’s an extreme example).

I think this method is the fastest way to develop a mini/tiny plugin :smiley:

Can I use TS?

Yeah, I totally agree!

For example, by turning on “Run on plugin load” you can add your custom editor/file context menu items, Obsidian event handlers (e.g. vault.on("create", ...)), and so on.

That’s why: Is it possible to modify Obsidian's default behavior with code? - #7 by ush

I think you can’t, unfortunately :cry:

Typewriter Scroll plugin centers the caret if I’m understanding correctly.

There was also an open feature request for “scrolloff” behaviour like in Vim which doesn’t strictly center, but adds a configurable margin at top and bottom.

I haven’t heard of this plugin until now, and it looks awesome!

But what this plugin does seems a little bit different from recentering to me.

The plugin always keeps the cursor in the middle of the screen. On the other hand, recentering scrolls the viewport so that the current cursor is placed in the middle when the recentering command is executed. And after that, the cursor can go away from the center.

1 Like

It works!

Just provide more screenshots for the people who uses the Macro for the first time.
in Step 2, creating a Macro choice means:
2.1 in the QuickAdd Settings panel, config as what is shown in the bottom of the screenshot, and press Add Choice:

2.2 Press the Gear Icon next for the newly added “Recenter” line, to config the Macro Choice.

Then:
2.2.1 Press the “+” button to create a Macro named “Recenter” automatically (sorry for the typo)

2.2.2 Select the newly created “Recenter” and press the Gear Icon next to it

and you will come to:

2.3 Add(Choose actually) the User Scripts which is created, in this case it is “recenter”(for the recenter.js) and press Add

After Step 2.3, you should see:

Then follow the instruction provided by @ush and go to Step 3.

Don’t forget to click the thunder icon to make this macro runnable as a standalone command:

Another reminder is, there are 3 things are created:

  1. the recenter.js which is located in your vault, wherever you want(but it cannot be in the .obsidian folder)
  2. a Macro named Recenter, which can be managed by press the Manage Macro button on the QuickAdd setting panel
  3. a Macro Choice named ‘Recenter’, which is different from the Macro, and this Macro Choice can be managed directly on the QuickAdd setting panel.

A recommendation is:
rename the Macro from Recenter to Recenter_macro to distinguish the Macro and the Marco Choice better.

3 Likes

Thank you!
I used that plugin before. But I think the scenario/requirement here is different.
I think what I want is something that can make the editor jump back to where I was typing the text whenever I asked.

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