Skip to content

Instantly share code, notes, and snippets.

@BrodieRobertson
Forked from jpentland/tabc.sh
Last active August 14, 2022 23:40
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save BrodieRobertson/dd7e3d793a501066c50086ed795c0357 to your computer and use it in GitHub Desktop.
Save BrodieRobertson/dd7e3d793a501066c50086ed795c0357 to your computer and use it in GitHub Desktop.
Add or remove windows from suckless' tabbed
#!/bin/sh
# Usage:
# tabc.sh <command>
# Commands:
# add <direction-of-tabbed> <window-id> - Add window to tabbed
# remove <window-id> - Remove window from tabbed
# list <tabbed-id> - List all clients of tabbed
#
# Functions
#
# Get wid of root window
function get_root_wid {
xwininfo -root | awk '/Window id:/{print $4}'
}
# Get children of tabbed
function get_clients {
id=$1
xwininfo -id $id -children | sed -n '/[0-9]\+ \(child\|children\):/,$s/ \+\(0x[0-9a-z]\+\).*/\1/p'
}
# Get class of a wid
function get_class {
id=$1
if [ -z $id ]; then
echo ""
else
xprop -id $id | sed -n '/WM_CLASS/s/.*, "\(.*\)"/\1/p'
fi
}
#
# Main Program
#
cmd=$1
if [ $cmd = "add" ]; then
tabbedid=$(bspc query -N -n $2)
if [ -z $tabbedid ]; then
tabbed &
sleep 0.1
tabbedid=$(xdotool search --class tabbed | tail -n1)
fi
fi
case $cmd in
add)
wid=$3
xdotool windowreparent $wid $tabbed
;;
remove)
wid=$2
tabbedid=$(bspc query -N -n focused)
xdotool windowreparent $wid $(get_root_wid)
;;
list)
tabbedid=$2
get_clients $tabbed
;;
esac
@bruce-hill
Copy link

Instead of tabbed &; sleep 0.1; tabbedid=$(xdotool search --class tabbed | tail -n1), you can just do tabbed &; tabbedid=$! ($! is the process ID of the last run process)

@BrodieRobertson
Copy link
Author

BrodieRobertson commented Feb 22, 2020

Thank you that seems like exactly what I was looking for

Edit: I just tested this and it works assuming that the window is already open it works but if it hasn't it still puts more in a situation where I need to sleep until the window has opened.

Edit Edit: Actually I think I was just doing something else wrong

@tanshunyuan
Copy link

Hey Brodie,

I watched your video on adding tabbed to bspwm and I was following your gist but I found some errors in your current gist. I had to refer back to the gist that was in the video which seemed accurate.

I think the following line is supposed to be at line 58 which is under the remove block,
if [ -z $(tabc list $tabbedid) ]; then xdotool windowkill $tabbedid fi

I found that a variable is misspelled, xdotool windowreparent $wid $tabbed is supposed to be xdotool windowreparent $wid $tabbedid
Adding a focused window to west/east or left/right is not working is due to the target window not being a instance of tabbed.

@stefanszymanski
Copy link

Some hints:

  • There's no need to sleep after calling tabbed, calling tabbed -d will return the wid of the created window (also I believe xdos -m parameter causes it to wait until there's a window that matches). Nonetheless I don't get what you need $tabbedid for, because you never use it?!
  • Using the syntax name() { offers way better compability with older shells than function name {. Especially because you specified /bin/sh as interpreter, your script may not run in other people's environments.
  • Quoting variables prevents globbing and word splitting. I recommend shellcheck for linting your scripts :)

@guidiamond
Copy link

Instead of tabbed &; sleep 0.1; tabbedid=$(xdotool search --class tabbed | tail -n1), you can just do tabbed &; tabbedid=$! ($! is the process ID of the last run process)

You can actually just change tabbed to tabbed -c in the add cmd, which is a builtin param for tabbed that auto closes when the last tab is closed.

@ivanmilov
Copy link

Hello!
To get win id without sleep I use:

tabbedid=$(tabbed -c -d)

-d detaches tabbed from the terminal and prints its XID to stdout.

@plmi
Copy link

plmi commented Feb 12, 2022

Here is a solution to retrieve the window id without using sleep. Hope it helps: plmi/dotfiles@71d1fd5#diff-126a1e1993e27b2e10ae75f52c689bcacc01f9dc135d345f4f910975126e43d1

@etherbiswas
Copy link

@BrodieRobertson i would love to have the scirpt working again! Can you please help?

@etherbiswas
Copy link

etherbiswas commented Mar 20, 2022

very annoying error! i think it has someting to do with being posix compliant.

.config/bspwm/tabc.sh: 15: function: not found
0x6bf
.config/bspwm/tabc.sh: 17: Syntax error: "}" unexpected

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment