Linux

How to Save Read-Only Files in VIM Editor in Linux/Unix-like OS

Slide1

Having talked about How to create, edit, save a file and quit Vim Editor in Linux, and related topics such as A brief Introduction to Linux and how to create disk partition in Ubuntu Linux, and Locate, Find, and Grep: How to search for files and patterns in Linux/Unix-like OS, it’s also very important to show you one simple but can sometimes slow down your job when trying to quickly edit read-only files in vim editor. Before I move on I will like to refer you to my previous write-ups on relevant topics such as Provisioning Azure Resources using Azure Az PowerShell Cmdlet from Cloud Shell, how to secure access to your Virtual Machine with Just-in-Time (JIT) VM Access, and how to build your first CI/CD Pipeline in Azure DevOps using ASP.Net Core Application.

Vim editor or VI- iMproved is a text editor that does not give you the normal Graphical User Interface (GUI). Real Linux users don’t make use of GUIs. No matter how popular, slick, and sophisticated the interfaces become for Linux and UNIX, you’ll always need to be able to navigate in a text editor, and one of such wonderful editor is Vim formerly Vi editor. Kindly refer to these related guides: how to configure email notifications for Azure CI/CD build Pipeline on Azure DevOps Project, how to create an App Service Plan with Continuous Deployment to deploy a .NET Application from GitHub, and Azure DevOps and GitHub integration for Docker and Kubernetes deployment.

The VIM editor is the modification of the original standard UNIX full screen editor known as VI editor. It's been around almost since UNIX began and it has changed very little.  The name was changed from Vi editor to Vim editor in order to  get around the limitations of Vi editor.  It was created by  Bram Moolenaar. The name stands for VI iMproved, contains numerous  features when compared to  the old VI editor and these features include: help, multiple windows, syntax highlighting, programmer support, and HTML support. 

As a Linux System Administrator, we have all been a victim of having to get on a file, opened it using vi or vim editor to inspect the contents, and realize we need to alter and make a bunch of changes, and discover that you don’t have the right privileges to write to the file. This has happened to me in particular before I learnt my lessons. 😎😎. We usually face this kind of problem, when we open a file and forget to use the sudo (Super User Do) command.

I believe we are familiar with the Linux Terminal, if not refer to the links to my previous post stated above. Now, let’s say we want to create a new directory called " readonly" with the command mkdir readonly and use the cd command to navigate to the newly created directory that is cd readonly. We will use the echo command to create a txt file with content such as "This is a read-only file" using the command below:

~/readonly$ echo "This is a read-only file" > readfile.txt

Let’s use the cat command to view the content of the readfile.txt i.e. cat readfile.txt . See the screenshot below for clarity.

Readonly-files
Creating Directory and File in Linux Terminal

In addition, let’s use the ls -l command to check the ownership rights to the file we just created.

Right-permission
Write Permission

As you can see from the screenshot above the permission says the owner can write, the group can write and others can write as well. Since the goal is to make the readfile.txt a read only file, we will proceed to changing the write permission mode and assign the read-only permission using the command:chmod 444 readfile.txt. If we use the ls -l command to view the file permission, we will see that the owner can read-only, the group can read-only and others can read-only (444 = r-- r-- r--: owner/group/others are all only able to read the file.). Refer to the screenshot below;

read-only-right
Setting Read Only Permission

If we enter the readonly folder using cd readonly command we will see a file that has read-only privileges. What if we try to add addition text contents to the file using the vim editor? This takes us to the main point of the day of how to save read-only files in vim editor.

Open the file with the vim readfile.txt command and type any text you want to add and try saving and exiting the vim editor with the normal :wq! command .

File-written-to-the-file
Written file to the Read only file

From the screenshot above, the file we added actually write to the existing file contents. But why did it happen so? The common answer to this is because the folder privileges override the files privileges. For instance, if we use ls -l to view the folder (readonly), we will realize that the owner which is me in this case still have the write privileges. And because I can write to the folder I have the right privileges to write to the files inside it.

Folder-permission
Owner’s Write-Read Permission
Note! For the fact that I can write to the file using vim editor, doesn't mean that every command can work! For instance, if I make an attempt to write to the file within the terminal using the echo command,  I will get permission denied message prompt. Let's try it by writing echo "editing secure files" >> readfile.txt and hit the enter key.
Permission-Denied
Permission denied

Let’s assume that I send the file readfile.txt to a guest user who doesn’t have the write permission to the folder on my system and the guest user make an attempt to write to the file, he will get an error as shown below when editing and saving it with vim editor using the normal :wq! command: Here the first part :w means write a file while the q! means quite vim editor.

 E212: Can't open file for writing
Press ENTER or type command to continue

When you’re faced with this kind of issue, what do you do? Usually, you exit vim:

:q!

And maybe open the file again again with sudo: and make all the changes again with so much time wasted. If you’re an intermediate vim user, then you save the file to /tmp directory:

:w /tmp/foo

And then you sudo move the /tmp/foo to the right location:

$ sudo mv /tmp/foo /etc/techdirectarchive/httpd.conf

That works but below is another method that you can use without quitting vim and that is:

:w !sudo tee % >/dev/null

What the command does:

  • :w = Write a file.
  • !sudo = Call shell sudo command.
  • tee = The output of the vim write command is redirected using tee.
  • % = Triggers the use of the current filename meaning the filename of currently open file.

Simply put, the ‘tee’ command is run as sudo and follows the vim command on the current filename given.

What simply takes place when using the :w !sudo tee % command is vim spawns sudo tee filename and pipes the contents of the file to its stdin. The tee command now runs in a privileged environment and redirects its stdin to filename. The >/dev/null part discards tee‘s stdout as you don’t need to see it in vim.

If anyone asked me to write this command in a haste, I might not be able to because it’s too long and complicated to remember. Instead, I will create a vim alias for this command and point it to the  ~/.vimrc:

cnoremap sudow w !sudo tee % >/dev/null
  • cnoremap: tells vim that the following shortcut is to be associated in the command line
  • w !sudo tee % >/dev/null: added a redirection of messages to NULL to make a clean command

Now the next time you’re in this situation, just type:

:sudow

That’s all you need. See you next time!👏

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Kindly subscribe to TechDirectArchive
This is default text for notification bar