Can I gain root permission without leaving vim?

前端 未结 2 1393
半阙折子戏
半阙折子戏 2021-02-05 12:33

Sometimes when I edit a file which require root permission (e.g. files under /etc), but I forget run vim as sudo.

After edit finished, and type :wq to save

相关标签:
2条回答
  • 2021-02-05 13:13

    To force a save use the following command

    :w !sudo tee %
    

    It will prompt you for your password.

    0 讨论(0)
  • 2021-02-05 13:14

    The mentioned trick with tee is nice; if you need this often I'd recommend the SudoEdit plugin, which offers a :SudoWrite command (and even a sudo: protocol handler).

    0 讨论(0)
提交回复
热议问题