开发者

How to time a vim session using 'time'

I'm trying to log the time I spend working in vim. I've got a script that works with gvim but when I try to set it up with vim it locks up the terminal session silently or with the message 'Vim: Warning: Output is not to a terminal'

Here is the script that works with gvim:

#!/bin/sh                                                                                                                                                                                                        

workfile="/home/na/writing/fiction.txt"                                                                                                                                                                          
worklog="/home/na/writing/worktime.log"                                                                                                                                                                          

d=`date --rfc-3339 date`                                                                                                                                                                                         
t=$( { /usr/bin/time -f "%e" /usr/local/bin/gvim -f -S /home/na/.vim/writeroom/writeroom.vim $workfile; } 2>&1 )                                                                                                 
w=`wc -w $workfile`                                                                                                                                                                                              

echo $d $t $w >> $worklog                                                                                                                                                                                        

When I close the gvim window I get a logfile containing a date, the number of seconds I spent editing the file, and a word count for the file.

2011-08-15 700.15 238869 /home/na/writing/fiction.txt                                                                                                                                                            

I would like the same using vim in a terminal session.

I understand vim talks to the terminal directly instead of to stdout but I don't care about what vim returns, I want the output from the time command.

These don't work:

t=`/usr/bin/time -f "%e" /usr/local/bin/vim -f $workfile`                                                                                                                                                            
t=$( { /usr/bin/time -f "%e" /usr/local/bin/vim -f $workfile; } 2>&1 )                                                                                                                                               
t=$( { /usr/bin/time -f "%e" /usr/local/bin/vim -f $workfile; } )                                                                                                                                                    

I suspect there's some combination of backticks and paranthesis that will make this work bu开发者_开发知识库t I haven't stumbled onto it yet.


The backtick or $() operators capture vim's output (what you see on the terminal when opening vim is vim's output from its stdout), so you can't do that.

You could try this instead:

start=$(date +%s)
/usr/local/bin/vim -f $workfile
end=$(date +%s)
duration=$((end-start))
echo "$duration"


Some versions of time (eg: GNU's) support a flag to set the output file. eg:

/usr/bin/time -o /tmp/foo vim

If you're using bash you'll need to use the path for time (or a leading \) as bash has a reserved word time which behaves similar to the time command (but operates on an entire pipeline) that does not support the -o flag.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜