You must have Linux distribution installed and configured on your machine. In our case, we have Ubuntu 20.04 Linux distribution. On the other hand, you must have sudo rights to use the Linux system and Vim utility.
At the very start, you have to make sure that you have a Vim utility installed on your Linux distribution. For this, we will check it first in our system. Therefore, you have to open the command-line terminal from the Activity bar of your Linux desktop. Afterward, execute the below Vim command to get information regarding Vim. As shown in the image, there is no Vim utility installed on our Ubuntu 20.04. It also suggests some instruction commands to install Vim on the system. Use one of them to install Vim.
Now, it’s our turn to install Vim on our Linux system. So, we have to use the sudo apt command to do so. Try the below command to install it on your system. It will need your root account passcode to initialize the process of installation. Enter your sudo password and hit the “Enter” button to continue. You will see that it will start installing and downloading Vim utility along with other bundles.
During the installation process, it will pause, and a question will pop out. The system will intimate a question to affirm your installation action, which says: “Do you want to continue? [Y/n] “. If you want to pursue the Vim installation process then you have to tap “y” and hit Enter, otherwise press “n” and tap the “Enter” button. So, we tapped “Y” and pressed the Enter key to proceed with the installation of the Vim editor in Ubuntu 20.04.
It will start the proper installation of the Vim editor. You have to wait for some time until it completes its process.
After the installation of Vim, you can check its information by using the same command as below.
If your Linux system successfully opens the below window as shown in the snapshot attached, then you are good to use the Vim editor.
Get an Error:
Now, we will create a file using the Vim command in the command shell. We will create this file in any of the directories. So, try the below Vim command to make a file “smtpd.conf” in the directory path given below.
$ vim /usr/lib64/sas12/smtpd.conf
The below-shown window will be opened, having the path and file name listed at the below side of the lower header. It’s time to write the contents of this file in the given path.
To write the contents of a file and quit the Vim editor, try the below “wq” command along with the colon “:” sign, then press the Enter button to run this command.
Right after you tap the “Enter” button, you will get an error: “E212: Can’t open file for writing” as shown in the below output image. Press the “Enter” button to continue.
To check the reason for this error, we have to write another instruction in the Vim editor. Try the below sudo command in the Vim editor, starting with the keyword “:w”, ending with the keyword “tee” and percentage sign. Hit the “Enter” key to see the cause of an error.
It requires your sudo account password to write the file contents and save the file in the mentioned path. Enter your root passcode and hit the “Enter” button. The output shows that there is no such file or directory available in your Linux system. This means that we provide the reason for this error. Press again the “Enter” key to continue with Vim.
Write the below quit command and hit the key “Enter” to come back in the terminal shell.
We learned about the reason for the error, which is because the path provided was not actually created. You can also check the path in the command line shell using the “ls” command. The output shows that such a directory is not created.
$ ls /usr/lib64/sas12
Firstly, we have to create a path or a directory in the command-line shell. To create a directory mentioned in the path provided above, we have to use the “mkdir” command along with the “-p” flag. If you use the “mkdir” without the keyword “sudo” it may through an exception “Permission denied”.
Let’s try this command with the “sudo” keyword and you will see that it works fine.
Again write the Vim command followed by the path of a file to check the changes. It will open the Vim editor.
When you write the below sudo command, it may require your sudo account password to continue. After writing the passcode and hitting the “Enter” button, you can see it will generate a warning. You have to press the “L” key followed by the “Enter” button to load this file in a provided folder.
It will load the file and give it read-only rights. Hit the Enter key.
Now, when you enter the “wq” command, it will work fine.
We have done the solution of the error: “Vim can’t open file for writing”, in a blink of an eye. I hope you can solve this error conveniently.