Learning Vim Command: ": w!sudo Tee%"

Source: Internet
Author: User
Tags stdin

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%"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.