Skip to content

Instantly share code, notes, and snippets.

@johnlindquist
Created September 11, 2025 15:31
Show Gist options
  • Save johnlindquist/2c2c94006cd8d8fe161d55ab504c5ce3 to your computer and use it in GitHub Desktop.
Save johnlindquist/2c2c94006cd8d8fe161d55ab504c5ce3 to your computer and use it in GitHub Desktop.
Yabai window management script - move window right to next space
# Move Window Right - Yabai Window Management Script
## Overview
This Bash script is part of a macOS window management system using Yabai. It moves the currently focused window to the next space to the right, automatically creating a new space if the window is already at the rightmost space.
## Purpose
- **Primary function**: Move the active window one space to the right
- **Edge case handling**: Creates a new space when at the rightmost position
- **Focus management**: Ensures the moved window remains focused after the operation
## Key Features
### 1. **Intelligent Space Navigation**
- Queries the current space and finds the next available space to the right
- Uses `jq` to parse Yabai's JSON output for accurate space information
- Handles edge cases when there's no space to the right
### 2. **Automatic Space Creation**
- When at the rightmost space, automatically creates a new space
- Moves the window to the newly created space
- Maintains user focus on the moved window
### 3. **Comprehensive Logging**
- Logs all operations through a centralized `log_helper.sh` script
- Tracks before/after states for debugging
- Records window IDs, space indices, and display information
### 4. **Error Handling**
- Validates window ID retrieval before attempting operations
- Exits gracefully with error logging if no window is focused
- Uses proper null checks for JSON parsing
## Technical Details
### Dependencies
- `/opt/homebrew/bin/yabai` - Yabai window manager
- `/opt/homebrew/bin/jq` - JSON processor for parsing Yabai queries
- `log_helper.sh` - Centralized logging utility (same directory)
### Operation Flow
1. **Initial State Capture**: Logs current window, space, and display
2. **Window Identification**: Gets the focused window's ID
3. **Space Detection**: Finds the next space to the right
4. **Movement Logic**:
- If next space exists: Move window directly
- If at rightmost space: Create new space, then move
5. **Focus Restoration**: Ensures the moved window stays focused
6. **Final State Logging**: Records the end state for verification
### Key Functions
- `log_state()`: Captures and logs the current window management state
- `focus_window()`: Refocuses a specific window by ID with verification
### Implementation Notes
- Uses 0.1-second delays after space changes to ensure Yabai completes operations
- String comparison for empty space detection uses literal `'""'` check
- Window focus is explicitly restored after movement to handle Yabai quirks
## Usage
```bash
~/.config/scripts/move_window_right.sh
```
Typically bound to a keyboard shortcut via Karabiner-Elements or similar tools.
## Integration
Part of a larger window management system with complementary scripts:
- `move_window_left.sh` - Move window to previous space
- `space_right.sh` - Focus next space without moving windows
- `remove_empty_spaces.sh` - Clean up unused spaces
## Logging
Logs are written via `log_helper.sh` with structured format:
- **START/END**: Script lifecycle events
- **INFO**: Informational messages
- **ACTION**: Yabai commands being executed
- **ERROR**: Error conditions
- **DEBUG**: Detailed state information
---
*File: move_window_right.sh*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment