This is a frequently encountered situation. For example, we often need to connect to the server and use vim to modify the configuration file. Most configuration file modifications require sudo
permissions, but when reading unnecessary. Therefore, many times we use the current system account to modify files for a long time, only to find out at the end :wq
that we do not have write permission. In the past, I used sudo vim
to re-edit after exiting, but this method is too troublesome. Is there any other way to retain the currently edited content when switching users?
伊谢尔伦2017-05-16 16:45:06
Execute the following in vim to save the file contents without switching users. .
:%!sudo bash -c "cat > '%'"
p.s. If there are no special symbols such as spaces in the file name, the quotation marks around the last % can be omitted.