Search code examples
workspacei3

How can I open a work space from the command line in i3?


I'd like to script opening a workspace with the 'next' available number and then have open in that workspace maybe a two windows each already pointed at a specific view. Any pointers on how to do that? Is i3 actually scriptable in this regard? I have only just started using i3 and loving it, just want to now have it do things I need on a regular basis ;-)


Solution

  • All of i3's "scripting" that isn't already in the config is done through i3-msg

    i3-msg sends messages to i3 window manager. It's mostly config lines(or IPC, which is a bit harder).

    How can I open a work space from the command line in i3

    How do you open it in your config?

    bindsym Mod4+1 workspace $ws1

    Something like that right? Now, using i3-msg to move to workspace 1:

    i3-msg workspace 1

    Simple, right?

    The rest of your question is pretty unclear, but I'll do my best:

    I'd like to script opening a workspace with the "next" available number"

    How you'll start out with doing that is(after doing man i3-msg):

    i3-msg -t get_workspaces

    This returns a json(if you've done any coding you'll feel warm inside and if not you're going to be scared by the output). Then I'm going to ask you to read up on this:

    https://i3wm.org/docs/ipc.html#_receiving_replies_from_i3

    Using jq or something like json.sh to parse through the output with bash, you should get which workspaces are "active", example:

    [{"num":3,"name":"3","visible":true,"focused":false,"rect":{"x":0,"y":0,"width":1920,"height":1080},"output":"HDMI-1","urgent":false},{"num":2,"name":"2","visible":true,"focused":false,"rect":{"x":3520,"y":0,"width":1920,"height":1080},"output":"VGA-1","urgent":false},{"num":1,"name":"1","visible":true,"focused":true,"rect":{"x":1920,"y":32,"width":1600,"height":868},"output":"eDP-1","urgent":false},{"num":5,"name":"5","visible":false,"focused":false,"rect":{"x":1920,"y":32,"width":1600,"height":868},"output":"eDP-1","urgent":false}]
    

    Reading through the website I gave you, you can see, workspaces 1,2,3 and 5 are "active" aka have windows in them. After parsing the actual json you'll end up with an array or something of this. If you want to spawn on 4(the real "next" desktop) or 6 (aka biggest number + 1) is up to you. You didn't mention what you want to script it in, so I'll leave that to you to figure out.

    maybe two windows each already pointed at a specific view

    I'm not sure what you mean. You probably want 2 windows of X(we'll use a terminal in this case) in the workspace we've determined as "next" ?

    Let's go back to i3-msg for a bit.

    Imagine you doing whatever you're trying to do, manually, as a list of commands you're firing to i3.

    "Go to workspace X" : i3-msg workspace 4

    "Spawn a program called kitty there": kitty

    "Split vertically and spawn another program": i3-msg split v

    "Spawn another kitty window": kitty

    After that you can get back to your current workspace(I suggest saving it in a variable and just reusing i3-msg workspace $curr_workspace).

    As I said, the question wasn't about the actual scripting so I left that out to figure it out on your own, but don't hesitate to ask a concrete question under the bash tag. :). Hopefully I didn't completely misunderstand your question.

    Welcome to the i3 community.