Only one file name allowed error

5,184

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}.

Share:
5,184
Hussain Tamboli
Author by

Hussain Tamboli

About me Full Stack Developer at LAZADA Linkedin

Updated on September 18, 2022

Comments

  • Hussain Tamboli
    Hussain Tamboli over 1 year

    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.

    • Admin
      Admin over 11 years
      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.
    • Admin
      Admin over 11 years
      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.
    • Admin
      Admin over 11 years
      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").
    • Admin
      Admin over 11 years
      Sure thing. thanks. Can't +1 for you.
  • Hussain Tamboli
    Hussain Tamboli over 11 years
    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?
  • January
    January over 11 years
    no. But you can forcefully overwrite a file.
  • Hussain Tamboli
    Hussain Tamboli over 11 years
    what good may come from this. a user can tamper with other user's data. Doesn't this look bad?
  • Hussain Tamboli
    Hussain Tamboli over 11 years
    yes. i just cross checked it with vim