如何在VS Code中通过tasks.json在侧边栏显示Markdown预览(不打开文件)
Preview test.md in VS Code Without Opening It (Using tasks.json)
The Command You Need
VS Code's built-in command to trigger Markdown preview is markdown.showPreviewToSide (use markdown.showPreview instead if you want a full-tab preview). To target a specific file without opening it, you'll pass the file path as an argument via the VS Code CLI.
Step-by-Step Setup
1. Open tasks.json
Press Ctrl+Shift+B (macOS: Cmd+Shift+B), select "Create tasks.json file from template", then choose "Others" to get a blank template.
2. Add the Preview Task
Replace the default content with this configuration:
{ "version": "2.0.0", "tasks": [ { "label": "Preview test.md", "type": "shell", "command": "code", "args": [ "--command", "markdown.showPreviewToSide", "${workspaceFolder}/test.md" ], "problemMatcher": [] } ] }
3. Run the Task
- Press
Ctrl+Shift+Bagain, select the "Preview test.md" task from the list. - The Markdown preview will open in the side panel, and
test.mdwon't be loaded into an editor tab.
Key Details
- Ensure the
codecommand is in your system PATH: install it via VS Code's command palette (Ctrl+Shift+P) by searching for "Shell Command: Install 'code' command in PATH". - Adjust the file path in
argsiftest.mdis in a subdirectory (e.g.,${workspaceFolder}/docs/test.md). - Swap
markdown.showPreviewToSidewithmarkdown.showPreviewto open the preview in a standalone tab instead of the side panel.
内容的提问来源于stack exchange,提问作者Abhishek Chavhan
相关产品推荐
相关产品推荐

