5

I use GNU screen with 5 different sessions, each of which stores its own command history. But often I don't find a command in the history, although I'm sure I've used it exactly in the session I'm currently in, a few weeks ago.

It seems there is a hard limit on the history size? How can I set that to infinity?

A related question: How can I control where the history of a session is stored? Often, our sysadmin reboots the computer(s) and I lose all my sessions, and I have to do screen -wipe because the sessions are corrupted. Then, all histories are gone. :( I'd like to find the stored histories in that case and bind them in a newly created session.

P.S.: I use bash in all screen sessions.

UPDATE: I am not asking about how to unify the session histories. Just, I want each session to have an infinite history, and I want to be able to 'load' that history into a new session, should the old one get corrupted or deleted.

UPDATE 2: You've probably figured out: When I said I use GNU screen with 5 different sessions I really mean a GNU screen session with 5 different windows in it. Sorry.

Desperatuss0ccus
  • 252
  • 1
  • 4
  • 9
user9474
  • 2,368
  • 2
  • 24
  • 26

4 Answers4

6

You'd be better off using Bash's history than screen's. Screen keeps a scrollback buffer (probably in-memory rather than in a file). When you recall commands using Ctrl-a { it's actually digging through everything that appeared on the screen that's still in the buffer that looks like it follows a prompt character. There's not really a command history. You can increase the size of the scrollback buffer using screen -h num or the defscrollback num or scrollback num screen commands, by the way.

You can use warren's suggestion to keep your Bash history up to date. And/or you can use one of my logging functions found here that can save your IP address or screen session ID along with date, time, current working directory and actual command. I use this all the time myself. You may have to set the variable $hcmntextra, which is used by my functions, to include $STY so the screen session name is logged, too.

Dennis Williamson
  • 60,515
  • 14
  • 113
  • 148
  • Thanks. One thing, when I talk about the different sessions keeping different histories I mean: when I type the `history` command in different sessions I get the commands that I typed in those sessions. I'm not talking about the scrollback buffer. Or is that the same? – user9474 Dec 06 '09 at 21:50
  • Yes, screen's `history` command is bound to Ctrl-a { by default. If you press that or use that command, screen does a screen-scrape looking for recent commands. If you type a character or two then do that it tries to match what you typed to the beginning of a previous command. (If after this, you press up-arrow, for example, you're then using Bash's history - not screen's.) For an example of how screen history *doesn't* work (for me): If I call up a man page and exit, then type a letter that appears in the man page but not in a recent command, then do a Ctrl-a { - I get text from the man page. – Dennis Williamson Dec 07 '09 at 00:39
4

I asked a similar question on SU a bit ago: https://superuser.com/questions/37576/can-history-files-be-unified-in-

To quote the accepted answer:


here are two things you need to do:

Insert the command shopt -s histappend in your .bashrc. This will append to the history file instead of overwriting it.
Also in your .bashrc, insert PROMPT_COMMAND="$PROMPT_COMMAND;history -a; history -n" and the history file will be re-written and re-read each time bash shows the prompt.


See also the history man pages.

warren
  • 17,829
  • 23
  • 82
  • 134
  • Actually that is not what I meant. I like the fact that the histories of different sessions are kept apart. I do relatively unrelated stuff in the different sessions. I just don't want the histories to be purged automatically. I will update the question to make this clearer. – user9474 Dec 06 '09 at 18:51
  • ok - I thought you were looking to join them – warren Dec 06 '09 at 19:52
  • 1
    Did just the trick that I needed. Hard to picture why `shopt -s` is not a default! – pierce.jason Mar 28 '16 at 18:18
3

My preferred solution to this problem is a variation of one of the previous answers.

I like to have a separate file stored for each screen session/window combination and each file is appended each time its corresponding prompt is shown:

if [[ "$STY" = "" ]]; then STY="none"; fi
if [[ "$WINDOW" = "" ]]; then WINDOW="none"; fi
export HISTFILE=~/.bash_history.$STY.$WINDOW;


PROMPT_COMMAND="$PROMPT_COMMAND; history -a"

This creates the following history files:

.bash_history.none.none
.bash_history.2756.pts-9.linux.0
.bash_history.2756.pts-9.linux.1
.bash_history.2881.pts-9.linux.0

If you want window {x} to always use its own history file but use the same file regardless of the screen session, you can merely leave out the STY variables:

if [[ "$WINDOW" = "" ]]; then WINDOW="none"; fi
export HISTFILE=~/.bash_history.$WINDOW;


PROMPT_COMMAND="$PROMPT_COMMAND; history -a"

This would create the following history files:

.bash_history.none
.bash_history.0
.bash_history.1
.bash_history.2
1

The command histories for each window are in memory for each bash session. You'd have to have bash write to different history files for them to persist and remain separate like you're asking.

You can do this by setting HISTFILE per screen session. I imagine the way to do this would be something like this in your startup file:

if [[ "$WINDOW" != "" ]] ; then export HISTFILE=~/.bash_history.$WINDOW ; fi

Note that this doesn't solve the problem of updating the history file when your shells are terminated by a reboot.

staticsan
  • 1,529
  • 1
  • 11
  • 14