Search code examples
shellterminalconfigfish

How can I record work history or log including command and output on terminal with fish?


I'm trying to edit /.config/fish/config.fish and add script like this env now= date +%Y%m%d%H%M%S ;set path /Users/t/logs/script_{$now}.txt ;script {$path}. But it does not work.

How should I edit config.fish file in order to record terminal log including output ?


Solution

  • First, your attempt to incorporate a timestamp in the file name is wrong. It should be now=(date +%Y%m%d%H%M%S). Too, prefixing it with the env command means it won't be visible to the subsequent statements because the env causes the var to only be visible to whatever command is part of the env command. What you wrote is the equivalent of

    env now=something
    path=...
    script $path
    

    That first env will simply add now to the env vars then display the list of env vars.

    Second, you should ensure this is only done for interactive shells:

    if status is-interactive
        now=(...)
        set path /Users/...$now.txt
        script $path
    end
    

    Third, you need to avoid infinite recursion. If you don't then the shell started by the script command will start another script'ed session. There are many ways to do this but the simplest is an env var:

    if test -z "$SCRIPTED"
        set -gx SCRIPTED nested
        now=(...)
        path=/Users/...$now.txt
        script $path
    end
    

    Fourth, you may want to do exec script $path once you're sure this is working reliably. Otherwise when you exit the captured session you're returned to a non-capturing shell and you may not notice the transition. Alternatively, add something like echo WARNING: your session is no longer being captured after the script command to help make it really obvious.