Search code examples
zshiterm2oh-my-zsh

zsh - How can I do automatic resetting of the session on invalid command execution; avoiding "Broken Pipe" message


I am using oh-my-zsh on iTerm2. Every time an invalid command is executed, zsh shows the "Broken Pipe" message. Please see the screen-shot below:

zsh-broken-pipe-message

I have to manually reset the session by pressing "command+R" (Macbook) in order to get the prompt back and start using the shell again.

I would want the zsh/iTerm2 to bring back the prompt automatically in case an invalid command is executed.

Is there any setting/configuration I can do in zsh to achieve the desired behavior?

EDIT: My iTerm is configured to use zsh instead of login shell.


Solution

  • After doing some research, I found the solution.

    We can use the zsh's ERROR trap to re-launch the shell in case there is an error in the command or the command exits with error status.

    I wrote the following in .zshrc file:

    TRAPZERR() {
    if [[ $? -gt 0 ]];then
         /Applications/iTerm.app/Contents/MacOS/iTerm2 --launch_shell
    fi
    }
    

    And it worked !!