You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Markdown preview resets when switching tabs #147718
bugIssue identified by VS Code Team member as probable bughelp wantedIssues identified as good community contribution opportunitiesmarkdown-extThe Markdown extension: language features and preview
I am also running into this while using Markdown Preview as a reading mode for long documents. After switching to another file/tab and then coming back, losing the previous scroll position means I have to manually find where I left off every time. Preserving the preview scroll position per document would make reading and cross-referencing Markdown files much smoother.
A more specific reproduction case that is different from the existing recording:
Open a long Markdown file in source/edit mode and leave the editor position around a specific line, for example line 100.
Open the same file in Markdown Preview / read mode.
Scroll the preview further down, to content corresponding to a much later part of the document.
Switch to another Markdown tab/file.
Switch back to the first Markdown Preview tab.
Expected: Markdown Preview preserves the preview's own scroll position from step 3.
Actual: Markdown Preview jumps back to the source editor's anchored position from step 1, for example the area corresponding to line 100, instead of the later preview position where I was reading.
This is disruptive when using Markdown Preview as a reading mode for long documents, because switching tabs makes the reader lose the actual preview reading position.
PR #334681 addresses the stale-content reset in the original report and recording. In the recording, the preview displays the edited paragraph, then falls back to the earlier "b" after another tab hides it.
Visible updates are applied with updateContent messages, but leave webview.html unchanged. Hiding the preview discards the webview; revealing it restores that old HTML. The PR tracks when the stored HTML is behind and forces a full refresh when the panel is hidden. It covers both the dynamic preview and vscode.markdown.preview.editor, including a panel hidden during rendering and a forced refresh requested while a regular refresh is pending.
The same content-reset sequence is reported in #271233 and in the tab-switch comments on #265277. The PR description contains before/after measurements and regression coverage.
The newer comments here describe preserving the preview's independent scroll position. This PR does not claim to resolve that separate behavior.
Context first, because this issue and #317033 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:
This issue is the preview side: the preview loses its own scroll position and jumps to the line the editor is anchored at — usually line 0, i.e. the top of the file.
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 preview side. It is also distinct from the stale-content reset that #334681 fixes; that PR explicitly does not cover the scroll position.
Still present in 1.143.1 (e646868, Windows x64). The code-level cause:
The preview panel is created without retainContextWhenHidden, so hiding the tab destroys the webview and revealing it reloads the HTML. On reload, preview-src/index.ts restores the reading position from the persisted scrollProgress — but throws it away whenever the host-side starting line changed since the last save:
settings.line is MarkdownPreview.#line on the host side. That field gets overwritten with the source editor's topmost visible line by two paths in src/preview/preview.ts, neither of which is gated on the user having actually scrolled the editor:
TopmostLineMonitor.onDidChanged (fired from onDidChangeTextEditorVisibleRanges) → scrollTo(line) → #line = topLine
So the sequence that loses the position is:
Scroll the preview down.
The source .md editor is open in a background tab, so it is not in visibleTextEditors and scrollEditorWithPreview cannot move it — it stays at line 0.
Activate that editor tab. onDidChangeActiveTextEditor fires, #line is reset to the editor's topmost visible line.
Return to the preview. The webview reloads, sees originalState.line !== settings.settings.line, discards scrollProgress, and scrolls to settings.line — the top of the file.
This explains why the repro is flaky, and why it does not happen when switching between the preview and a non-markdown tab: neither path fires, #line is unchanged, and scrollProgress restores correctly. That is very likely why #318532 could not be reproduced on request and was closed. It also matches tanglinfang's variant above, where the preview jumps back to the source editor's anchored line rather than to the very top — same mechanism, different editor line.
Worth noting for anyone looking for a workaround: "markdown.preview.scrollPreviewWithEditor": false does not help. It is only checked on the webview side, so the host still clobbers #line, scrollProgress is still discarded on reload, and the if (settings.settings.scrollPreviewWithEditor) branch that would scroll to settings.line is then skipped — leaving the preview at the top regardless. What does help is keeping the source editor closed, or using a locked preview, since the !this.#locked guard suppresses the onDidChangeActiveTextEditor path.
Two defects look separable here:
MarkdownPreview.scrollTo mutates #line — the anchor used to restore the webview after a reload — even when the webview will ignore the resulting updateView message because scrollPreviewWithEditor is disabled.
A preview the user has scrolled independently is re-anchored to the editor merely because that editor became active, without any editor scroll having occurred. onDidChangeActiveTextEditor arguably should not re-anchor a preview whose own scroll position is newer.
bugIssue identified by VS Code Team member as probable bughelp wantedIssues identified as good community contribution opportunitiesmarkdown-extThe Markdown extension: language features and preview
Activity
fix: defer markdown preview refresh when panel is hidden
tanglinfang commentedon Jul 5, 2026
I am also running into this while using Markdown Preview as a reading mode for long documents. After switching to another file/tab and then coming back, losing the previous scroll position means I have to manually find where I left off every time. Preserving the preview scroll position per document would make reading and cross-referencing Markdown files much smoother.
tanglinfang commentedon Jul 5, 2026
A more specific reproduction case that is different from the existing recording:
Expected: Markdown Preview preserves the preview's own scroll position from step 3.
Actual: Markdown Preview jumps back to the source editor's anchored position from step 1, for example the area corresponding to line 100, instead of the later preview position where I was reading.
This is disruptive when using Markdown Preview as a reading mode for long documents, because switching tabs makes the reader lose the actual preview reading position.
vscode_markdown_preview_editor_anchor_vscode_display.mov
bompus commentedon Oct 5, 2026
PR #334681 addresses the stale-content reset in the original report and recording. In the recording, the preview displays the edited paragraph, then falls back to the earlier "b" after another tab hides it.
Visible updates are applied with
updateContentmessages, but leavewebview.htmlunchanged. Hiding the preview discards the webview; revealing it restores that old HTML. The PR tracks when the stored HTML is behind and forces a full refresh when the panel is hidden. It covers both the dynamic preview andvscode.markdown.preview.editor, including a panel hidden during rendering and a forced refresh requested while a regular refresh is pending.The same content-reset sequence is reported in #271233 and in the tab-switch comments on #265277. The PR description contains before/after measurements and regression coverage.
The newer comments here describe preserving the preview's independent scroll position. This PR does not claim to resolve that separate behavior.
ermse commentedon Oct 26, 2026
Context first, because this issue and #317033 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 preview side. It is also distinct from the stale-content reset that #334681 fixes; that PR explicitly does not cover the scroll position.
Still present in 1.143.1 (e646868, Windows x64). The code-level cause:
The preview panel is created without
retainContextWhenHidden, so hiding the tab destroys the webview and revealing it reloads the HTML. On reload,preview-src/index.tsrestores the reading position from the persistedscrollProgress— but throws it away whenever the host-side starting line changed since the last save:settings.lineisMarkdownPreview.#lineon the host side. That field gets overwritten with the source editor's topmost visible line by two paths insrc/preview/preview.ts, neither of which is gated on the user having actually scrolled the editor:DynamicMarkdownPreview'svscode.window.onDidChangeActiveTextEditorhandler →this.update(editor.document.uri, new StartingScrollLine(getVisibleLine(editor)))→MarkdownPreview.scrollTo(line)→#line = topLineTopmostLineMonitor.onDidChanged(fired fromonDidChangeTextEditorVisibleRanges) →scrollTo(line)→#line = topLineSo the sequence that loses the position is:
.mdeditor is open in a background tab, so it is not invisibleTextEditorsandscrollEditorWithPreviewcannot move it — it stays at line 0.onDidChangeActiveTextEditorfires,#lineis reset to the editor's topmost visible line.originalState.line !== settings.settings.line, discardsscrollProgress, and scrolls tosettings.line— the top of the file.This explains why the repro is flaky, and why it does not happen when switching between the preview and a non-markdown tab: neither path fires,
#lineis unchanged, andscrollProgressrestores correctly. That is very likely why #318532 could not be reproduced on request and was closed. It also matches tanglinfang's variant above, where the preview jumps back to the source editor's anchored line rather than to the very top — same mechanism, different editor line.Worth noting for anyone looking for a workaround:
"markdown.preview.scrollPreviewWithEditor": falsedoes not help. It is only checked on the webview side, so the host still clobbers#line,scrollProgressis still discarded on reload, and theif (settings.settings.scrollPreviewWithEditor)branch that would scroll tosettings.lineis then skipped — leaving the preview at the top regardless. What does help is keeping the source editor closed, or using a locked preview, since the!this.#lockedguard suppresses theonDidChangeActiveTextEditorpath.Two defects look separable here:
MarkdownPreview.scrollTomutates#line— the anchor used to restore the webview after a reload — even when the webview will ignore the resultingupdateViewmessage becausescrollPreviewWithEditoris disabled.onDidChangeActiveTextEditorarguably should not re-anchor a preview whose own scroll position is newer.Refresh markdown preview html before a hidden panel is rebuilt (#334681)