| 
 | 1 | +(context_managers)=  | 
 | 2 | + | 
 | 3 | +# Context Managers  | 
 | 4 | + | 
 | 5 | +libtmux provides context managers for all main tmux objects to ensure proper cleanup of resources. This is done through Python's `with` statement, which automatically handles cleanup when you're done with the tmux objects.  | 
 | 6 | + | 
 | 7 | +Open two terminals:  | 
 | 8 | + | 
 | 9 | +Terminal one: start tmux in a separate terminal:  | 
 | 10 | + | 
 | 11 | +```console  | 
 | 12 | +$ tmux  | 
 | 13 | +```  | 
 | 14 | + | 
 | 15 | +Terminal two, `python` or `ptpython` if you have it:  | 
 | 16 | + | 
 | 17 | +```console  | 
 | 18 | +$ python  | 
 | 19 | +```  | 
 | 20 | + | 
 | 21 | +Import `libtmux`:  | 
 | 22 | + | 
 | 23 | +```python  | 
 | 24 | +import libtmux  | 
 | 25 | +```  | 
 | 26 | + | 
 | 27 | +## Server Context Manager  | 
 | 28 | + | 
 | 29 | +Create a temporary server that will be killed when you're done:  | 
 | 30 | + | 
 | 31 | +```python  | 
 | 32 | +>>> with Server() as server:  | 
 | 33 | +...     session = server.new_session()  | 
 | 34 | +...     print(server.is_alive())  | 
 | 35 | +True  | 
 | 36 | +>>> print(server.is_alive())  # Server is killed after exiting context  | 
 | 37 | +False  | 
 | 38 | +```  | 
 | 39 | + | 
 | 40 | +## Session Context Manager  | 
 | 41 | + | 
 | 42 | +Create a temporary session that will be killed when you're done:  | 
 | 43 | + | 
 | 44 | +```python  | 
 | 45 | +>>> server = Server()  | 
 | 46 | +>>> with server.new_session() as session:  | 
 | 47 | +...     print(session in server.sessions)  | 
 | 48 | +...     window = session.new_window()  | 
 | 49 | +True  | 
 | 50 | +>>> print(session in server.sessions)  # Session is killed after exiting context  | 
 | 51 | +False  | 
 | 52 | +```  | 
 | 53 | + | 
 | 54 | +## Window Context Manager  | 
 | 55 | + | 
 | 56 | +Create a temporary window that will be killed when you're done:  | 
 | 57 | + | 
 | 58 | +```python  | 
 | 59 | +>>> server = Server()  | 
 | 60 | +>>> session = server.new_session()  | 
 | 61 | +>>> with session.new_window() as window:  | 
 | 62 | +...     print(window in session.windows)  | 
 | 63 | +...     pane = window.split()  | 
 | 64 | +True  | 
 | 65 | +>>> print(window in session.windows)  # Window is killed after exiting context  | 
 | 66 | +False  | 
 | 67 | +```  | 
 | 68 | + | 
 | 69 | +## Pane Context Manager  | 
 | 70 | + | 
 | 71 | +Create a temporary pane that will be killed when you're done:  | 
 | 72 | + | 
 | 73 | +```python  | 
 | 74 | +>>> server = Server()  | 
 | 75 | +>>> session = server.new_session()  | 
 | 76 | +>>> window = session.new_window()  | 
 | 77 | +>>> with window.split() as pane:  | 
 | 78 | +...     print(pane in window.panes)  | 
 | 79 | +...     pane.send_keys('echo "Hello"')  | 
 | 80 | +True  | 
 | 81 | +>>> print(pane in window.panes)  # Pane is killed after exiting context  | 
 | 82 | +False  | 
 | 83 | +```  | 
 | 84 | + | 
 | 85 | +## Nested Context Managers  | 
 | 86 | + | 
 | 87 | +Context managers can be nested to create a clean hierarchy of tmux objects that are automatically cleaned up:  | 
 | 88 | + | 
 | 89 | +```python  | 
 | 90 | +>>> with Server() as server:  | 
 | 91 | +...     with server.new_session() as session:  | 
 | 92 | +...         with session.new_window() as window:  | 
 | 93 | +...             with window.split() as pane:  | 
 | 94 | +...                 pane.send_keys('echo "Hello"')  | 
 | 95 | +...                 # Do work with the pane  | 
 | 96 | +...                 # Everything is cleaned up automatically when exiting contexts  | 
 | 97 | +```  | 
 | 98 | + | 
 | 99 | +This ensures that:  | 
 | 100 | + | 
 | 101 | +1. The pane is killed when exiting its context  | 
 | 102 | +2. The window is killed when exiting its context  | 
 | 103 | +3. The session is killed when exiting its context  | 
 | 104 | +4. The server is killed when exiting its context  | 
 | 105 | + | 
 | 106 | +The cleanup happens in reverse order (pane → window → session → server), ensuring proper resource management.  | 
 | 107 | + | 
 | 108 | +## Benefits  | 
 | 109 | + | 
 | 110 | +Using context managers provides several advantages:  | 
 | 111 | + | 
 | 112 | +1. **Automatic Cleanup**: Resources are automatically cleaned up when you're done with them  | 
 | 113 | +2. **Clean Code**: No need to manually call `kill()` methods  | 
 | 114 | +3. **Exception Safety**: Resources are cleaned up even if an exception occurs  | 
 | 115 | +4. **Hierarchical Cleanup**: Nested contexts ensure proper cleanup order  | 
 | 116 | +5. **Resource Management**: Prevents resource leaks by ensuring tmux objects are properly destroyed  | 
 | 117 | + | 
 | 118 | +## When to Use  | 
 | 119 | + | 
 | 120 | +Context managers are particularly useful when:  | 
 | 121 | + | 
 | 122 | +1. Creating temporary tmux objects for testing  | 
 | 123 | +2. Running short-lived tmux sessions  | 
 | 124 | +3. Managing multiple tmux servers  | 
 | 125 | +4. Ensuring cleanup in scripts that may raise exceptions  | 
 | 126 | +5. Creating isolated environments that need to be cleaned up afterward  | 
 | 127 | + | 
 | 128 | +[target]: http://man.openbsd.org/OpenBSD-5.9/man1/tmux.1#COMMANDS  | 
0 commit comments