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?
teeis supposed to do? <command>| tee<tempfile>teealso put it all in stdout thus blasting my terminal?lessis a thing, you know.vimtolessto take advantage of features like window splitting and syntax highlighting. Thanks!vimyou 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.