Q:

How can I duplicate the terminal/prompt in a file like using history + sink

I know the sink command can divert the stdout to a file, but basically if I do this in the command window:
library(data.table)
a = 1;
b = 2;
a
[1] 1

Only the last line [1] 1will be printed in the file.
Is there a way my whole command window could be printed to a file like it is done with sink ?
NOTE: I want it to be done each time I write something to avoid losing everything if R crashes, meaning I do not want to have to type printAllCommandToFile() for this to be done

A:

What about txtStart from the "TeachingDemos" package? See here.
Sometimes, when introducing students to R, I've recommended it to help them remember what they did and what the results were, a situation somewhat like you describe.
In my experience on a Linux machine, even if you close R without calling txtStop, the output is saved to whatever text file you had specified at the start of your session. 

