34

I generally find myself using two or three tabs in my Terminal on OS X.

A minor inconvenience is that, by default, the tabs don't share their command history. So, searching for a previous command that I ran on another tab by typing history | grep thingIamlooking for does not find it.

Is there a way of forcing Terminal tabs to share history, or is there some other "history_of_all_tabs" command?

0

2 Answers 2

37

I don't think there is a way to share history amongst Terminal tabs only, but here is something that could help you.

The history is not something that is handled by your terminal but by your shell (bash, tcsh, zsh, etc.)

Here are some options that'll help you set up a shared history amongst all the terminal windows (the shell instances).

This should be put somewhere in your .bashrc file.

export HISTCONTROL=ignoredups:erasedups  # no duplicate entries
export HISTSIZE=100000                   # big big history
export HISTFILESIZE=100000               # big big history
shopt -s histappend                      # append to history, don't overwrite it

# Save and reload the history after each command finishes
export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"
6
  • 1
    +.5 for good answer, +.5 for good grammar ;)
    – Matt
    Jul 26, 2012 at 9:49
  • @Lifely's solution isn't working for me in El Capitan. At all. Even the max length of the history file. Is there a new solution since 2012? (I'd have commented above, but don't have the rep)
    – JP Duffy
    Apr 26, 2016 at 20:17
  • 1
    This is working for me (added in .bash_profile) in El Capitan with the addition of export SHELL_SESSION_HISTORY=0 in my .bash_profile and running: touch ~/.bash_sessions_disable
    – Andy
    May 31, 2016 at 17:12
  • works great on Ubuntu 18.04 for bash Sep 26, 2018 at 0:45
  • 1
    Please see this comment: unix.stackexchange.com/questions/1288/… "The problem with this PROMPT_COMMAND solution is that the numbers for each history item changes after each command :(. For example if you type history and 1) ls 2) rm, then you do !1 to repeat 1, the history number might change and might run the rm command..." Jan 21, 2020 at 23:52
2

For zsh users, append the following to your ~/.zshrc to share history between your terminals:

# Appends every command to the history file once it is executed
setopt inc_append_history
# Reloads the history whenever you use it
setopt share_history
# (Nice to have) ignore consecutive duplicates in history
setopt histignoredups

Taken from this blog post: Sharing history in zsh - Markus Wein

1
  • 1
    To make this equivalent with the accepted answer, you can add setopt histignoredups to dedup the history.
    – Allan Bogh
    Feb 27 at 18:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .