Edit and save read-only files with vim

When I tried to edit the readonly file on EC2, it got stuck for a moment, so make a note. There are two ways to do it.

Method 1: Add the sudo command when opening the file

The simplest and easiest way.

Terminal


$sudo vim Select the file you want to edit

After editing with vim, you can save it with: wq as usual.

Method 2: Forcibly save the edited file with readonly

I think this is the most common pattern. I'm writing this because I became. No, I haven't run it with sudo after editing! Even if it becomes, just hit the following command without being impatient.

vim


:w !sudo tee % 
#The contents are output

The meaning of each ** w **: Save **! **: Instruct subsequent commands ** sudo **: Run with administrator privileges ** tee **: A command that outputs a file and standard output at the same time. File overwrite is possible when combined with sudo **% **: Specify the currently open file

Then end with: q !.

vim


:q!

This will save it safely.

Recommended Posts

Edit and save read-only files with vim
Save and retrieve files with Pepper
Read and write csv files with numpy
Reading and writing JSON files with Python
The file edited with vim was readonly but I want to save it
Drag and drop local files with Selenium (Python)
Download and import files with Splunk external python
Vim with Atom
Reading and writing fits files with Python (memo)
Rename and move files (directories) with mv command
I tried to read and save automatically with VOICEROID2 2
I tried to automatically read and save with VOICEROID2
Automatically save .py and .html files in Jupyter notebook.
Read and write files with Slackbot ~ Bot development with Python ~
Save lists, dictionaries and tuples to external files python
Upload files with Django
Linux: files and directories
With and without WSGI
kobitonote.py --Synchronize and save items edited with Kobito to Evernote
Visualize cavity flow with matplotlib and save as gif animation
Upload and delete files to Google Cloud Storages with django-storage