]> git.sur5r.net Git - i3/i3.github.io/blob - docs/4.13/user-contributed/workspace-current-output.html
save docs for 4.13
[i3/i3.github.io] / docs / 4.13 / user-contributed / workspace-current-output.html
1 ---
2 layout: default
3 title: Docs
4 group: Docs
5 ---
6 <div id="content" class="usergen">
7
8 <h1>User-contributed article: Move a given workspace to active output</h1>
9
10 <h2>Introduction</h2>
11
12 <p>Assume you have multiple monitors, and various workspaces on each of them.</p>
13
14 <p>You have some workspace 1 active on monitor A, and you want to jump to
15 workspace 2. The default behavior, using the built-in command <code>workspace 2</code>
16 is to display the workspace 2 on the screen it belongs to.</p>
17
18 <p>Another legitimate use-case would be to display the workspace 2 on the
19 currently active screen.</p>
20
21 <p>Unfortunately, there is no built-in command in i3 to achieve that, yet. However,
22 thanks to the IPC interface, it is possible to write an external script
23 achieving that.</p>
24
25 <h2>Installation</h2>
26
27 <p>First, we need to create an executable script doing the job thanks to the <a href="/docs/ipc.html">IPC</a> interface.
28 In this article, we will assume the script is stored in <code>~/.config/i3/switch-workspace.py</code></p>
29
30 <p>Here is a possible implementation of the script:</p>
31
32 <pre>
33 #!/usr/bin/env python
34
35 from json import loads
36 from os import popen
37 from sys import argv
38
39 def ipc_query(req="command", msg=""):
40     ans = popen("i3-msg -t " + req + " " +  msg).readlines()[0]
41     return loads(ans)
42
43 if __name__ == "__main__":
44     # Usage &amp; checking args
45     if len(argv) != 2:
46         print "Usage: switch-workspace.py name-of-workspace"
47         exit(-1)
48
49     newworkspace = argv[1]
50
51     # Retrieving active display
52     active_display = None
53     for w in ipc_query(req="get_workspaces"):
54         if w['focused']:
55             active_display = w['output']
56
57     # Moving workspace to active display
58     print ipc_query(msg="'workspace " + newworkspace + "; move workspace to output " + active_display + "; workspace " + newworkspace + "'")
59 </pre>
60
61 <p>This script uses i3-msg directly, but if you have several IPC scripts, or more
62 complex ones, or if you use quotes in your workspaces' names you may want to
63 use the <a href="https://github.com/ziberna/i3-py">python i3-ipc bindings</a>
64 as done in <a href="/docs/user-contributed/swapping-workspaces.html">this article</a>
65 (however, in this case, you might have to add some bindings to the library to
66 implement chained command since sequentiality is critical in this script).</p>
67
68 <p>Do not forget to make the script executable, usually, the following
69 command, assuming you adopted the naming of our article, should work:</p>
70
71 <pre>
72 chmod u+x ~/.config/i3/switch-workspace.py
73 </pre>
74
75 <h2>Configuration</h2>
76
77 <p>Once the script is created and made executable, the last step is to add the
78 corresponding bindings in your i3 conf file.</p>
79
80 <p>Basically, one way to do that is to replace the <code>workspace X</code> bindings with
81 <code>exec --no-startup-id ~/.config/i3/switch-workspace.py X</code>.</p>
82
83 <p>For a standard <em>fr bépo</em> keyboard layout, this will result in:</p>
84
85 <pre>
86 # switch to workspace
87 bindsym $mod+quotedbl exec --no-startup-id ~/.config/i3/switch-workspace.py 1
88 bindsym $mod+guillemotleft exec --no-startup-id ~/.config/i3/switch-workspace.py 2
89 bindsym $mod+guillemotright exec --no-startup-id ~/.config/i3/switch-workspace.py 3
90 bindsym $mod+parenleft exec --no-startup-id ~/.config/i3/switch-workspace.py 4
91 bindsym $mod+parenright exec --no-startup-id ~/.config/i3/switch-workspace.py 5
92 bindsym $mod+at exec --no-startup-id ~/.config/i3/switch-workspace.py 6
93 bindsym $mod+plus exec --no-startup-id ~/.config/i3/switch-workspace.py 7
94 bindsym $mod+minus exec --no-startup-id ~/.config/i3/switch-workspace.py 8
95 bindsym $mod+slash exec --no-startup-id ~/.config/i3/switch-workspace.py 9
96 bindsym $mod+asterisk exec --no-startup-id ~/.config/i3/switch-workspace.py 10
97 </pre>
98
99 <p>But of course, you will have to adapt this to your own keyboard layout.</p>
100
101 <h2>References</h2>
102
103 <ul>
104 <li><a href="https://github.com/ziberna/i3-py">Python i3-ipc bindings</a></li>
105 <li><a href="/docs/user-contributed/swapping-workspaces.html">Swapping workspaces</a></li>
106 <li><a href="/docs/ipc.html">IPC interface</a></li>
107 <li>A bug repport explaining <a href="http://bugs.i3wm.org/report/ticket/809">how to make the focus follow the moved workspace</a></li>
108 </ul>
109
110 <p>Author: <a href="http://captnfab.chezlefab.net/">captnfab</a>, 2014-08-02</p>