6

Sometimes I forget to do sudo vim and open files to which I don't have write permission. I saw this post and it was really helpful.

When I did :w !sudo tee % it asked me for a password and then the options (O)K and (L)OAD. This worked fine. But when I did :wq !sudo tee %, vim gave me an error -

E172: Only one file name allowed

Why didn't it let me go to the prompt after wq. I don't understand this error.

  • I wanted to leave a comment and ask those guys on stackoverflow.com/questions/1005/… but don't have enough reputation to leave a comment. – Hussain Tamboli Oct 11 '12 at 13:11
  • 1
    Putting random words in code formatting makes your questions harder to read. Use italics or bold for ordinary text, if necessary, please. – Mat Oct 11 '12 at 13:30
  • Sorry about that. I'll keep this in mind. Also I should put only the first occurrence of code-word in code, right? Don't mind me asking it here instead of meta. – Hussain Tamboli Oct 11 '12 at 13:34
  • 1
    You should put all code and commands in code blocks. For things like vim (i.e. command names that are also the app's name), I'd leave it in plain text when it's included in a plain sentence (e.g. "I use vim to do wonderful things"), but put code markings around it if it's part of a command (e.g. "When I run vim -d ... foo happens"). – Mat Oct 11 '12 at 13:38
  • Sure thing. thanks. Can't +1 for you. – Hussain Tamboli Oct 11 '12 at 13:40
7

Because :w and :wq are two different commands with different options.

Here is the description of the :w command with the !{cmd} option:

:[range]w[rite] [++opt] !{cmd}
                    Execute {cmd} with [range] lines as standard input
                    (note the space in front of the '!').  {cmd} is
                    executed like with ":!{cmd}", any '!' is replaced with
                    the previous command |:!|.

Note that :w! and :w !{cmd} are two completely different things; in the former, exclamation mark means "force write", in the latter it is a sort of a pipe sign to pass the whole range of text to the subsequent command (in your case, to tee run as superuser, which both returns the file to vi and saves it to the filename represented by the wildcard %).

Sadly, there is no such option for the command :wq. The only other available forms of this command are :wq! (here the exclamation mark is not for piping to another command, but for force quit, much like :w!) and :w[!] {file}.

  • can I :wq! and forcefully save it even if I don't have the write permissions? In my case if I am not the super user? – Hussain Tamboli Oct 12 '12 at 5:04
  • no. But you can forcefully overwrite a file. – January Oct 12 '12 at 5:14
  • what good may come from this. a user can tamper with other user's data. Doesn't this look bad? – Hussain Tamboli Oct 12 '12 at 5:28
  • 1
    you can forcefully overwrite your own file. Vim cannot give you more permissions that the system gives you. – January Oct 12 '12 at 5:31
  • yes. i just cross checked it with vim – Hussain Tamboli Oct 12 '12 at 5:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.