i3window manager 俺々式 config 法

数あるウィンドウマネージャの中でも,特に使いやすい(と私が感じている)タイル型ウィンドウマネージャが i3 window manager.一部の人たちの間では arch , vim と併せてオタク 3 点セットなどと言われています(ソース).i3 はその無駄のなさ,スタイルの良さの他に,設定ファイルが細かく書けるのも魅力の 1 つだと思います.そこで今回は, i3 がもっと使いやすくなる(と私が考える)設定ファイルの書き方を紹介します.

ウィンドウの境界

default_border pixel 1

これは私の好みなのですが,後で focus と組み合わせることで効果を発揮します.

モードの活用

i3 には,モードという概念が存在します.例えばテンプレート設定(/etc/i3/config)では,ウィンドウの大きさを変更する resize モードが用意されています.簡単に説明すると次のような感じです.

bindsym $mod+r mode "resize"
# $mod+r を押すことで resize モードに入ることができる

mode "resize" {
    bindsym $left resize shrink width 10 px
    # この中括弧内の bindsym / bindcode は, resize モードに入ったときだけ有効になる
    
    bindsym Return mode "default"
    # Returnキーを押すことで default モードに戻ることができる
}

# 中括弧の外側は default モード

この設定のもとでウィンドウの幅を狭めたいときは,まず $mod+r を押して resize モードに入り,左キーを何回か押して好きな幅まで狭めた後, Return キーを押して default モードに戻れば良いわけです.

これを活用し,様々な操作を種類ごとにまとめてモードの中に入れてしまおうと思います.

exit モード

exit / restart / reload の 3 つのコマンドを exit モードに入れます.

bindsym $mod+Shift+e mode "exit"
mode "exit" {
    bindsym Return mode "default"
    bindsym e exit
    bindsym r restart
    bindsym w reload
}

exit 時に終了したいプロセスがあれば, exit 時に kill してしまってもよいでしょう.

    # bindsym e exit の代わりに: 
    bindsym e exec killall mozc_server; exit
    # mozc_server を終了し, exit する

また,どんなモードを定義するときも,必ず default モードに戻る手段を書くのを忘れないようにしましょう.下手するとターミナルやランチャーすら起動できなくなって死にます.

layout モード

既に画面上に並んでいるウィンドウのレイアウトを変更するコマンドを layout モードに入れます.

bindsym $mod+l mode "layout"
mode "layout" {
    bindsym Return mode "default"
    bindsym h layout splith; mode "default"
    bindsym v layout splitv; mode "default"
    bindsym t layout tabbed; mode "default"
    bindsym s layout stacking; mode "default"
    bindsym f floating toggle; mode "default"
}

layout モードのまま何回もレイアウトを変更することはあまり無いと思うので,一度操作したら自動的に default モードに戻るようにしています.

split モード

現在のウィンドウを分割して新しいウィンドウを作るコマンドを split モードに入れます.

bindsym $mod+s mode "split"
mode "split" {
    bindsym Return mode "default"
    bindsym h split horizontal; mode "default"
    bindsym v split vertical; mode "default"
}

focus モード

フォーカスしているウィンドウを変更するコマンドを focus モードに入れます.

bindsym $mod+f border normal; mode "focus"
mode "focus" {
    bindsym Return border pixel 1; mode "default"
    bindsym h border pixel 1; focus left; border normal
    bindsym j border pixel 1; focus down; border normal
    bindsym k border pixel 1; focus up; border normal
    bindsym l border pixel 1; focus right; border normal
    bindsym p border pixel 1; focus parent
    bindsym c focus child; border normal
    bindsym f border pixel 1; focus mode_toggle; border normal
}

上下左右キーではなく vim-like な h/j/k/l を使っているのは個人的な趣味です.また, focus モードでは,フォーカスされているウィンドウの境界が pixel ではなく normal,すなわちウィンドウタイトルが表示される状態になるようにしています.

move モード

ウィンドウを移動するコマンドを move モードに入れます.

bindsym $mod+m border normal; mode "move"
mode "move" {
    bindsym Return border pixel 1; mode "default"
    bindsym h move left
    bindsym j move down
    bindsym k move up
    bindsym l move right
    bindsym c move absolute position center
    bindsym m move position mouse
}

resize モード

ウィンドウの大きさを変更するコマンドを resize モードに入れます.

bindsym $mod+r border normal; mode "resize"
mode "resize" {
    bindsym Return border pixel 1; mode "default"
    bindsym h resize shrink width
    bindsym Shift+h resize shrink width 50 px
    bindsym j resize grow height
    bindsym Shift+j resize grow height 50 px
    bindsym k resize shrink height
    bindsym Shift+k resize shrink height 50 px
    bindsym l resize grow width
    bindsym Shift+l resize grow width 50 px
}

workspace モード

ワークスペースは, 1 つのディスプレイに複数の仮想デスクトップを作ることができる便利な機能です.たくさんある方が嬉しいので, workspace モード内でたくさん作っちゃいましょう.

bindsym $mod+w mode "workspace"
mode "workspace" {
    bindsym Return mode "default"
    bindsym 0 workspace 0; mode "default"
    bindsym Shift+0 move container to workspace 0; workspace 0; mode "default"
    bindsym 1 workspace 1; mode "default"
    bindsym Shift+1 move container to workspace 1; workspace 1; mode "default"
    ……
    bindsym 9 workspace 9; mode "default"
    bindsym Shift+9 move container to workspace 9; workspace 9; mode "default"
    bindsym A workspace A; mode "default"
    bindsym Shift+A move container to workspace A; workspace A; mode "default"
    bindsym B workspace B; mode "default"
    bindsym Shift+B move container to workspace B; workspace B; mode "default"
    ……
    bindsym Z workspace Z; mode "default"
    bindsym Shift+Z move container to workspace Z; workspace Z; mode "default"
}

これで 0〜9,A〜Z あわせて 36 個のワークスペースを作ることができました.

その他のコマンド

いくつかのコマンドは, default モードで使えるようにしておきます.

bindsym $mod+Return exec terminator
bindsym $mod+Shift+space exec rofi -show run
bindsym $mod+Delete kill
bindsym $mod+Shift+f fullscreen toggle

モード同士の切り替え

focus モードから resize モードに切り替えたいときもあるでしょう.そのときにいちいち default モードに戻らずに済むようにします.

mode "focus" {
    ……
    # 追記:
    bindsym $mod+Shift+e border pixel 1; mode "exit"
    bindsym $mod+l border pixel 1; mode "layout"
    bindsym $mod+s border pixel 1; mode "split"
    bindsym $mod+m mode "move"
    bindsym $mod+r mode "resize"
    bindsym $mod+w border pixel 1; mode "workspace"
}

他のモードについても同様にこれを書きます.

説明文

コマンドが多すぎて覚えられないので,どこかに説明文を表示したいです. i3bar には現在のモードが表示されるようになっているので,ここを変えればよさそうです.

# mode "layout" { の代わりに: 
mode "layout [h: splith] [v: splitv] [t: tabbed] [s: stacking] [f: floating toggle]" {
    ……
}

まとめ

以上を全てまとめると次のような config になります.

bar{
	status_command i3status
}
default_border pixel 1
bindsym Mod4+Return exec terminator
bindsym Mod4+Shift+space exec rofi -show run
bindsym Mod4+Delete kill
bindsym Mod4+Shift+f fullscreen toggle
bindsym Mod4+Shift+e mode "exit [e: exit] [r: restart] [w: reload]"
bindsym Mod4+l mode "layout [h: splith] [v: splitv] [t: tabbed] [s: stacking] [f: floating toggle]"
bindsym Mod4+s mode "split [h: horizontal] [v: vertical]"
bindsym Mod4+f border normal; mode "focus [h: left] [j: down] [k: up] [l: right] [p: parent] [c: child] [f: floating]"
bindsym Mod4+m border normal; mode "move [h: left] [j: down] [k: up] [l: right] [c: center] [m: mouse]"
bindsym Mod4+r border normal; mode "resize [h: shrink width] [j: grow height] [k: shrink height] [l: grow width]"
bindsym Mod4+w mode "workspace"
mode "exit [e: exit] [r: restart] [w: reload]"{
	bindsym Return mode "default"
	bindsym e exec killall mozc_server; exit
	bindsym r restart
	bindsym w reload
	bindsym Mod4+l mode "layout [h: splith] [v: splitv] [t: tabbed] [s: stacking] [f: floating toggle]"
	bindsym Mod4+s mode "split [h: horizontal] [v: vertical]"
	bindsym Mod4+f border normal; mode "focus [h: left] [j: down] [k: up] [l: right] [p: parent] [c: child] [f: floating]"
	bindsym Mod4+m border normal; mode "move [h: left] [j: down] [k: up] [l: right] [c: center] [m: mouse]"
	bindsym Mod4+r border normal; mode "resize [h: shrink width] [j: grow height] [k: shrink height] [l: grow width]"
	bindsym Mod4+w mode "workspace"
}
mode "layout [h: splith] [v: splitv] [t: tabbed] [s: stacking] [f: floating toggle]"{
	bindsym Return mode "default"
	bindsym h layout splith; mode "default"
	bindsym v layout splitv; mode "default"
	bindsym t layout tabbed; mode "default"
	bindsym s layout stacking; mode "default"
	bindsym f floating toggle; mode "default"
	bindsym Mod4+Shift+e mode "exit [e: exit] [r: restart] [w: reload]"
	bindsym Mod4+s mode "split [h: horizontal] [v: vertical]"
	bindsym Mod4+f border normal; mode "focus [h: left] [j: down] [k: up] [l: right] [p: parent] [c: child] [f: floating]"
	bindsym Mod4+m border normal; mode "move [h: left] [j: down] [k: up] [l: right] [c: center] [m: mouse]"
	bindsym Mod4+r border normal; mode "resize [h: shrink width] [j: grow height] [k: shrink height] [l: grow width]"
	bindsym Mod4+w mode "workspace"
}
mode "split [h: horizontal] [v: vertical]"{
	bindsym Return mode "default"
	bindsym h split horizontal; mode "default"
	bindsym v split vertical; mode "default"
	bindsym Mod4+Shift+e mode "exit [e: exit] [r: restart] [w: reload]"
	bindsym Mod4+l mode "layout [h: splith] [v: splitv] [t: tabbed] [s: stacking] [f: floating toggle]"
	bindsym Mod4+f border normal; mode "focus [h: left] [j: down] [k: up] [l: right] [p: parent] [c: child] [f: floating]"
	bindsym Mod4+m border normal; mode "move [h: left] [j: down] [k: up] [l: right] [c: center] [m: mouse]"
	bindsym Mod4+r border normal; mode "resize [h: shrink width] [j: grow height] [k: shrink height] [l: grow width]"
	bindsym Mod4+w mode "workspace"
}
mode "focus [h: left] [j: down] [k: up] [l: right] [p: parent] [c: child] [f: floating]"{
	bindsym Return border pixel 1; mode "default"
	bindsym h border pixel 1; focus left; border normal
	bindsym j border pixel 1; focus down; border normal
	bindsym k border pixel 1; focus up; border normal
	bindsym l border pixel 1; focus right; border normal
	bindsym p border pixel 1; focus parent
	bindsym c focus child; border normal
	bindsym f border pixel 1; focus mode_toggle; border normal
	bindsym Mod4+Shift+e border pixel 1; mode "exit [e: exit] [r: restart] [w: reload]"
	bindsym Mod4+l border pixel 1; mode "layout [h: splith] [v: splitv] [t: tabbed] [s: stacking] [f: floating toggle]"
	bindsym Mod4+s border pixel 1; mode "split [h: horizontal] [v: vertical]"
	bindsym Mod4+m mode "move [h: left] [j: down] [k: up] [l: right] [c: center] [m: mouse]"
	bindsym Mod4+r mode "resize [h: shrink width] [j: grow height] [k: shrink height] [l: grow width]"
	bindsym Mod4+w border pixel 1; mode "workspace"
}
mode "move [h: left] [j: down] [k: up] [l: right] [c: center] [m: mouse]"{
	bindsym Return border pixel 1; mode "default"
	bindsym h move left
	bindsym Shift+h move left 50 px
	bindsym j move down
	bindsym Shift+j move down 50 px
	bindsym k move up
	bindsym Shift+k move up 50 px
	bindsym l move right
	bindsym Shift+l move right 50 px
	bindsym c move absolute position center
	bindsym m move position mouse
	bindsym Mod4+Shift+e border pixel 1; mode "exit [e: exit] [r: restart] [w: reload]"
	bindsym Mod4+l border pixel 1; mode "layout [h: splith] [v: splitv] [t: tabbed] [s: stacking] [f: floating toggle]"
	bindsym Mod4+s border pixel 1; mode "split [h: horizontal] [v: vertical]"
	bindsym Mod4+f mode "focus [h: left] [j: down] [k: up] [l: right] [p: parent] [c: child] [f: floating]"
	bindsym Mod4+r mode "resize [h: shrink width] [j: grow height] [k: shrink height] [l: grow width]"
	bindsym Mod4+w border pixel 1; mode "workspace"
}
mode "resize [h: shrink width] [j: grow height] [k: shrink height] [l: grow width]"{
	bindsym Return border pixel 1; mode "default"
	bindsym h resize shrink width
	bindsym Shift+h resize shrink width 50 px
	bindsym j resize grow height
	bindsym Shift+j resize grow height 50 px
	bindsym k resize shrink height
	bindsym Shift+k resize shrink height 50 px
	bindsym l resize grow width
	bindsym Shift+l resize grow width 50 px
	bindsym Mod4+Shift+e border pixel 1; mode "exit [e: exit] [r: restart] [w: reload]"
	bindsym Mod4+l border pixel 1; mode "layout [h: splith] [v: splitv] [t: tabbed] [s: stacking] [f: floating toggle]"
	bindsym Mod4+s border pixel 1; mode "split [h: horizontal] [v: vertical]"
	bindsym Mod4+f mode "focus [h: left] [j: down] [k: up] [l: right] [p: parent] [c: child] [f: floating]"
	bindsym Mod4+m mode "move [h: left] [j: down] [k: up] [l: right] [c: center] [m: mouse]"
	bindsym Mod4+w border pixel 1; mode "workspace"
}
mode "workspace"{
	bindsym Return mode "default"
	bindsym 0 workspace 0; mode "default"
	bindsym Shift+0 move container to workspace 0; workspace 0; mode "default"
	bindsym 1 workspace 1; mode "default"
	bindsym Shift+1 move container to workspace 1; workspace 1; mode "default"
	bindsym 2 workspace 2; mode "default"
	bindsym Shift+2 move container to workspace 2; workspace 2; mode "default"
	bindsym 3 workspace 3; mode "default"
	bindsym Shift+3 move container to workspace 3; workspace 3; mode "default"
	bindsym 4 workspace 4; mode "default"
	bindsym Shift+4 move container to workspace 4; workspace 4; mode "default"
	bindsym 5 workspace 5; mode "default"
	bindsym Shift+5 move container to workspace 5; workspace 5; mode "default"
	bindsym 6 workspace 6; mode "default"
	bindsym Shift+6 move container to workspace 6; workspace 6; mode "default"
	bindsym 7 workspace 7; mode "default"
	bindsym Shift+7 move container to workspace 7; workspace 7; mode "default"
	bindsym 8 workspace 8; mode "default"
	bindsym Shift+8 move container to workspace 8; workspace 8; mode "default"
	bindsym 9 workspace 9; mode "default"
	bindsym Shift+9 move container to workspace 9; workspace 9; mode "default"
	bindsym A workspace A; mode "default"
	bindsym Shift+A move container to workspace A; workspace A; mode "default"
	bindsym B workspace B; mode "default"
	bindsym Shift+B move container to workspace B; workspace B; mode "default"
	bindsym C workspace C; mode "default"
	bindsym Shift+C move container to workspace C; workspace C; mode "default"
	bindsym D workspace D; mode "default"
	bindsym Shift+D move container to workspace D; workspace D; mode "default"
	bindsym E workspace E; mode "default"
	bindsym Shift+E move container to workspace E; workspace E; mode "default"
	bindsym F workspace F; mode "default"
	bindsym Shift+F move container to workspace F; workspace F; mode "default"
	bindsym G workspace G; mode "default"
	bindsym Shift+G move container to workspace G; workspace G; mode "default"
	bindsym H workspace H; mode "default"
	bindsym Shift+H move container to workspace H; workspace H; mode "default"
	bindsym I workspace I; mode "default"
	bindsym Shift+I move container to workspace I; workspace I; mode "default"
	bindsym J workspace J; mode "default"
	bindsym Shift+J move container to workspace J; workspace J; mode "default"
	bindsym K workspace K; mode "default"
	bindsym Shift+K move container to workspace K; workspace K; mode "default"
	bindsym L workspace L; mode "default"
	bindsym Shift+L move container to workspace L; workspace L; mode "default"
	bindsym M workspace M; mode "default"
	bindsym Shift+M move container to workspace M; workspace M; mode "default"
	bindsym N workspace N; mode "default"
	bindsym Shift+N move container to workspace N; workspace N; mode "default"
	bindsym O workspace O; mode "default"
	bindsym Shift+O move container to workspace O; workspace O; mode "default"
	bindsym P workspace P; mode "default"
	bindsym Shift+P move container to workspace P; workspace P; mode "default"
	bindsym Q workspace Q; mode "default"
	bindsym Shift+Q move container to workspace Q; workspace Q; mode "default"
	bindsym R workspace R; mode "default"
	bindsym Shift+R move container to workspace R; workspace R; mode "default"
	bindsym S workspace S; mode "default"
	bindsym Shift+S move container to workspace S; workspace S; mode "default"
	bindsym T workspace T; mode "default"
	bindsym Shift+T move container to workspace T; workspace T; mode "default"
	bindsym U workspace U; mode "default"
	bindsym Shift+U move container to workspace U; workspace U; mode "default"
	bindsym V workspace V; mode "default"
	bindsym Shift+V move container to workspace V; workspace V; mode "default"
	bindsym W workspace W; mode "default"
	bindsym Shift+W move container to workspace W; workspace W; mode "default"
	bindsym X workspace X; mode "default"
	bindsym Shift+X move container to workspace X; workspace X; mode "default"
	bindsym Y workspace Y; mode "default"
	bindsym Shift+Y move container to workspace Y; workspace Y; mode "default"
	bindsym Z workspace Z; mode "default"
	bindsym Shift+Z move container to workspace Z; workspace Z; mode "default"
	bindsym Mod4+Shift+e mode "exit [e: exit] [r: restart] [w: reload]"
	bindsym Mod4+l mode "layout [h: splith] [v: splitv] [t: tabbed] [s: stacking] [f: floating toggle]"
	bindsym Mod4+s mode "split [h: horizontal] [v: vertical]"
	bindsym Mod4+f border normal; mode "focus [h: left] [j: down] [k: up] [l: right] [p: parent] [c: child] [f: floating]"
	bindsym Mod4+m border normal; mode "move [h: left] [j: down] [k: up] [l: right] [c: center] [m: mouse]"
	bindsym Mod4+r border normal; mode "resize [h: shrink width] [j: grow height] [k: shrink height] [l: grow width]"
}

これで設定は完了です. i3 がとても使いやすくなります.
これだけの設定を手書きするのはとても面倒なので,適当にプログラムでも走らせるといいでしょう.

github にリンクを載せておきます.これの config.c を適当に自分好みに書き換えてから make を走らせれば,あなただけの設定ファイルが作れます
github.com



この記事があなたの役に立てば幸いです.