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
  • 8,880
  • 20
  • 59
  • 100

3 Answers3

2

This works for me in vi and vim:

:w !sudo tee %
the Tin Man
  • 155,156
  • 41
  • 207
  • 295
Helio Santos
  • 6,328
  • 3
  • 24
  • 30
0
cmap w!! %!sudo tee > /dev/null %

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

xck
  • 443
  • 7
  • 15
-2

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

Wu Yongzheng
  • 1,667
  • 17
  • 23