I often find myself in a situation where I'd like to send output to a temp file and then use VIM to open the temp file and inspect the results. I've come up with the following which generally seems to work pretty well:

function viewlog()
    local d="/tmp/viewlog"
    #local cmd="adb logcat"
    local cmd="tail --follow=name /var/log/syslog"
    #local cmd="tail --follow=name /var/log/syslog | grep myProcess"
    local stopmsg="TERMINATED"

    # setup temp file
    mkdir $d
    local fn=$(mktemp -p $d --suffix=".log")

    # start redirecting cmd to the tmp file in a subshell
    ( ( eval $cmd >> $fn && echo -n "$stopmsg") & ) 2> /dev/null
    local pid=$!

    # open the temp file in vim
    vim $fn +

    # kill the cmd when I exit vim
    kill $pid
    wait $pid

The cmd's output is put in a temp file and it opens in vim. I also have the following in my .vimrc to make reloading pretty easy:

map <F5> <ESC>:e!<CR>G

This allows me to easily "refresh" the temp file as new data is added.

This works great when I have a simple "generator" command like tail or adb logcat. but if I want to use a more complex command, like the commented out one with a pipe to grep above, it doesn't work - I don't see anything in my temporary file.

What is wrong with my shell function?

  • 4
    Isn't this what tee is supposed to do? <command> | tee <tempfile>
    – AlexP
    Jun 29 '17 at 20:25
  • but won't tee also put it all in stdout thus blasting my terminal?
    – awm129
    Jun 29 '17 at 20:33
  • 1
    Isn't that what you want? You asked how to redirect the output to a file and view it at the same time; what's the difference between "viewing the output" and "blasting the terminal"? And less is a thing, you know.
    – AlexP
    Jun 29 '17 at 21:09
  • Right. My title is a bit unclear - I'll try to clarify. I want to be able to open this temp file in vim while the command continues to append data to the tmp file. I'd prefer the cmd's output to never be displayed on the terminal. I'd prefer vim to less to take advantage of features like window splitting and syntax highlighting. Thanks!
    – awm129
    Jun 29 '17 at 21:37
  • Within vim you can type :edit! to update the buffer with new additions to the file, and some altogoobingleduckgoing shows vim file tail plugins and other things to research.
    – thrig
    Jun 29 '17 at 21:49

This is what autoread does:

:help autoread
When a file has been detected to have been changed outside of Vim and                      
it has not been changed inside of Vim, automatically read it again.                        
When the file has been deleted this is not done.

I have to admit, though, that this was never working in a satisfactory way for me. Depending on vim having focus or not, it might, or might not work. The feature completely broke down for me when using a tiling window manager. But give it a try, maybe it is working for you. Otherwise there are smarter alternatives on the market.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.