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?
Reference: http://vim.wikia.com/wiki/Su-write
Execute the following in vim to save the file contents without switching users. .
p.s. If there are no special symbols such as spaces in the file name, the quotation marks around the last % can be omitted.
Save to another file, then exit, overwriting the file