It seems that if I run the following inside the vim:
:w !wc -w
I get the word count of the file. But I don't understand the syntax. How does this work and how would I specify that I want the word count of a paragraph and not of the whole file?
It seems that if I run the following inside the vim:
:w !wc -w
I get the word count of the file. But I don't understand the syntax. How does this work and how would I specify that I want the word count of a paragraph and not of the whole file?
When you issue :w !wc -w, vim creates a temporary file and then pipes that file to the command following the !, or rather it puts your data in a temporary file and asks your shell to execute a command that looks something like the following:
(wc -w) < /tmp/vHhjUPf/2
Where that last part is some random folder/filename that vim stores your data in. One interesting thing to note, this command will fail in non-compliant shells like fish. This is because fish uses the (cmd) syntax for command substitution instead of its traditional use.
To specify a range of data to pass to an external command, type this:
:<range>w !<command>
For example,
:1,5w !wc -w
would count the number of words within the range enclosed by lines 1 and 5. Type :h 10.3 for more information on ranges.
You can also use
:<range>!<command>
to replace the contents of lines 1 to 5 with the output of the command, which is useful when using external commands to filter text. (e.g. for sorting.) Type :h ! for more information on filters.
If you wish to run an external command without having Vim pass text to its standard input, run the command like so: :!command.
The vim command :w simply writes to disk the current file.
Using :w newfilename you write the current file to a new filename.
The command :!ls -al run the external program ls with parameters -al and displays the result.
The command you mention (:w !wc -w) will (probably) simply write the current file into a pipe to a external commmand (wc -w) who in turn will count up the words in current file.