如何在Awesome WM中实现窗口多显示器最大化(含三屏横向快捷键操作)
Hey there! I’ve spent plenty of time tweaking Awesome WM for multi-monitor setups, so let’s break down exactly how to get your window maximized across 3 horizontal monitors (with a restore shortcut) and cover the general method for any number of displays.
1. Custom Shortcut for 3 Horizontal Monitors
First, we’ll add a custom function to your rc.lua that handles both maximizing across all three screens and restoring the window to its original state.
Step 1: Add the Function & State Storage
Paste this code into your rc.lua (I recommend putting it in the "Client Management" section, or near other window manipulation functions):
-- Global variable to save original window geometry for restore local three_monitor_restore = {} -- Maximize window across 3 horizontal monitors, or restore it function maximize_across_three_monitors(c) -- Get all screens sorted left-to-right (matches your horizontal setup) local screens = screen.screens table.sort(screens, function(a, b) return a.geometry.x < b.geometry.x end) -- Exit if we don't have at least 3 screens if #screens < 3 then return end -- Calculate the combined bounds of the three monitors local total_x = screens[1].geometry.x local total_y = screens[1].geometry.y local total_width = screens[3].geometry.x + screens[3].geometry.width - total_x local total_height = math.max( screens[1].geometry.height, screens[2].geometry.height, screens[3].geometry.height ) -- Save original state if we're maximizing, or restore if we're toggling back if not three_monitor_restore[c] then -- Save every detail of the window's original state three_monitor_restore[c] = { x = c.x, y = c.y, width = c.width, height = c.height, maximized = c.maximized, fullscreen = c.fullscreen } -- Disable default maximize/fullscreen to avoid conflicts c.maximized = false c.fullscreen = false -- Apply the multi-monitor geometry c:geometry({x = total_x, y = total_y, width = total_width, height = total_height}) else -- Restore the window to its original state c.x = three_monitor_restore[c].x c.y = three_monitor_restore[c].y c.width = three_monitor_restore[c].width c.height = three_monitor_restore[c].height c.maximized = three_monitor_restore[c].maximized c.fullscreen = three_monitor_restore[c].fullscreen -- Clear the saved state so the next press maximizes again three_monitor_restore[c] = nil end end
Step 2: Bind the Shortcut
Add this key binding to your rc.lua (adjust the keys to your preference—here I use Mod4 + Shift + M):
-- Bind multi-monitor maximize/restore to a shortcut awful.key({modkey, "Shift"}, "m", function(c) maximize_across_three_monitors(c) end, {description = "maximize/restore window across 3 horizontal monitors", group = "client"})
2. General Method for Any Number of Monitors
If you ever want to maximize windows across a custom set of monitors (not just three), here’s a flexible function that works for any display arrangement:
Step 1: Add the General Function
-- Global storage for window states local multi_monitor_restore = {} -- Maximize window across specified screens, or restore it function maximize_across_screens(c, target_screens) -- Default to all screens if no target is provided target_screens = target_screens or screen.screens -- Sort screens (adjust the sort logic if you have vertical/hybrid arrangements) table.sort(target_screens, function(a, b) return a.geometry.x < b.geometry.x end) -- Calculate the combined bounds of the target screens local min_x = math.huge local min_y = math.huge local max_x = 0 local max_y = 0 for _, s in ipairs(target_screens) do min_x = math.min(min_x, s.geometry.x) min_y = math.min(min_y, s.geometry.y) max_x = math.max(max_x, s.geometry.x + s.geometry.width) max_y = math.max(max_y, s.geometry.y + s.geometry.height) end -- Toggle between maximize and restore if not multi_monitor_restore[c] then multi_monitor_restore[c] = { x = c.x, y = c.y, width = c.width, height = c.height, maximized = c.maximized, fullscreen = c.fullscreen } c.maximized = false c.fullscreen = false c:geometry({x = min_x, y = min_y, width = max_x - min_x, height = max_y - min_y}) else c.x = multi_monitor_restore[c].x c.y = multi_monitor_restore[c].y c.width = multi_monitor_restore[c].width c.height = multi_monitor_restore[c].height c.maximized = multi_monitor_restore[c].maximized c.fullscreen = multi_monitor_restore[c].fullscreen multi_monitor_restore[c] = nil end end
Step 2: Example Bindings
Here are some useful shortcuts you can add with this function:
- Maximize across all monitors:
awful.key({modkey, "Control"}, "m", function(c) maximize_across_screens(c) end, {description = "maximize/restore window across all monitors", group = "client"}) - Maximize across monitors 1 and 2 only:
awful.key({modkey, "Control", "Shift"}, "m", function(c) maximize_across_screens(c, {screen[1], screen[2]}) end, {description = "maximize/restore window across monitor 1 & 2", group = "client"})
3. Quick Tips
- After editing
rc.lua, pressMod4 + Ctrl + Rto reload Awesome WM and apply your changes. - If you have a vertical or mixed monitor arrangement, adjust the
table.sortlogic (e.g., sort bya.geometry.yinstead ofxfor vertical stacks). - Make sure to test this with windows that aren’t already in fullscreen or default maximized mode—those states can interfere with the custom geometry.
内容的提问来源于stack exchange,提问作者Sheridan

