Why doesn't “history | vim” work?

前端 未结 3 1237
说谎
说谎 2020-12-15 10:01

I want to use the Vim to see the result of history (not in the shell). I think history | vim will work (use the result of history as t

相关标签:
3条回答
  • 2020-12-15 10:45

    Apart of vim -, you may try bash command substitution like:

    vim <(history)
    

    See also:

    • How to write whole buffer to standard output from the command line? at Vim SE
    • How to edit files non-interactively (e.g. in pipeline)? at Vim SE
    0 讨论(0)
  • 2020-12-15 11:06

    By piping into vim, you are changing the standard input stream. Because vim is an interactive program, it requires the standard input to be the console.

    If you want to view in vim, you should tell it you are reading the file from stdin (by supplying the argument -):

    history | vim -
    

    Alternatively, you could just use more or less:

    history | more
    history | less
    

    These latter two are preferable. If you pipe into vim, it will see your "file" as having modifications, and so you can't quit with a straight :q command. Instead you have to force quit by :q!, which is a bit clunky.

    On the other hand, you can exit more or less just by typing q. Have a look at the man-page for these two programs. You'll use them a lot.


    As recommended by Russell Silva in the comments, you can open vim in read-only mode when you read from stdin. Just supply the -R argument. Then you can quit normally without needing the override:

    history | vim -R -
    
    0 讨论(0)
  • 2020-12-15 11:06

    This happend to me trying to send it to background console (&)

    One script used:

    ... 
    vi "$file" &
    ...
    # change to just:
    
    vi "$file"
    

    removing &, problem went away.

    0 讨论(0)
提交回复
热议问题