如何使用 VS Code 以只读模式打开文件

Kan*_*ani 6 file visual-studio-code vscode-settings

我想用VS Code查看一些代码文件并编辑我自己的代码来调用它们。我不应该更改它们(包括文件模式),因此最好以只读模式打开这些文件。

其实我也因为不小心修改,遇到了一些麻烦。所以我认为我应该以更安全的方式查看这些文件。

如何用 VS 代码做到这一点?我知道 PyCharm 和 Notepad++ 已经实现了这个功能,那么 VS Code 也有吗?我什至在它的官方网站上也没有找到它。

非常感谢。

Mar*_*ark 11

在稳定版本 v1.79 中,能够检测和/或将文件标记为只读,这在很大程度上要感谢 @jackpunt。

有四个新命令和一些新设置来实现这一点。

这些命令和设置实际上不会更改文件系统中的文件权限 - 它们只是阻止文件在 vscode 编辑器中写入/粘贴等。

命令:
File: Set Active Editor Writeable in Session
workbench.action.files.setActiveEditorWriteableInSession
Run Code Online (Sandbox Code Playgroud)
File: Set Active Editor Readonly in Session
workbench.action.files.setActiveEditorReadonlyInSession
Run Code Online (Sandbox Code Playgroud)
File: Toggle Active Editor Readonly in Session
workbench.action.files.toggleActiveEditorReadonlyInSession
Run Code Online (Sandbox Code Playgroud)
Files: Reset Active Editor Readonly in Session  // or may be named 'Clear...'
workbench.action.files.resetActiveEditorReadonlyInSession
Run Code Online (Sandbox Code Playgroud)
设置:
Files: Readonly From Permissions         // in the Settings UI, default is disabled   

"files.readonlyFromPermissions": true,   // in settings.json, default is disabled

> Marks files as readonly when their file permissions indicate as such. 
> This can be overridden via Files: Readonly Include and Files: Readonly
> Exclude settings.

Run Code Online (Sandbox Code Playgroud)
Files: Readonly Include             // in the Settings UI

    "files.readonlyInclude": {      // in your user/workspace settings.json
        "dist/*.min.js": true
    }

> Configure path or glob patterns to mark as read-only.   You can
> exclude matching paths via the Files: Readonly Exclude setting.  Files
> from readonly system providers will always be read-only independent of
> this setting.

Run Code Online (Sandbox Code Playgroud)
Files: Readonly Exclude

    "files.readonlyExclude": {       // in your user/workspace settings.json
        "dist/dontExclude.min.js": true
    }

> Configure path or glob patterns to exclude from being marked as
> read-only if they match as a result of the Files: Readonly Include
> setting. Files from readonly system providers will always be read-only
> independent of this setting.

Run Code Online (Sandbox Code Playgroud)

将文件设置为只读和取消设置

只读文件包括设置


另请参阅vnext 发行说明 v1.79:只读模式

只读模式

在某些情况下,将某些工作区文件夹或文件显式标记为只读会很有帮助,例如当它们的内容由不同的进程管理时(例如由node_modules node.js 包管理器管理的文件夹)。

对于这种情况,已添加新设置以在资源管理器以及文本和笔记本编辑器中将文件路径标记为只读:

  • files.readonlyInclude:如果匹配,将使文件只读的路径或全局模式
  • files.readonlyExclude:匹配时将跳过文件只读的路径或全局模式files.readonlyInclude
  • files.readonlyFromPermissions:磁盘上没有写权限的文件是否应该是只读的

根据设置规则,如果路径被视为只读,则您无法从资源管理器中修改它(例如删除它),并且您将看到只读文本或笔记本编辑器。

对于只读模式的更多临时切换,添加了新命令,允许仅更改当前会话的模式,从而推翻设置配置的内容:

  • Set Active Editor Readonly in Session:将活动编辑器标记为只读
  • Set Active Editor Writeable in Session:将活动编辑器标记为可写
  • Toggle Active Editor Readonly in Session:在只读和可写之间切换
  • Clear Active Editor Readonly in Session:重置会话状态


Nik*_*aPi 5

根据 Visual Studio Code 功能请求 #99322,https://github.com/microsoft/vscode/issues/99322,此功能将不会在 VSCode 中实现。然而扩展“只读模式支持”试图实现一些只读功能,但并不理想。它似乎需要修改配置文件以自动忽略编辑,但是对于根目录之外的文件,它会在您保存时提供一个弹出窗口以忽略更改。

  • 这看起来像是过时的答案。自 2023 年 5 月起,可以:https://code.visualstudio.com/updates/v1_79#_readonly-mode (4认同)

efb*_*own 2

另一个不错的选择是只读指标扩展(marketplacegithub)。