Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
857 views
in Technique[技术] by (71.8m points)

linux - Why doesn't "history | vim" work?

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 the input of vim), but it returns with:

$history | vim
Vim: Warning: Input is not from a terminal
Vim: Error reading input, exiting...

Vim: Finished.

Can anybody explain this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 -

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...