In a Linux environment, if you directly use the VI/Vim command to edit files without modification permissions, you will be prompted not to save the files, the general solution is to close the file and re-open the file with the sudo permission before saving it (provided that the user has the sudo permission ). In factVI/Vim ModeWith some simple commands, you canSave the current file without closing it (thanks to the method provided by Xiao Zhe ):
- Enter the following command:%! Sudo tee %>/dev/null
- Enter as promptedSudoPermission Password
- Enter"L"(LOad File)
- Input: QCommand to exit
About"%! Sudo tee %>/dev/null"This command is described as follows:
% # File content edited by VI/Vim
!# Pipelines
Sudo# Operate with root permission
Tee#Output the standard input (that is, the content of the currently edited file from the MPs Queue) to the standard output, and write it to the specified file (That is, the file currently edited by VI/vim.)
%#Files edited by VI/Vim
>/Dev/null# Redirect standard output/Dev/null (no output)