2

Is there a way to increase the default amount of history reverse-i-search (CTRL+R) keeps? In Ubuntu 16.04, it doesn't seem to have a very long "memory", sometimes even commands from yesterday are already gone from its history, ie. will not appear even after cycling through all entries with repeated CTRL+R.

I'm using Terminator as the console.

Juha Untinen
  • 143
  • 1
  • 8

3 Answers3

4

It is really easy, just change your HISTSIZE and HISTFILESIZE variables in you .bashrc

HISTSIZE=-1
HISTFILESIZE=-1

In bash 4.3 and later you can also use HISTSIZE=-1 HISTFILESIZE=-1 to set it to be unlimited.

Consider that HISTSIZE is the number of lines or commands that are stored in memory in a history list while your bash session is ongoing.

HISTFILESIZE is the number of lines or commands that (a) are allowed in the history file at startup time of a session, and (b) are stored in the history file at the end of your bash session for use in future sessions.

Take a look at this link

Regards,

3

There is no time limit but rather a line or size limit on the history of the commandline.

You can modify that in your ~/.bashrc

export HISTSIZE=500000
export HISTFILESIZE=500000

Would mean a "memory" of 500.000 lines in your history, both currently (HISTSIZE) and written to your history file (HISTFILESIZE).

However, keep in mind that the file is only written when you exit a terminal.

You can reverse-i-search your commands in the same terminal, before the history is written to the history file, but cannot find your commands in any new terminals before you have not exited the original one!

Robert Riedl
  • 4,351
  • the file is only written when you exit a terminal. Ah, I think that explains it. I generally leave it "running" with the host OS (virtualbox) in hibernation. In my .bashrc there was HISTSIZE=1000 and HISTFILESIZE=2000 from some team template. – Juha Untinen Feb 14 '18 at 09:48
  • 1
    history -a writes it "on demand", if you need it – Robert Riedl Feb 14 '18 at 09:51
  • There is also a hacky solution to write history immediately, with PROMPT_COMMAND="history -a" in the .bashrc described here. Although I'm not sure I can recommend that... what I would recommend is setting a HISTTIMEFORMAT like this export HISTTIMEFORMAT='[%Y.%m.%d %H:%M:%S] ' so you see exactly when you executed the command – Robert Riedl Feb 14 '18 at 09:56
  • update: I've been running this "hacky solution" not for three years without issues on multiple devices. So I can definitely recommend this. Its really great to have your history immediately sync up on every command window or ssh session (on the same device) – Robert Riedl Jun 22 '23 at 08:07
1

The size of the history is specified with the two variables HISTSIZE (history of a single terminal session, saved in RAM) and HISTFILESIZE (size of the history file, usually ~/.bash_history). This size is set in number of lines in your ~/.bashrc file, e.g.:

HISTSIZE=1000
HISTFILESIZE=10000

This saves 1000 lines (= commands) per terminal session and 10000 in the history file.

The reverse-i-search searches the history of the current session as well as the history file, but it can't include commands from other sessions that are still opened.

Further reading:

dessert
  • 39,982