0

Possible Duplicate:
How does the vim “write with sudo” trick work?

This situation always happen: I try to vi a configuration file but I forget to add sudo before it, and when I try to save it, vi prompt me that this file is read only.

How can I change to super-user privilege without get out of vi and re-edit that file?

Community
  • 1
  • 1
Yishu Fang
  • 9,448
  • 21
  • 65
  • 102

3 Answers3

2

This works for me in vi and vim:

:w !sudo tee %
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Helio Santos
  • 6,606
  • 3
  • 25
  • 31
0
cmap w!! %!sudo tee > /dev/null %

Add the line in ~ /. Vimrc and do: w!! to keep with sudo

m0zgen
  • 569
  • 7
  • 20
-2

Usually I save it in temp with :w /tmp/foo.txt and then sudo cp.

Wu Yongzheng
  • 1,707
  • 17
  • 23