Pipe Vim buffer to stdout

后端 未结 5 798
执念已碎
执念已碎 2021-02-07 02:37

I\'d like to use Vim in the middle of a pipe. This existing post looks like what I\'d like to do, except I was hoping to do it without Python\'s help -- only with bash. [It it

5条回答
  •  有刺的猬
    2021-02-07 02:42

    To print buffer to shell standard output, vim needs to start in Ex mode, otherwise it'll open "normal" way with its own window and clear any output buffers on quit.

    Here is the simplest working example:

    $ echo foo | vim +%p -escq /dev/stdin
    foo
    

    which is equivalent to:

    $ echo foo | vim -es '+%print' '+:q!' /dev/stdin
    foo
    

    Special file descriptor to standard input needs to be specified (/dev/stdin) in order to prevent extra annoying messages.

    And here are some examples with parsing strings:

    $ echo This is example. | vim '+s/example/test/g' '+%p' -escq! /dev/stdin
    This is test.
    $ echo This is example. | vim - '+s/example/test/g' '+%p' -escq!
    Vim: Reading from stdin...
    This is test.
    

    Related:

    • How to edit files non-interactively (e.g. in pipeline)? at Vim SE
    • How to write buffer content to stdout? at stackoverflow SE

提交回复
热议问题