7

Is there a way to copy a window to more than one workspace. E.g. if chrome is open on the desktop of Workspace 1, instead of moving it, I would like to copy it to Workspace X, such that, when I switch to Workspace X, chrome window is present there as well.

Suggestions don't need to be limited to OS only solutions. If someone can suggest an easily acquirable free program, that would really help as well.

Current OS Version: Ubuntu 14.04-amd64

  • 4
    if you'd accept a solution for Unity specifically, then right click on window title bar and select "Always on visible workspace" option, the window will be present no matter what workspace you switch to. There's also a scripting solution that will work on any desktop environment with xdotool. Let me know if you want me to post either of these as an answer – Sergiy Kolodyazhnyy Jun 09 '16 at 22:31
  • Though it does help but it's not exactly what I was after. 'Always on visible workspace' does exactly what it sounds like, the window is always visible on every workspace. However, what if I make a window visible on Workspace 1 and 2. – fancyPants Jun 14 '16 at 15:44
  • Ah, in that case i may have a scripting idea. This question seems to have been asked before but there is no solution for that one. I will begin working on the script and will report as soon as i have it working – Sergiy Kolodyazhnyy Jun 14 '16 at 15:49
  • Much appreciated – fancyPants Jun 20 '16 at 17:48
  • 1
    @nealmcb a possible solution quite depends on the window manager. Might be worth mentioning what the solution should work on. Mutter? Unity? Wayland? – Jacob Vlijm Dec 29 '19 at 09:29
  • @JacobVlijm Yes indeed. As stated, I'm targeting "modern Ubuntu, i.e. Gnome on Bionic”, but the latest KDE would also be of interest. Seems like Sergly might have a script for something also. – nealmcb Dec 29 '19 at 18:55

2 Answers2

5

Kubuntu:

showing a window on multiple destops works out of the box. Just right click on the app icon in the task bar

screenshot

select "Move to Desktop" >> "All Desktops"

This way the window is displayed on every "Desktop" (the wording "move" in the context menu is a bit misleading).

Gnome

As it turns out this works out of the box as well

Right click on the Window Title and select "Always on visible Workspace"

enter image description here

guntbert
  • 13,134
  • 2
    Question is to have the window appear automatically on specific desktops. OP already knows how to move a window to another desktop. – vanadium Dec 31 '19 at 11:28
  • 1
    @vanadium FWIW as the bounty maker, I'm very happy to know how to send it to all desktops. But I use Gnome, and am impressed by the depth of the scripting answer, so I'll give the bounty to that one. Thanks all! – nealmcb Dec 31 '19 at 17:23
  • @nealmcb I wholeheartedly agree with that decision :+1 – guntbert Dec 31 '19 at 17:34
  • 2
    @nealmcb Thank you very much for the bounty! Btw, the option provided by guntbert also exists in GNOME. Just right click the window csd or border and click Always on visible workspace. – BeastOfCaerbannog Dec 31 '19 at 19:29
  • 2
    I just wanted to point out that I do not consider this a valid answer for the question, even if it provides useful information. – vanadium Jan 01 '20 at 15:23
  • 1
    Thanks folks. @guntbert would you be willing to add the Gnome approach to your answer? It works for me, and is a good thing to know about. – nealmcb Jan 01 '20 at 22:37
3

I created the following bash script that I think approximates what the OP wants. It doesn't copy the selected window on the selected workspaces. It rather automatically moves the selected window on the selected workspaces when the latter change.

#!/bin/bash
# get id of window
id=$(xwininfo -id $(xdotool getwindowfocus) | grep "Window id" | cut -d' ' -f4)
# get pid of script
script_pid=$(echo $$)

# file for temporarily storing id-pid pairs
temp="/tmp/ids-pids.txt"
# create the file if it does not exist
touch $temp

# check if window id already exists in temp
# if yes, kill the script and remove the window id entry
# if no, add a window id-script pid entry
# this is used as a toggle for turning the script on/off with the same keystroke
if grep -q $id $temp; then
    kill $(grep $id $temp | cut -d' ' -f2)
    sed -i "/$id/d" $temp 
    kill $$
else
    echo $id $script_pid >> $temp
fi

# enter the workspaces numbers that the window can appear on as an array
# input is space-delimited
IFS=' ' read -r -a array < <( zenity --entry \
                --width 400 \
                --title="Window on multiple workspaces" \
                --text="Window on workspaces:" )

# if the user cancels text entry or inputs nothing, then exit and kill the script
if [ ${#array[@]} -eq 0 ]; then
    sed -i "/$id/d" $temp 
    kill $$
fi

while true
do
    # get current workspace
    workspace=$(wmctrl -d | grep \* | cut -d' ' -f1)
    # move window to the workspace if it is in the allowed workspaces array
    if [[ "${array[@]}" =~ "${workspace}" ]]; then 
        wmctrl -i -r $id -t $workspace
    fi

    # check if window is closed
    # if yes, remove its id-pid entry and kill the script 
    if echo $(xwininfo -id $id 2>&1) | grep -q "X Error:"; then
        sed -i "/$id/d" $temp
        kill $(grep $id $temp | cut -d' ' -f2)
        kill $$
    fi

    # sleep is used so that the command won't run continuously
    sleep 1
done

How to use

  • First, make sure you have the programs that are used in this script installed:

    sudo apt install xdotool wmctrl zenity
    
  • Copy and paste the script in an .sh file in your computer, for example in your Home folder (/home/user/window_workspaces.sh).

  • Make it executable:

    chmod u+x /home/user/window_workspaces.sh
    
  • Assign the script to a shortcut. In GNOME, open Applications OverviewSettingsDevicesKeyboard → click the "+" at the bottom and enter a shortcut name, the path to the script and the shortcut you wish to use, as shown in the picture below. I have chosen Super+Z as the script's shortcut.

    GNOME shortcut

  • On your focused window, press Super+Z. A window will pop up where you can enter the workspaces you wish your window to appear on, separated by space, as shown below.

    Workspaces entry

  • Press Super+Z again while having the same window focused and it will return back to normal, i.e. only appear in one workspace.

The script should work in all desktop environments.

Feel free to suggest any changes!

  • 1
    Wow - that's scary, and much appreciated, if for nothing else than seeing some brave scripting going on. Can you clarify the performance impacts? It seems to run wmctrl every second - is that for each window that appears in multiple workspaces? Is there any way to wait on window manager events like changes of workspace instead, to make it both more responsive and less resource-intensive? – nealmcb Dec 31 '19 at 05:25
  • @nealmcb Hi! Indeed the script runs wmctrl every second for every window in multiple workspaces. I tried to measure the impact on the system with various ways and it seems negligible. I haven't noticed any impact on my computer's performance even with the script running for about 10 windows. However, I also would prefer the script to run only on workspace change, rather than looping every second, but I cannot find any event triggered by workspace changing. Did you use it? I have also made a change (check the updated script). I added a check to kill the script if you close the window. – BeastOfCaerbannog Dec 31 '19 at 11:18