How to switch to another user to continue editing while retaining the currently edited content in vim
PHPz
PHPz 2017-05-16 16:43:06
0
3
762

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?

PHPz
PHPz

学习是最好的投资!

reply all(3)
曾经蜡笔没有小新
:w !sudo tee %

Reference: http://vim.wikia.com/wiki/Su-write

伊谢尔伦

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.

黄舟

Save to another file, then exit, overwriting the file

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!