Raveen Kumar

How to create and move windows and containers to workspaces on the fly in i3wm

I do not know why i3wm does not have this key-bind by default. But this piece of code solves it.

This is the task that i want the i3wm to do,

  1. Move window to any existing workspace
  2. If the input workspace does not exist create a new one and move the window to it.
    set $custom_command [r]ename workspace [m]ove to workspace [c]reate workspace [q]uit
    bindsym $mod+c mode "$custom_command"
    
    mode "$custom_command" {
        bindsym r exec i3-input -F 'rename workspace to "%s"' -P 'New name: '; mode "default"
        bindsym m exec "bash ~/.scripts/move_window_to_existing_workspace.bash"
        bindsym c exec i3-input -F 'workspace "%s"' -P 'New Workspace name: '; mode "default"
    
        bindsym q mode "default"
    }

~/.scripts/move_window_to_existing_workspace.bash file contents

#!/bin/bash
workspace=$(i3-msg -t get_workspaces | tr "," "\n" | grep name | cut -d ":" -f 2 | sed "s/\"//g" | dmenu)
i3-msg "move window to workspace \"$workspace\"; mode \"default\""

Add this code to i3 config.

Now when I press Super+c m, dmenu shows up with existing workspace and you can use arrow keys to select, and the window will be moved to it. Alternatively you can type a new workspace name and the window will be moved to that workspace, Super+c m new_workspace_name.

You can also use,

i3-msg "move window to workspace \"$workspace\"; mode \"default\"; workspace \"$workspace\""

in line 4 of the ~/.scripts/move_window_to_existing_workspace.bash to move to that workspace immediately after moving the window.

#I3wm #Linux #Desktop-Environment