如何在XMonad中按窗口名称/类名聚焦指定窗口并绑定快捷键?
Hey there, let's walk through exactly how to focus specific windows by their class name, window title, or app type (like Firefox) in XMonad, and bind these actions to hotkeys without any annoying interactive prompts. Here's everything you need to know:
1. First, set up the required imports
You'll need a few extra modules to work with window properties and focus actions. Add these to your ~/.xmonad/xmonad.hs file:
import XMonad import XMonad.Actions.WindowBringer (bringWindow) import XMonad.Util.WindowProperties (className, title) import XMonad.StackSet (focusWindow)
2. Focus a specific app (e.g. Firefox)
Let's start with a concrete example: focusing the first Firefox window that's open. First, confirm Firefox's class name by running xprop | grep WM_CLASS in a terminal, then clicking your Firefox window—you'll see something like WM_CLASS(STRING) = "firefox", "Firefox" (we'll use the second string here, "Firefox").
Add this function to your config:
-- Focus the first open Firefox window focusFirefox :: X () focusFirefox = do -- Find all windows with class name "Firefox" firefoxWindows <- findWindow (className =? "Firefox") case firefoxWindows of -- If we found windows, focus the first one (win:_) -> focusWindow win -- If no Firefox windows are open, do nothing [] -> return ()
3. Create generic functions for class/title matching
Want a reusable way to focus any app by its class name or window title? Add these generic functions:
-- Focus the first window matching a given class name focusByClass :: String -> X () focusByClass targetClass = do matches <- findWindow (className =? targetClass) case matches of (win:_) -> focusWindow win [] -> return () -- Focus the first window matching a given window title focusByName :: String -> X () focusByName targetName = do matches <- findWindow (title =? targetName) case matches of (win:_) -> focusWindow win [] -> return ()
4. Bind these actions to hotkeys
Now wire these functions up to your preferred shortcuts. In your keybindings section, add entries like this:
myKeys conf = mkKeymap conf $ [ -- Your existing keybindings go here... -- Bind Mod4+Shift+F to focus Firefox ("M-S-f", focusFirefox) -- Bind Mod4+Shift+T to focus your terminal (replace "Alacritty" with your terminal's class name) , ("M-S-t", focusByClass "Alacritty") -- Bind Mod4+Shift+D to focus Discord (using its window title) , ("M-S-d", focusByName "Discord") ]
Don't forget to link your custom keybindings in the main config:
main = xmonad $ def { keys = myKeys -- Your other XMonad config options (layout, border, etc.) go here }
5. Test it out
After saving your config, press Mod4+q to restart XMonad. Now your new shortcuts should work instantly—no prompts, just direct focus to the window you want.
Quick tip: Find window properties easily
If you're unsure of an app's class name or window title, run xprop | grep -E "(WM_CLASS|WM_NAME)" in a terminal, then click the target window. The output will show you exactly what strings to use in your functions.
内容的提问来源于stack exchange,提问作者Chris Stryczynski

