Skip to content
The Chat Archive
gpt-4o-search-preview-high-loc· OpenAI LMArena Search Arena 24K

awesomewm lua code to cycle windows and activate the selection with hotkey alt+tab (like…

  1. User

    awesomewm lua code to cycle windows and activate the selection with hotkey alt+tab (like mac), hotkey to go to workspace 1-4, and hotkeys to move focused window to workspace 1-4. include making the windows themselves draggable

  2. Assistant

    To configure AwesomeWM to cycle through windows using Alt+Tab, switch to workspaces 1-4, move the focused window to workspaces 1-4, and make windows draggable, you can add the following Lua code to your AwesomeWM configuration file (rc.lua):

    -- Import necessary modules
    local awful = require("awful")
    local gears = require("gears")
    
    -- Define modkey
    local modkey = "Mod1"  -- Alt key
    
    -- Cycle through windows with Alt+Tab
    awful.keygrabber {
        start_callback = function()
            -- Start cycling through clients
            local clients = awful.client.visible()
            if #clients > 1 then
                local current_index = gears.table.hasitem(clients, client.focus)
                local next_index = (current_index % #clients) + 1
                clients[next_index]:emit_signal("request::activate", "key.ungrab", {raise = true})
            end
        end,
        stop_callback = function()
            -- Stop cycling
        end,
        export_keybindings = true,
        stop_event = "release",
        stop_key = "Tab",
        keybindings = {
            {{modkey}, "Tab", function() end},
        }
    }
    
    -- Hotkeys to switch to workspaces 1-4
    for i = 1, 4 do
        awful.keyboard.append_global_keybindings({
            awful.key({ modkey }, "#" .. i + 9,
                function ()
                    local screen = awful.screen.focused()
                    local tag = screen.tags[i]
                    if tag then
                        tag:view_only()
                    end
                end,
                {description = "view tag #"..i, group = "tag"}
            ),
        })
    end
    
    -- Hotkeys to move focused window to workspaces 1-4
    for i = 1, 4 do
        awful.keyboard.append_global_keybindings({
            awful.key({ modkey, "Shift" }, "#" .. i + 9,
                function ()
                    if client.focus then
                        local tag = client.focus.screen.tags[i]
                        if tag then
                            client.focus:move_to_tag(tag)
                        end
                    end
                end,
                {description = "move focused client to tag #"..i, group = "tag"}
            ),
        })
    end
    
    -- Make windows draggable
    client.connect_signal("manage", function (c)
        c:connect_signal("request::activate", function()
            c:emit_signal("request::activate", "mouse_click", {raise = true})
        end)
    
        c:connect_signal("mouse::press", function(c, _, _, button)
            if button == 1 then
                c:emit_signal("request::activate", "mouse_click", {raise = true})
                awful.mouse.client.move(c)
            end
        end)
    end)
    

    Explanation:

    1. Cycle Through Windows with Alt+Tab: The awful.keygrabber is used to capture the Alt+Tab key combination. When Alt+Tab is pressed, it cycles through the visible clients (windows) on the current screen. Releasing the Tab key stops the cycling.

    2. Switch to Workspaces 1-4: The for loop binds the Alt key combined with the number keys 1 to 4 to switch to the corresponding workspaces (tags). The "#" .. i + 9 expression maps the number keys to their keycodes.

    3. Move Focused Window to Workspaces 1-4: Another for loop binds the Alt+Shift key combination with the number keys 1 to 4 to move the currently focused window to the corresponding workspace.

    4. Make Windows Draggable: The client.connect_signal function connects to the manage signal, which is emitted when a new client appears. It then connects to the mouse::press signal to allow dragging windows by holding the left mouse button.

    Note: Ensure that the awful and gears modules are required at the beginning of your rc.lua file. Also, make sure that the modkey is set to "Mod1" to correspond to the Alt key. Adjust the keybindings as needed to fit your preferences.

    After adding this code to your rc.lua file, reload AwesomeWM to apply the changes.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.