Only one file name allowed error
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}
.
Hussain Tamboli
About me Full Stack Developer at LAZADA Linkedin
Updated on September 18, 2022Comments
-
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 over 11 yearsI wanted to leave a
comment
and ask those guys on stackoverflow.com/questions/1005/… but don't have enoughreputation
to leave a comment. -
Admin over 11 yearsSorry 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 over 11 yearsYou 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 runvim -d ...
foo happens"). -
Admin over 11 yearsSure thing. thanks. Can't +1 for you.
-
-
Hussain Tamboli over 11 yearscan 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 over 11 yearsno. But you can forcefully overwrite a file.
-
Hussain Tamboli over 11 yearswhat good may come from this. a user can tamper with other user's data. Doesn't this look bad?
-
Hussain Tamboli over 11 yearsyes. i just cross checked it with vim