How to write buffer content to stdout?

后端 未结 9 1763
执笔经年
执笔经年 2020-12-09 01:54

Is there any chance to write the content of the current vim buffer to stdout?

I\'d like to use vim to edit content that was passed via stdin - without the need of a

9条回答
  •  清歌不尽
    2020-12-09 02:27

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

    Here is the simplest working example:

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

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

    And here are some string parsing examples:

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

    Here is a simple example using ex which is equivalent to vi -e:

    ex -s +%p -cq /etc/hosts
    

    Related:

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

提交回复
热议问题