Search code examples
macoscommand-linearrow-keysiterm

iTerm2 get previous DIFFERENT command using up and down arrow keys


I found it irritating that if you run one command 5 times you have to press the arrow key 6 times to get the previous command. Is it some way to change this behavior?

iTerm2 Build 1.0.0.20111020


Solution

  • That's not a feature of iTerm but of your shell's history feature. If you use the default Bash you can put this into your ~/.bashrc:

    export HISTCONTROL=ignoreboth
    shopt -s histappend
    # After each command, save and reload history
    export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"
    

    The first line will tell Bash to ignore duplicated and empty history entries. The second line will merge the history of multiple open sessions (e.g. in multiple tabs or windows). The thirs line will make sure that the history is preserved after each command.