master ohmu/wmutils / scripts / closest.sh
ohmu  ·  2026-07-25
 1#!/bin/sh
 2#
 3# z3bra - 2014 (c) wtfpl
 4# find and focus the closest window in a specific direction
 5# depends on: focus.sh
 6
 7# get current window id
 8CUR=$(pfw)
 9
10usage() {
11    echo "usage: $(basename $0) <direction>" >&2
12    exit 1
13}
14
15next_west() {
16    lsw | xargs wattr xi | sort -nr | sed "0,/$CUR/d" | sed "1s/^[0-9]* //p;d"
17}
18
19next_east() {
20    lsw | xargs wattr xi | sort -n | sed "0,/$CUR/d" | sed "1s/^[0-9]* //p;d"
21}
22
23next_north() {
24    lsw | xargs wattr yi | sort -nr | sed "0,/$CUR/d" | sed "1s/^[0-9]* //p;d"
25}
26
27next_south() {
28    lsw | xargs wattr yi | sort -n | sed "0,/$CUR/d" | sed "1s/^[0-9]* //p;d"
29}
30
31# Use the specification of your choice: WASD, HJKL, ←↑↓→, west/north/south/east
32case $1 in
33    h|a|west|left)  wid=$(next_west)  ;;
34    j|s|south|down) wid=$(next_south) ;;
35    k|w|north|up)   wid=$(next_north) ;;
36    l|d|east|right) wid=$(next_east)  ;;
37    *)              usage             ;;
38esac
39
40test ! -z "$wid" && /home/$USER/.wmutils/scripts/focus.sh "$wid"