I recently switched to Ghostty and wanted to restore my terminal sessions after restarts - including the working directory, window positions, splits, and even background colors I use to visually distinguish projects.
Shell Integration
First, enable shell integration in your Ghostty config (~/.config/ghostty/config):
shell-integration = bash
Then add this to the top of your ~/.bashrc:
# Ghostty shell integration for Bash. This should be at the top of your bashrc!
if [ -n "${GHOSTTY_RESOURCES_DIR}" ]; then
builtin source "${GHOSTTY_RESOURCES_DIR}/shell-integration/bash/ghostty.bash"
fiThis enables features like clicking to open URLs and proper working directory tracking. See the Ghostty shell integration docs for more details.
Window State Restoration
To restore window positions, splits, and working directories after restart, add this to your Ghostty config:
window-save-state = always
Now when you quit and reopen Ghostty, your windows come back exactly where they were, with the correct directories.
Restoring Background Colors
I use different background colors for different projects - dark blue for my main app, dark brown for tools, etc. The problem is that window-save-state doesn’t restore terminal escape sequences like background colors.
The solution is to use PROMPT_COMMAND to automatically set the background based on the current directory. Add this to your ~/.bashrc:
# Auto-set background color based on current directory
set_bg_for_directory() {
local appearance
appearance=$(defaults read -g AppleInterfaceStyle 2>/dev/null)
# AppleInterfaceStyle is "Dark" in dark mode, missing/empty in light mode
if [ "$appearance" = "Dark" ]; then
case "$PWD" in
*/my-app*)
printf '\e]11;#1a1b26\a' # dark blue
;;
*/my-tools*)
printf '\e]11;#31261e\a' # dark brown
;;
*)
printf '\e]11;#000000\a' # default black
;;
esac
else
case "$PWD" in
*/my-app*)
printf '\e]11;#e8e8f0\a' # light blue
;;
*/my-tools*)
printf '\e]11;#f0ebe5\a' # light brown
;;
*)
printf '\e]11;#ffffff\a' # default white
;;
esac
fi
}
# Run on every prompt
PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND;}set_bg_for_directory"Now when Ghostty restores a window with its working directory, the first prompt automatically sets the correct background color. As a bonus, the color also updates whenever you cd between projects or switch between macOS dark and light mode.
https://github.com/ghostty-org/ghostty/blob/main/src/shell-integration/README.md
https://deepwiki.com/ghostty-org/ghostty/9.1-shell-integration-system