Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.)

share|improve this question
add comment

2 Answers 2

up vote 5 down vote accepted

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##*.}
share|improve this answer
    
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 7 '11 at 0:28
1  
You could put a command PROMPT_COMMAND to write history every time the prompt is issued. –  Dennis Williamson Jan 7 '11 at 3:12
    
Is it possible to write the history files everytime the session is detached? –  user9474 Jan 7 '11 at 4:49
    
Did you mean history -a instead of history -h? –  user9474 Jan 7 '11 at 5:06
    
Sorry, I copied the wrong line, that is history -w –  Zoredache Jan 7 '11 at 6:35
show 1 more comment

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'
share|improve this answer
1  
Note that the most recent version (4.1) of screen has a %S escape which would greatly simplify doing this. –  Dennis Williamson Jan 7 '11 at 3:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.