3

Vim newb here. I'm trying to understand the behaviour of this documented function:

                                                    *:w_c* *:write_c*
:[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 |:!|.

I tried the following:

  • Open vim
  • go to insert mode and type the text 'foo'
  • Enter the ex command :w ! touch

I expected this to create a file named 'foo', as typing 'touch foo' does in the shell. Instead I get this error:

:write ! touch
usage:
touch [-A [-][[hh]mm]SS] [-acfhm] [-r file] [-t [[CC]YY]MMDDhhmm[.SS]] file ...

shell returned 1

It's like it's sending an empty buffer to touch. I tried saving the file to see if that made a difference but it did not. I'm reading the book 'Practical Vim' which gives this example:

:write ! sh

will run the contents of the buffer as shell commands. If I change foo to echo foo and run this command I get the expected behaviour:

:w ! sh
foo

Thanks in advance for any help.

1 Answer 1

2

The "bang" command passes buffer contents through stdin. But "touch" expects its argument on the command line.

To "connect" the two things use xargs utility.

:w !xargs touch
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.