Using Vim to edit files is very convenient and vim is the most common tool for modifying system configuration. But sometimes we take care of the convenience and forget that we have permission to edit the file. It is often found that they do not have permission to write the contents of the file when they are saved. What to do? Save the file using: W <newname> as a new file, and then replace it with the new file, although it is possible (and you need to pay attention to modify the file operation permissions), but still a bit of trouble. The following will use a command to save the file without permissions.
First up command:
: w!sudo Tee% >/dev/null
Here ":" is indicated in the command line mode of VIM. Note W and! Separated by a space between them.
The following is the first introduction: W!<cmd> command
: W!<cmd> is used to send the current buffer contents as standard input to the cmd command. For example, W!sh is to treat each row of the buffer as a shell command and then execute. For ease of understanding, here is an R command. : R!<cmd> is used to read the execution results of the <cmd> command into the current buffer. such as:. R!date is writing the current time to the current line (.).
Next is the tee command.
When Bo Master beginner Unix, feel LS, CP and other commands are easy to find is the abbreviation of the corresponding word. But tee commands are hard to understand and seldom used. Tee works as a standard input to the previous named standard output (that is, output to the screen) and then to the standard output and saved as the corresponding file. Drawing this process is just the letter T, which is why RMS and others named the command tee.
As an example:
LS |tee out.txt
LS will output the current directory list, this output is piped into the tee command, tee will be the input directly output the same time, back up to the OUT.txt file.
The last one is sudo, this used *nix should know, is to execute the command superviser.
So much for the prep, let's look at how the naming at the beginning of this article works.
First W!sudo Tee This part sends the buffer contents to the tee command. sudo ensures that the tee command is executed with supervisor so that write permissions are guaranteed.
Now tee has obtained the content of our file, then we will save the content as another file. Here we save the file as its own. Enter% to represent the file path name (% is the dedicated register that holds the current file path name in Vim, and is automatically replaced with the full path at the command line). Here we can actually save the file content with the Tee command, but the tee command also has standard output, in order to avoid repeated display, we throw it to the black hole, namely/dev/null.
After reading the command, we actually perform to see the effect.
Execute vi/etc/hostname. Execute after Edit: w! will pop up an error warning. Here we re-save with the command at the beginning. After executing the command, vim pops up a confirmation prompt that the content has changed. Note At this point, tee has helped us to write the contents of the file to the original pieces. We press O to confirm. Then quit as usual. This time we forgot sudo while editing the file, but we did successfully modify the file. Finally, if you do not really want to modify the hostname, finish this exercise to change the document back.
Use Vim to save files without editing permissions