3

I would like to configure GNU screen such that it stores the command histories of all the different windows in different files.

I know by default GNU screen does not store the command histories (of its different windows) in a file at all (it stores them in memory instead), but it might be possible to tell it to store them in files instead?

The different command history files should have the names <session>.<window>.history, or similar.

Does anyone have an idea how to do that?

(Just to be clear, I want each GNU screen window to write a different file. I like that each window has a different history, and I typically run different types of commands in the different windows.)

Desperatuss0ccus
  • 252
  • 1
  • 4
  • 9
user9474
  • 2,448
  • 2
  • 25
  • 26

2 Answers2

5

Create a script somewhere that looks like this.

~/bin/myshell

#!/bin/bash
HISTFILE=~/.bash_history_w$WINDOW
# HISTFILE=~/.bash_history_w${WINDOW}_s${STY##*.}  # with session name.
export HISTFILE
exec /bin/bash

Adjust your .screenrc with a line like this.

shell ~/bin/myshell

Thanks, but there is a problem: It seems the files don't actually get written until I close a window. In my case though, the windows will always be active and never closed. How can I trigger that the files get written without closing the windows?

You can manually force a write by running the command history -w. I don't believe there is any way to have bash automatically commit the history other then at exit. There appears to be an option to do that in zsh though, search for INC_APPEND_HISTORY.

If you want a log of what was done per session you could use script for that. If you wanted to use script to create per session+window log under script you might adjust myshell like this.

#!/bin/bash
SHELL=/bin/bash  # reset the shell back to bash since screen -s will adjust to to myshell
export SHELL
script -a  -q -f ~/.sessionlog_w${WINDOW}_s${STY##*.}
Zoredache
  • 130,897
  • 41
  • 276
  • 420
  • Thanks, but there is a problem: It seems the files don't actually get written until I close a window. In my case though, the windows will always be active and never closed. How can I trigger that the files get written without closing the windows? – user9474 Jan 07 '11 at 00:28
  • 1
    You could put a command `PROMPT_COMMAND` to write history every time the prompt is issued. – Dennis Williamson Jan 07 '11 at 03:12
  • Is it possible to write the history files everytime the session is detached? – user9474 Jan 07 '11 at 04:49
  • Did you mean `history -a` instead of `history -h`? – user9474 Jan 07 '11 at 05:06
  • Sorry, I copied the wrong line, that is `history -w` – Zoredache Jan 07 '11 at 06:35
  • Isn't `history -a` better? It would just append the latest commands instead of writing the whole history? The whole history can be very very long. – user9474 Jan 07 '11 at 07:00
1

Shell history and screen history are two different things. A shell remembers commands that you type while screen remembers everything: commands that you type and their output (even the prompt).

If you want to keep the history from screen then you can use its logging feature. At the end of your ~/.screenrc add these commands:

screen
screen $SHELL -c 'screen -X logfile "/tmp/$STY.%n.history"; screen -p 0 -X deflog on; screen -p 0 -X log on'
Dennis Williamson
  • 62,149
  • 16
  • 116
  • 151