Learning Vim Command: ": w!sudo Tee%"
The original Original url:http://www.haw-haw.org/node/1501 from Commandlinefu
The original text explains this order in this way:
Save the file you are editing in Vim without the necessary permissions.
(Save a file edited in Vimwithout the needed permissions)
Below we analyze how this command is done to save the file without the necessary permissions
: w!sudo Tee%
Man, Vim will find
Percent ("%") represents the current file name
According to the man VIM hint:
: [Range]w[rite]! {cmd} Execute {cmd} with [Range] lines as standardinput (note the space in front of the '! '). {cmd} isexecuted like with ":! {cmd} ", any '!" was replaced withthe previous command ": W!sudo tee%" This command means that the contents of the currently edited file are entered as standard input to the command sudo tee In the file name, Sudo is saved as the current file name this feature may actually be more necessary for Debian and Ubuntu users because we are direct root often forget sudo to edit/etc files directly with vim (but not necessarily, Vim found saved files can not be saved when prompted) and so on, save time only found no permissions. The curve method is to save a temporary file first, exit and then SUDOCP back. However, in fact, the process can be done directly in Vim, which is the order. Consult the VIM documentation (input: HELP:W), which will refer to the command: w! {cmd}, let vim execute an external command {cmd}, and then pass the contents of the current buffer from stdin. Tee is a gadget that saves stdin to a file. and%, is the name of a read-only register in Vim, which always holds the file path of the currently edited file.
Learning Vim Command: ": w!sudo Tee%"