You can start vim or vi text editor in read-only mode. You will be protected from writing the files. There are various ways way to achieve write protection mode in vim.
How to open a file in readonly mode:
- Use view command within vim. The syntax is: view {file-name}
- Use vim/vi command line option. The syntax is: vim -R {file-name}
- Modifications not allowed using command line option: The syntax is: vim -M {file-name}
Understanding options
The easiest syntax is:
$ view my-important.file
If you try to write, you will get an error as follows:
E45: 'readonly' option is set (add ! to override)
So just add ! to force overwrite it:
:w!
OR
:x!
The -R option forces read-only mode. The ‘readonly’ option will be set. You can still edit the buffer, but will be prevented from accidentally overwriting a file. If you do want to overwrite a file, add an exclamation mark to the Ex command, as in “:w!” or “:x!“.
# vim -R /etc/httpd/httpd.conf
If you made some modification and want to save it forcefully, type:
:w!
The -m option forces modifying files is disabled mode. Resets the ‘write’ option. You can still modify the buffer, but writing a file is not possible at all:
$ vim -m mycode.py
The -M option forces the ‘modifiable’ and ‘write’ options to be unset, so that changes are not allowed and files can not be written.
$ vim -M my-service.sh
If you try to insert or modify text, you will get an error on screen, “E21: Cannot make changes, ‘modifiable’ is off“:
Fig.01: Open a file in a tab in vim in readonly mode using -M option
A note about opening a tab in readonly mode in vim/vi
The syntax is as follows to open a new tab:
:tab sview file
OR open in a same pane:
:view file
For example, edit /etc/nginx/nginx.conf in read-only mode:
$ sudo vim -M /etc/nginx/nginx.conf
Now open a file named /etc/nginx/conf.d/cyberciti.biz.conf or /etc/nginx/conf.d/default.conf in read only mode in a new tab, use:
:tab sview /etc/nginx/conf.d/default.conf
To go to next tab type:
:tabn
To go to previous tab type:
:tabp
To list all tabs, enter:
:tabs
Sample session:
Animated gif.01: Open a file and a tab in vim in readonly mode demo
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
- RSS feed or Weekly email newsletter
- Share on Twitter • Facebook • 1 comment... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
For example, edit /etc/nginx/nginx.conf in read-only mode:
$ sudo vim +M /etc/nginx/nginx.conf
Why is there a plus there? A typo or on purpose?