You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

i3窗口管理器:如何实现基于方向的窗口调整而非固定缩放?

可以在i3中实现类似tmux的方向式窗口调整

i3原生的resize命令不支持根据窗口位置动态调整行为,但可以通过编写脚本结合i3的IPC接口实现这个逻辑。核心思路是:通过脚本获取当前窗口的父容器布局、窗口在容器中的位置,判断后执行对应的resize操作。

步骤1:安装依赖

需要jq工具解析i3输出的JSON格式窗口树,Debian/Ubuntu系系统可执行:

sudo apt install jq

步骤2:编写方向调整脚本

创建~/.config/i3/resize_directional.sh,内容如下:

#!/bin/bash

DELTA="2"  # 调整幅度,和你配置里的$resize_delta保持一致

# 获取当前聚焦窗口及父容器信息
current_win=$(i3-msg -t get_tree | jq '.. | select(.focused == true)')
parent=$(echo "$current_win" | jq '.. | select(.type == "con" and .nodes[]?.id == ('"$(echo "$current_win" | jq '.id')"'))')

# 提取父容器布局和当前窗口在子节点中的索引
layout=$(echo "$parent" | jq -r '.layout')
win_index=$(echo "$parent" | jq '.nodes | index(.[] | select(.id == ('"$(echo "$current_win" | jq '.id')"')))')

case $1 in
    left)
        if [ "$layout" = "splith" ]; then
            [ "$win_index" -eq 0 ] && i3-msg "resize shrink width $DELTA px or $DELTA ppt" || i3-msg "resize grow width $DELTA px or $DELTA ppt"
        fi
        ;;
    right)
        if [ "$layout" = "splith" ]; then
            [ "$win_index" -eq 0 ] && i3-msg "resize grow width $DELTA px or $DELTA ppt" || i3-msg "resize shrink width $DELTA px or $DELTA ppt"
        fi
        ;;
    up)
        if [ "$layout" = "splitv" ]; then
            [ "$win_index" -eq 0 ] && i3-msg "resize shrink height $DELTA px or $DELTA ppt" || i3-msg "resize grow height $DELTA px or $DELTA ppt"
        fi
        ;;
    down)
        if [ "$layout" = "splitv" ]; then
            [ "$win_index" -eq 0 ] && i3-msg "resize grow height $DELTA px or $DELTA ppt" || i3-msg "resize shrink height $DELTA px or $DELTA ppt"
        fi
        ;;
esac

给脚本添加执行权限:

chmod +x ~/.config/i3/resize_directional.sh

步骤3:修改i3配置

替换原resize模式中的绑定规则,改为调用脚本:

set $resize_delta 2

mode "resize" {
        # 字母键方向调整
        bindsym h exec --no-startup-id ~/.config/i3/resize_directional.sh left
        bindsym j exec --no-startup-id ~/.config/i3/resize_directional.sh down
        bindsym k exec --no-startup-id ~/.config/i3/resize_directional.sh up
        bindsym l exec --no-startup-id ~/.config/i3/resize_directional.sh right

        # 箭头键对应逻辑
        bindsym Left exec --no-startup-id ~/.config/i3/resize_directional.sh left
        bindsym Down exec --no-startup-id ~/.config/i3/resize_directional.sh down
        bindsym Up exec --no-startup-id ~/.config/i3/resize_directional.sh up
        bindsym Right exec --no-startup-id ~/.config/i3/resize_directional.sh right

        # 退出resize模式
        bindsym Return mode "default"
        bindsym Escape mode "default"
        bindsym $mod+r mode "default"
}

说明

  • 脚本仅处理水平/垂直分割的容器(splith/splitv),浮动窗口、标签页(tabbed)等布局下会保持i3原生行为。
  • 可直接修改脚本中的DELTA值,或同步调整i3配置里的$resize_delta,保持幅度一致。

内容的提问来源于stack exchange,提问作者Ilya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 21:53:18