Pipe Vim buffer to stdout

后端 未结 5 819
执念已碎
执念已碎 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:47

    You cannot simply put vim inside a pipe, because then Vim cannot display its UI.

    ls | vim - | more # Does not work
    

    One way to solve this is to use gvim -f - inside the pipe, as it opens in a separate window. You need to write the file via :w >> /dev/stdout and then :quit!.

    Alternatively (and the only way in a console-only non-graphical environment), you could write your own script / function vimAndMore that takes the command that should be following vim in the pipe as an argument, and goes like this:

    vimAndMore()
    {
        TMPFILE=/tmp/pipecontents
    
        # Slurp stdin into the temp file.
        cat - > "$TMPFILE" || exit $?
    
        # Reconnect stdin to the terminal, so that Vim doesn't complain with "Warning:
        # Input is not from a terminal", and the terminal is kept intact.
        exec 0

    And change the pipe to this:

    ls | vimAndMore | more
    

提交回复
热议问题