Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 751
Kani Avatar asked Jul 31 '20 02:07

Kani


2 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.

like image 62
NikolaPi Avatar answered Sep 22 '22 11:09

NikolaPi


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.

like image 23
intotecho Avatar answered Sep 21 '22 11:09

intotecho