How to open a file in vim in read-only mode on Linux/Unix

You can open a file in read-only mode in Vim using the -R or --readonly option, either when starting Vim or from within the editor.

Here are two ways to do this:

  1. When starting Vim:
vim -R <file>
  1. From within the editor:

Start Vim as you normally would, then enter the following command:

:set ro

This will change the file to read-only mode, and you won’t be able to make any changes to the file. To exit the read-only mode, enter the following command:

:set noro

Leave a Comment