4

I've recently begun to use iTerm2 instead of the macOS Terminal application and I'm really happy with it. I'm very used to use to exit command to close the instance that currently has focus. With instance I mean one of the following: Tab, Window or Split Panel.

How can I write a function exit that closes the instance that is currently running? The following workaround works for windows but neither for tabs nor for split panels:

function exit {
    osascript -e 'tell application "iTerm2" to close first window'
}

The solution should work with either zsh or bash.

  • 2
    exit is a shell builtin (command). Having a function with the same name is not a good idea. – fd0 Jun 18 '18 at 12:40
  • 1
    This issue is not very clear. When I have multiple instances of iTerm, typing exit only closes that instance (just tested to verify). – Allan Jun 18 '18 at 14:51
  • 1
    @Allan sorry for the confusion: With instance I mean one of the following: Tab, Window or Split Panel. That said I mean the following: If I have an iTerm2 Window containing of two tabs, I want to close the focused one by typing exit. If this first tab is closed, exit closes the whole window (this is the part is already working). – dtell Jun 18 '18 at 15:19
  • 1
    Ok...so the way that I am reading this is you want the exit function to operate differently when there is > 1 window/tab/pane. I don't see this as possible. – Allan Jun 18 '18 at 15:44
  • 2
    I think there's a confusion between exiting a session and closing a window. I'm willing to be that in Terminal, when you type exit, it closes the session but leaves the window/tab/pane open. Correct? – Allan Jun 18 '18 at 15:55
3

In Terminal, the default setting for handling closed sessions is to leave the window open.

In Preferences > Profiles:

enter image description here

In iTerm2, the default is to close the window

enter image description here

If you disable that option in iTerm2, it should give you the behavior you're looking for.

0

in your .profile you could add

alias killiTerm2='killall iTerm2'

the from the terminal, you would just call killiTerm2, and it would kill every running instance of iTerm2

  • This does in no way solve my problem since I don't want to close the whole application. – dtell Jun 18 '18 at 13:44
0

The section Scripting of the iTerm2 documentation helped me fixing my problem.

In .zshrc I defined the following function:

function exit {
    osascript ~/.zsh/.function_exit.as
}

With ~/.zsh/.function_exit.as as

tell application "iTerm2"
    tell current window to close current session
end tell

Additionally I set the following option in the iTerm2 settings:

enter image description here

enter image description here

Using all the above things together I achieve the following behaviour:

enter image description here

Since this does not depend on the Shell used everything should work perfectly using bash (and the corresponding dot-file).

  • 1
    This is a good solution with the exception of the function name. exit is a builtin command and creating a function with a reserved name could cause adverse results. – Allan Jun 18 '18 at 22:21

You must log in to answer this question.

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