Skip to content

Instantly share code, notes, and snippets.

@k33bs
Last active January 14, 2026 03:15
Show Gist options
  • Select an option

  • Save k33bs/dd453281e97de7282b798cf707891ffb to your computer and use it in GitHub Desktop.

Select an option

Save k33bs/dd453281e97de7282b798cf707891ffb to your computer and use it in GitHub Desktop.
Kitty terminal config with session restore (iTerm2-like)

Kitty Terminal Config with Session Restore

Windows, tabs, layouts and working directories saved on quit, restored on startup.

Installation

  1. Copy save-session.py to ~/.config/kitty/
  2. Create empty session file: touch ~/.config/kitty/last-session.conf
  3. Add contents of kitty-config.conf to the END of your ~/.config/kitty/kitty.conf
  4. Restart kitty

Usage

  • cmd+q saves your session (tabs + directories) then quits
  • Next time you open kitty, all tabs restore to their directories
  • Note: Menu quit won't save session - always use cmd+q
# =============================================================================
# Window
# =============================================================================
tab_bar_style powerline
remember_window_size yes
# New tabs/windows open in same directory
map cmd+t launch --cwd=current --type=tab
map kitty_mod+t launch --cwd=current --type=tab
map cmd+n launch --cwd=current --type=os-window
map kitty_mod+n launch --cwd=current --type=os-window
# =============================================================================
# Session restore (reopen with previous tabs/directories)
# =============================================================================
startup_session ~/.config/kitty/last-session.conf
map cmd+q kitten ~/.config/kitty/save-session.py
#!/usr/bin/env python3
"""Kitten to save session and quit kitty."""
import os
def main(args):
pass
from kittens.tui.handler import result_handler
@result_handler(no_ui=True)
def handle_result(args, answer, target_window_id, boss):
session_file = os.path.expanduser("~/.config/kitty/last-session.conf")
lines = []
first_os_window = True
# Iterate through ALL OS windows
for os_window_id in boss.os_window_map:
tm = boss.os_window_map[os_window_id]
if first_os_window:
first_os_window = False
else:
lines.append("")
lines.append("new_os_window")
first_tab = True
for tab in tm.tabs:
if first_tab:
first_tab = False
else:
lines.append("")
lines.append("new_tab")
lines.append(f"layout {tab.current_layout.name}")
windows = list(tab.windows)
if not windows:
continue
# First window - no location needed
first_window = windows[0]
cwd = first_window.cwd_of_child
if cwd:
lines.append(f"launch --cwd={cwd}")
else:
lines.append("launch")
# Subsequent windows - determine split direction from geometry
for i, window in enumerate(windows[1:], 1):
cwd = window.cwd_of_child
# Compare with previous window to guess split direction
prev = windows[i - 1]
# If window is to the right or left, it's hsplit
# If window is above or below, it's vsplit
if window.geometry.left != prev.geometry.left:
location = "hsplit"
else:
location = "vsplit"
if cwd:
lines.append(f"launch --location={location} --cwd={cwd}")
else:
lines.append(f"launch --location={location}")
# Write session file
with open(session_file, "w") as f:
f.write("\n".join(lines))
# Quit kitty
boss.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment