Skip to content

Markdown keeps scrolling to a specific position when switching tabs #317033

Description

Type: Bug

  1. Open a markdown file in preview mode and scroll to a position
  2. Switch to the text editor mode for the markdown file
  3. Now scroll to a different position
  4. Switch tabs away from the markdown file and then back to the markdown file
  • Expected behaviour: editor does not scroll when switching back
  • Actual behaviour: editor always scrolls back to the position it was in the preview

VS Code version: Code 1.120.0 (0958016, 2026-05-12T20:17:22Z)
OS version: Windows_NT x64 10.0.26200
Modes:

System Info
Item Value
CPUs 13th Gen Intel(R) Core(TM) i5-1345U (12 x 2496)
GPU Status 2d_canvas: enabled
GPU0: VENDOR= 0x8086, DEVICE=0xa7a1 [Intel(R) Iris(R) Xe Graphics], DRIVER_VENDOR=Intel, DRIVER_VERSION=32.0.101.7084 ACTIVE
GPU1: VENDOR= 0x1414, DEVICE=0x008c [Microsoft Basic Render Driver], DRIVER_VERSION=10.0.26100.8246
Machine model name:
Machine model version:
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
skia_graphite: disabled_off
trees_in_viz: disabled_off
video_decode: enabled
video_encode: enabled
webgl: enabled
webgl2: enabled
webgpu: enabled
webnn: disabled_off
Load (avg) undefined
Memory (System) 15.66GB (2.29GB free)
Process Argv
Screen Reader no
VM 0%
Extensions (35)
Extension Author (truncated) Version
copilot-chat Git 0.48.1
ruff cha 2026.42.0
vscode-cylc cyl 1.3.0
shebang-language-associator dav 1.4.0
vscode-eslint dba 3.0.24
gitlens eam 17.12.2
vscode-github-actions git 0.31.5
vscode-pull-request-github Git 0.144.0
git-graph mhu 1.30.0
black-formatter ms- 2026.4.0
debugpy ms- 2026.6.0
flake8 ms- 2026.6.0
isort ms- 2026.5.11331015
mypy-type-checker ms- 2026.4.0
python ms- 2026.4.0
vscode-pylance ms- 2026.2.1
jupyter ms- 2025.9.1
jupyter-keymap ms- 1.1.2
jupyter-renderers ms- 1.3.0
vscode-jupyter-cell-tags ms- 0.1.9
vscode-jupyter-slideshow ms- 0.1.6
remote-ssh ms- 0.122.0
remote-wsl ms- 0.104.3
powershell ms- 2025.4.0
remote-explorer ms- 0.5.0
remote-server ms- 1.5.3
sqlite-viewer qwt 25.12.2
vscode-yaml red 1.23.0
jinjahtml sam 0.20.0
even-better-toml tam 0.21.2
shellcheck tim 0.39.5
simple-rst tro 1.5.4
explorer vit 1.50.4
volar Vue 3.3.0
vscode-markdown-alert yah 0.0.4

(1 theme extensions excluded)

demo720p.mp4

Activity

  1. added
    bug
    on Jun 10, 2026
  2. danieltodor commented on Jun 21, 2026

    @danieltodor

    Same (or similar) issue haunts me for months now.

    When i scroll somewhere in a markdown file and switch between the preview and edit mode with ctrl+shift+v the scroll position keeps jumping to the top of the file which is very annoying when i just wanted to check if my edit renders correctly. And sometimes when i scroll back to that position after the preview and switch between the editor/preview state again, then the scroll position keeps jumping to that frequent position instead of the top. But it's still not the scroll position where it should be (because i scrolled somewhere else before that).

  3. richardroberts-slamcore commented on Jul 16, 2026

    @richardroberts-slamcore

    Yes, I have the same issue. The scroll position in the markdown editor jumps when the focus has been in another editor pane or another tab and then the focus is returned to the markdown editor. Note that the cursor does not jump, only the scroll position. The cursor is often scrolled out of the view in fact (and moving the cursor using one of the arrow keys then of course returns the scrolling position such that the cursor is again visible). This bug occurs whether or not the markdown preview of that file is also open.

  4. gomico commented on Jul 17, 2026

    @gomico

    Same, still exists in 1.129.0

    pasting my issue content here since it's closed as duplicated:

    Version Behavior
    1.118.0 Not affected — markdown.togglePreview opens the preview in a new tab (side-by-side).
    1.119.0 / 1.119.1 Not affected — markdown.togglePreview now defaults to opening in the current tab, but the scroll position issue does not occur here. However, the preview always opens scrolled to the top, regardless of where the source editor was scrolled to.
    1.120.0 First affected — the scroll-locking behavior described in this issue is present.

    Agent analysis suggests that:

    The root cause is likely related to changes in the markdown preview's scroll state management introduced between 1.119.1 and 1.120.0.

    Specifically, during that window:

    1. preview.ts added a #scrollToFirstDiffChange mechanism — When the preview opens, it forces a scroll to the first changed line (in diff mode). This internal #line field is carried over when the preview is closed, and appears to be persisted into the editor's view state. If the preview scrolls to line N, then exits, the editor restores line N as its "saved" scroll position.

    2. The new diffScrollSync.ts module — This manages scroll synchronization between the source editor and the rendered diff preview. When the preview closes, the last synced scroll offset from this module may be written back into the editor's state, overwriting the user's actual scroll position.

    3. clearTimeout(this.#scrollingTimer) was added to dispose() — This prevents a pending scroll-sync callback from firing during teardown. However, if the last scroll event sent from the preview (before closing) already committed a stale position to the editor, clearing the timer only prevents a potential correction, not the root issue.

    The core problem: When exiting preview mode (markdown.showSource or toggling off), the editor's view state saves the scroll position that was last synced between editor and preview — not the position the user actually scrolled to in the source editor. This saved state is then restored on tab switch, causing the "jump back" behaviour.

    A quick way to verify: if the scroll position is locked to the line that was at the top of the preview at exit time (rather than the editor's own scroll position), that confirms the view state is picking up the preview's last known scroll anchor instead of the editor's.

  5. RichardGTsl commented on Jul 17, 2026

    @RichardGTsl

    I have the same problem. It affects some files and not others. The affected files have headings down to level 4 (####). The unaffected files only go to level 2. (##) Perhaps this has something to do with the bug.

  6. lmBored commented on Jul 26, 2026

    @lmBored

    I'm having the same issue in VSCode Insiders 1.128.0 and 1.131.0. It fixes itself after reloading windows, but it's a big inconvenience

    edit: Please prioritize these issues over AI support. These bugs should be prioritized for a code editor.

  7. RichardGTsl commented on Jul 31, 2026

    @RichardGTsl

    I've think I had one instance where closing the file and re-opening it resolved the issue. I've definitely had another example where re-opening files made no difference. Restarting vscode fixed it though.

  8. RichardGTsl commented on Jul 31, 2026

    @RichardGTsl

    I've just confirmed that re-opening files doesn't help but restarting VSCode fixes the problem.

  9. beifangzhishi-ops commented on Aug 6, 2026

    @beifangzhishi-ops

    Same issue here on Windows.

    I can also confirm that disabling both markdown.preview.scrollEditorWithPreview
    and markdown.preview.scrollPreviewWithEditor does not help — the editor
    still jumps back to the locked position after switching tabs/editor groups.

    A restart (or reloading the window) does fix it temporarily, but the bug
    comes back reliably after this sequence:

    1. Restart VS Code.
    2. Open a Markdown file and toggle the preview.
    3. Scroll around in the preview, then close the preview.
    4. Scroll the editor to a different position, then switch away and back.

    The editor then jumps back to the position that was at the top of the
    preview when it was closed. So the scroll position gets "re-locked" every
    time the preview is opened and closed.

    • VS Code Version: 1.132.0(改成你的版本)
    • OS: Windows 11

    One more observation that might narrow it down:

    The bug only reproduces when there is at least one other file tab open in the
    window — either in the same editor group or in a floating editor window.

    With only the affected Markdown file open (with or without its preview), the
    jump does not happen when switching back.

  10. CarterRoll commented on Aug 24, 2026

    @CarterRoll

    I noticed that the preview stays where you last reloaded the preview. If you click the three dots in the top right and click "Refresh Preview", that "saves" a new scroll location when you click back into the tab.

  11. joseph423 commented on Aug 28, 2026

    @joseph423

    Are we gonna have a solution to this?

  12. lmBored commented on Aug 28, 2026

    @lmBored

    Are we gonna have a solution to this?

    Are you asking VSCode dev to pause their AI development and actually improve the editor? No way. Check back in a year /s

  13. lmBored commented on Sep 13, 2026

    @lmBored

    Matt Bierner (@mjbvz) is this issue planned ahead to be worked on in near future? I see some vscode's fork already fixed it so it shouldn't be hard to just copy their implementation

  14. 1 remaining item

  15. evermake commented on Sep 16, 2026

    @evermake

    +1, issue persists even after deleting and re-creating the file. Very frustrating...

  16. phyloong commented on Sep 17, 2026

    @phyloong

    Version 1.138, this problem still exists.

  17. fastcat commented on Sep 18, 2026

    @fastcat

    Maybe related to #329625?

  18. vworld commented on Sep 21, 2026

    @vworld

    The issue persists, and the disorientation it causes is frustrating enough that switching to another IDE for Markdown editing seems to be an easier choice.

    A fix would be greatly appreciated. An editor unexpectedly jumping to a different scroll position when switching tabs is disruptive to the editing workflow and isn't something that can reasonably be worked around.

  19. dmoc24 commented on Sep 29, 2026

    @dmoc24

    Given how much AI driven development relies on markdown files I'm surprised this issue is not top priority. It's super disorienting to have to find the correct position in a plan.md file time and again.

  20. astroflexx commented on Oct 8, 2026

    @astroflexx

    Same issue. Please fix.

  21. AmitTzah commented on Oct 22, 2026

    @AmitTzah

    Same issue. Extremely annoying...

  22. TheLostTemple2023 commented on Oct 23, 2026

    @TheLostTemple2023

    Version 1.143.1, this problem still exists.

  23. njoAI commented on Oct 23, 2026

    @njoAI

    Workaround: Run "Developer: Reload Window". Seems to clear it for me for a while.

    I am also very surprised, that this is not taken care of in the age of AI. Currently I see such documents half of my work day...

  24. ermse commented on Oct 26, 2026

    @ermse

    Context first, because this issue and #147718 are two halves of one design problem.

    The markdown preview and the markdown text editor share a single remembered line number per resource, and it is used as a bidirectional sync anchor without any record of which side scrolled last. Whichever side you did not touch most recently can therefore overwrite the position of the side you did:

    Both are the same root design point seen from opposite ends, but they fail through different code, so the rest of this comment is specific to the editor side.


    Still present in 1.143.1 (e646868, Windows x64), and the cause here looks precise and fairly small.

    MarkdownPreviewManager re-applies a remembered preview scroll line to the text editor every time a markdown editor becomes active:

    // extensions/markdown-language-features/src/preview/previewManager.ts
    this._register(vscode.window.onDidChangeActiveTextEditor(textEditor => {
        // When at a markdown file, apply existing scroll settings
        if (textEditor?.document && isMarkdownFile(textEditor.document)) {
            const line = this.#topmostLineMonitor.getPreviousStaticEditorLineByUri(textEditor.document.uri);
            if (typeof line === 'number') {
                scrollEditorToLine(line, textEditor);
            }
        }
    }));

    That line comes from setPreviousStaticEditorLine, which StaticMarkdownPreview fires on every preview scroll. The handoff is clearly meant to be one-shot, and in the opposite direction it is:

    // extensions/markdown-language-features/src/preview/topmostLineMonitor.ts
    public getPreviousTextEditorLineByUri(resource: vscode.Uri): number | undefined {
        const scrollLoc = this.#previousTextEditorInfo.get(resource);
        this.#previousTextEditorInfo.delete(resource);   // consumed
        return scrollLoc?.line;
    }
    
    public getPreviousStaticEditorLineByUri(resource: vscode.Uri): number | undefined {
        return this.#previousStaticEditorInfo.get(resource)?.line;   // never consumed
    }

    getPreviousStaticEditorLineByUri never deletes the entry, so the preview's last scroll line is retained in #previousStaticEditorInfo for the lifetime of the extension host and re-applied on every subsequent activation of that editor. That matches the reported behaviour exactly — not a one-time jump after toggling the preview, but the editor snapping back to the same preview position again and again, overwriting the position from step 3 each time you return to the tab.

    It also explains Nils Johnsen (@njoAI)'s workaround above: Developer: Reload Window recreates TopmostLineMonitor with an empty ResourceMap, so the stale entry is gone "for a while" — until the preview is opened and scrolled again.

    The fix looks like it should mirror the text-editor side: delete the entry on read, and/or skip the scroll when the editor already has its own newer view state, rather than treating the stored preview line as a permanent anchor for the document.

  25. Dukejung commented on Oct 28, 2026

    @Dukejung

    Confirming the analysis above. On 1.143.1 (e646868, WSL remote), I applied the one-line change to the built-in extension's dist/extension.js, making getPreviousStaticEditorLineByUri consume the entry the same way getPreviousTextEditorLineByUri does:

    public getPreviousStaticEditorLineByUri(resource: vscode.Uri): number | undefined {
        const scrollLoc = this.#previousStaticEditorInfo.get(resource);
        this.#previousStaticEditorInfo.delete(resource);
        return scrollLoc?.line;
    }

    After a window reload, the editor no longer snaps back to the preview's position when I return to the tab. Switching from the preview back to the editor with Ctrl+Shift+V still lands on the preview's position once, so the intended handoff is kept.

  26. added a commit that references this issue on Nov 23, 2026
    2219f59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugmarkdown

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions