Running external commands in vim

5,287

Solution 1

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.

Solution 2

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.

Solution 3

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.

Share:
5,287

Related videos on Youtube

Cratylus
Author by

Cratylus

Updated on September 18, 2022

Comments

  • Cratylus
    Cratylus over 1 year

    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?

    • Cratylus
      Cratylus almost 11 years
      I want to understand the syntax of running external programs inside vim and this is just an example. In this specific example I get a word count which is useful feature but I don't know how to use the syntax to get information the way I would need.
    • goldilocks
      goldilocks almost 11 years
      See my comment to Evan's answer re: using VISUAL mode.
  • Cratylus
    Cratylus almost 11 years
    !w newfilename does not seem to work for me.
  • goldilocks
    goldilocks almost 11 years
    You can also select a range from visual mode -- ie., if you select a block of text in VISUAL and then run an external command, just that block gets fed to the command's stdin.
  • user79591
    user79591 almost 11 years
    In visual mode, ! expands to '<,'>!. < and > are marks that mark the start and end of the current/last visual selection, respectively. 'mark is the syntax for using a mark as part of a range.
  • hildred
    hildred over 10 years
    @Cratylus that was a typo it should be :w newfilename
  • n s
    n s almost 7 years
    Is there any known workaround for fish shell? It seems like this would be a common bug, but some searching didn't turn up an answer.