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

Viewed 11359

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.

Many thanks.

4 Answers

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.

You can make the file read only in the file system.

Linux:

chmod -w filename

For Windows, from File Explorer, right click file, select Properties, then Read Only.

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.

You could install the Local History extension, and see if you made changes and revert them.

Related