How to open a file in read-only mode with VS Code
Asked Answered
S

7

23

I want to view some code files with VS Code and edit my own code to call them. I shouldn't change them (include the file mode) so it's best if I open those files in read-only mode.

In fact, I have met some trouble because of the modification by accident. So I think I should view those files in a safer way.

How to do it with VS code? I know PyCharm and Notepad++ have implented this function, so has VS Code? I haven't find it even on its official website.

Shapiro answered 31/7, 2020 at 2:36 Comment(1)
I have the same issue. Some plugins such as vscode-java somehow achieved the capability.Staves
L
16

In the Stable Build v1.79 is the ability to detect and/or mark files as read-only thanks significantly to @jackpunt.

There are four new commands and some new settings implementing this.

These commands and settings DO NOT actually change the file permissions in the filesystem - they just prevent the file from being written/pasted to, etc. in a vscode editor.

Commands:
File: Set Active Editor Writeable in Session
workbench.action.files.setActiveEditorWriteableInSession
File: Set Active Editor Readonly in Session
workbench.action.files.setActiveEditorReadonlyInSession
File: Toggle Active Editor Readonly in Session
workbench.action.files.toggleActiveEditorReadonlyInSession
Files: Reset Active Editor Readonly in Session  // or may be named 'Clear...'
workbench.action.files.resetActiveEditorReadonlyInSession
Settings:
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.

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.

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.

setting files readonly and unsetting

readonly files include setting


Also see vnext release notes v1.79: Readonly mode

Readonly mode

In some cases it can be helpful to explicitly mark some of the workspace folders or files as readonly for example when their contents is being managed by a different process (such as the node_modules folder that is managed by node.js package manager).

For this scenario new settings have been added to mark file paths as readonly in the explorer and in text and notebook editors:

  • files.readonlyInclude: paths or glob patterns that will make a file be readonly if matching
  • files.readonlyExclude: paths or glob patterns that will skip files from being readonly when they match files.readonlyInclude
  • files.readonlyFromPermissions: whether a file that has no write-permissions on disk should be readonly

According to the rules of the settings, if a path is considered to be readonly, you cannot modify it from the explorer (e.g. delete it) and you will see a readonly text or notebook editor.

For more ad-hoc toggling of the readonly mode, new commands have been added that allow to change the mode for the current session only, overruling what the settings configure::

  • Set Active Editor Readonly in Session: mark active editor readonly
  • Set Active Editor Writeable in Session: mark active editor writeable
  • Toggle Active Editor Readonly in Session: toggle between readonly and writeable
  • Clear Active Editor Readonly in Session: reset the session state
Leaseback answered 10/5, 2023 at 21:28 Comment(0)
H
4

According to Visual Studio Code Feature Request #99322, https://github.com/microsoft/vscode/issues/99322, this feature will not be implemented in VSCode. However the extension "Read-Only Mode Support" Attempts to implement some read-only functionality, but is not ideal. It appears to require modification of config files to automatically ignore edits, but with files outside of the root directory, it offers a popup to ignore changes when you save.

Hydrotropism answered 31/7, 2020 at 3:49 Comment(2)
It looks like outdated answer. Since May 2023, it is possible: code.visualstudio.com/updates/v1_79#_readonly-modeSymbolize
This answer is outdated. See Fedor comment above or Mark answer below.Akene
S
2

Another good option is The Read-Only Indicator extension (marketplace, github).

Stephanus answered 3/2, 2021 at 11:16 Comment(0)
N
2

You can toggle read only mode in vs code using keyboard shortcut. Follow these steps:

  1. Go to Settings > Keyboard Shortcuts.
  2. Search: Toggle Active Editor Read-only in Session
  3. Set keyboard shortcut to the key which you want to toggle read only. I prefer "ctrl+shift+R" in windows or "cmd+shift+R" in mac.
  4. Now right click on the key binding you just set and select "Change when expression" and enter "editorTextFocus" or "editorFocus" and press enter. Change when expression

Your keyboard shortcut should look like this: Your keyboard shortcut should look like this

Now you can press that keyboard shortcut and toggle between (read-only = True) and (read-only = False).

Press your shortcut key to enable read only mode: Press your shortcut key to enable read only mode

Note: Your cursor should be on the file which you want to set to read only mode before you press the key.

Nonu answered 12/2 at 20:20 Comment(0)
M
1

You can make the file read only in the file system. But this is not what the OP asked for.

EDIT: So better to use the new read only feature https://code.visualstudio.com/updates/v1_79#_readonly-mode

To make a file read only:

Linux (except WSL):

chmod -w filename

WSL:

Windows:

  • From File Explorer, right click file, select Properties, Security Tab, then check 'Write Deny'.

Then each time you modify the file VSCODE warns you "Failed to save 'filename': File is read-only. Select 'Overwrite' to attempt to make it writeable." There are options to Overwrite, Save As... or Discard.

Select Discard to undo your inadvertent changes.

EDIT This is a workaround, not an answer to the question as @AnrDaemon commented

Maurili answered 12/4, 2021 at 1:38 Comment(5)
Probably the smartest way. In Windows: Open file in explorer, hit >Properties > Read only. Done.Ichthyosis
FYI, if you're using WSL, the chmod cmd doesn't work: afaict, you cannot change file permissions on files in the Windows system using chmod under WSL. You have to use the File Explorer method. However, there is no "read only" permission per se in Windows; you instead have to choose "Deny write".Cymophane
Thanks for the comment - I have updated the post to reflect this.Maurili
The question is not about making files read-only, but to prevent files from being modified by IDE. F.e. when you open a file from another project for reference.Detinue
this feature is now supplied directly in VScode.Summary
S
1

I expect that what you need/want is: https://github.com/microsoft/vscode/issues/161715 which allows to mark files/editors as read-only when you open them, independent of the filesystem/chmod attributes. (and shows the 'lock' icon when you open a read-only file)

**

Now part of standard VSCode (mid 2023)

Summary answered 2/10, 2022 at 18:34 Comment(0)
X
1

From the extensions section in vs code, search "Reader Mode", developed by "theoolee".

After installation, open your file in vs code, then press 'ctrl + shift + P' to enter command palette, then type "Reader Mode: Toggle Reader mode for current file" and click on it to enable read only mode.

Redo to disable the feature or just reload your file.

Xerosis answered 19/3, 2023 at 23:43 Comment(2)
Nice plugin. Sadly its reader-mode blocks the correct displaying of Python notebooks.Tamandua
this feature is now supplied directly in VScode.Summary

© 2022 - 2024 — McMap. All rights reserved.