1. Overview
Vim (Vi IMproved) is one of the oldest yet most popular text editors in the Linux community. It’s a revised clone of the original vi editor. The prime reason for its wide adoption is its efficiency and wide range of features.
One of the less-known but most useful features of the Vim editor is it allows us to open a file in a read-only mode. This feature comes in very handy while working with important files. The read-only mode provides an additional level of data safety by preventing overwriting of data inadvertently.
In this short tutorial, we’ll discuss how to open a file in a read-only mode. Additionally, we’ll also discuss how to exit from a read-only mode with and without saving the file.
2. Opening a File in a Read-Only Mode
Just like any other editor, by default, the Vim editor opens a file in a read-and-write mode. The good thing is it provides various options to open a file in a read-only mode.
To understand this, first, let’s create a simple text file to use as an example:
$ echo -e "Line 1\nLine 2\nLine 3" > file.txt
In this example, we’ve used the combination of the echo command and Shell’s redirection operator to create a text file.
Now, the sample file is ready. So let’s discuss the various methods that allow us to open a file in a read-only mode.
2.1. Using the view Command
Many Linux users prefer to use the view command to open a file in a read-only mode. The usage of the view command is identical to the vim command.
So let’s open the file.txt file using the view command:
$ view file.txt
Here, we can see that the view command opens a file in a regular Vim window. However, the main difference is that at the bottom left corner, it shows an informative message about the file’s read-only mode. In this case, it shows the message – “file.txt” [readonly]
The read-only mode is self-explanatory. In this mode, we can modify the file in the editor window. However, it prevents the accidental overwriting of the file by disabling writes.
2.2. Using the -R Option of the Vim Editor
Similarly, we can use the -R option with the Vim editor to open a file in read-only mode:
$ vim -R file.txt
This option provides the same functionality as the view command. So one can use this option as an alternative to the view command.
2.3. Using the -m Option of the Vim Editor
Apart from this, we can also use the -m option of the Vim editor to open a file in the read-only mode:
$ vim -m file.txt
Here, the -m option instructs Vim to disable the modifications in the file. With this option, we can edit the file in the editor window. However, writing modifications to the file isn’t allowed.
If we try to save the file, then the operation fails with the error – E142: File not written: Writing is disabled by the ‘write’ option.
2.4. Using the -M Option of the Vim Editor
Finally, we can also use the -M option to open the file in read-only mode. This option is the safest among all options because it doesn’t allow to modify the file in the editor window itself.
$ vim -M file.txt
If we try to modify the file, then the operation fails with the error – E21: Cannot make changes, ‘modifiable’ is off.
3. Exit From Vim Editor Without Saving the File
In the previous section, we discussed the various methods to open a file in read-only mode. Now, let’s discuss how to exit from the Vim editor without writing modifications to the file.
3.1. Using the quit Command With Force Option
In a normal scenario, we can use Vim’s quit command to exit from the editor. However, Vim doesn’t allow us to quit the editor if the file has been modified and changes aren’t written to disk.
In such cases, we’ve to use the exclamation mark(!) with the quit command to force the operation:
:quit!
OR
:q!
In this example, the q command represents a shortcut to the quit command.
3.2. Using the quit Command Without Checking for Changes
In addition to this, we can also use the ZQ command to quit the Vim editor. This option quits the editor without checking for changes. The behavior of this command is exactly similar to the q! command that we discussed in the previous section.
4. Exit From Vim Editor After Saving the File
In the previous section, we discussed two methods to exit from the Vim editor without saving the file. Now, let’s discuss a few methods to exit the Vim editor after saving the file.
4.1. Using the write and quit Commands With Force Option
By default, the Vim editor doesn’t allow us to save modifications if the file is opened in a read-only mode using the view command or the -R option. The operation fails with an error – readonly option is set (add ! to override).
The warning message is self-explanatory, and we can achieve the desired result using the two Vim commands:
:write!
:quit
Here, we’ve used a combination of the write and quit commands. The exclamation(!) mark in the first command is used to perform the write operation forcefully.
In addition to this, we can combine these two commands into a single command to achieve the same result:
:wq!
In this example, the w and q represent the shortcut of the write and quit commands.
4.2. Using the exit Command With Force Option
Similarly, we can use the Vim editor’s exit command with the force option. This allows us to save the file and quit the editor:
:exit!
OR
:xit!
OR
:x!
In this case, we can use the :xit or ‘*:x’* as a shortcut for the exit command.
4.3. By Enabling the write Option
Previously discussed methods don’t work if the file is opened in a read-only mode using the -m option. Because this option disables the write option and doesn’t allow overwriting, even using the exclamation mark.
In such cases, first, we’ve to enable the write option using the set command:
:set write
After this, we can use the write and quit commands without the force option.
4.4. By Enabling the modifiable and write Options
Similarly, just setting the write option is insufficient if the file is opened using the -M option, as this option turns off the modifiable flag.
In such scenarios, we need to enable the modifiable and write options, respectively:
:set modifiable
:set write
It’s important to note that these commands must be executed in the specified order only. After this point, we can use any variant of the write and quit commands.
5. Conclusion
In this article, we discussed different methods that show how to exit from the read-only mode of the Vim editor.
First, we saw various methods that allow us to open a file in a read-only mode. Then, we discussed the usage of the quit command to exit from the Vim editor without saving the file.
Finally, we also discussed some additional methods that allow us to exit from the Vim editor after saving the file.