92

How can I start applications on specific workspaces in i3 when it starts?

Why is this not working in my config file? :

workspace 1; exec firefox; workspace 2; exec chromium; workspace 1
Gradient
  • 3,659

5 Answers5

93

According to the Arch Wiki i3 page, to autostart an application on a specific workspace, you use i3-msg:

exec --no-startup-id i3-msg 'workspace 1:Web; exec /usr/bin/firefox'
jasonwryan
  • 73,126
  • 7
    Yes, a line like exec --no-startup-id i3-msg 'workspace 1; exec firefox; workspace 2; exec urxvt; workspace 1' works. But, I find it a little odd to use exec on i3-msg which is a command meant to execute i3 commands. Why can't I directly write workspace 1; exec firefox; workspace 2; exec urxvt; workspace 1 in my config file? – Gradient Oct 24 '13 at 05:05
  • 11
    This just opens everything in Workspace 1 for me. – cmc Oct 30 '14 at 16:15
  • 6
    Works for most apps. However google-chrome-stable always opens on the last workspace in my setup. For example, with this configuration, Chrome opens on workspace 4 next to htop. – danijar Jun 23 '15 at 20:05
  • 5
    Note that i3 now provides layout saving/restoring features that make it unnecessary to call i3-msg in most cases. – Wieland Apr 16 '16 at 15:32
  • @Wieland you should just edit that in to the answer... – jasonwryan Apr 16 '16 at 19:42
  • 4
    @danijar I would imagine it is because the amount of time it takes to startup Chrome exceeds the amount of time it takes to switch to workspace 4 and open urxvt. Google is smart and knows that the sooner it can get you a window the better, but they do this by returning "success" to the executing terminal/application (eg exec) ASAP and then loading the browser in a forked process, which is why exec thinks it was done with Chrome and it moved to the next line in your config. Add && sleep 3 and I'd bet Chrome opens in workspace 3. May need to go as high as 5 depending on # of extensions. – dragon788 Jun 19 '17 at 22:23
  • 1
    @dragon788 I ended up opening all other applications first then switching to the workspace where I want chrome to show up. That way I don't introduce any unnecessary delay. – danijar Jun 19 '17 at 22:31
  • 3
    @Wieland layout saving/restoring is different from auto start; the former only deals with setting up swallow traps for program windows while the latter starts an instance of an application as the OP requested. – Antony Thomas Jan 20 '18 at 20:57
  • You might wish to update this answer. This formulation opens all applications in the same workspace, the one defined in the first i3-msg command in the config file. This might have functioned in older versions of i3, but seems no longer valid. – Luís de Sousa Mar 22 '19 at 07:15
  • I'm seeing the Chrome behavior described above affect Electron applications. This isn't surprising, but I thought it'd be worth mentioning for others finding their way here. – pdoherty926 Jan 08 '20 at 15:06
  • For Chrome, I thought it wasn't respecting my workspace at first because it was going into a tab on the wrong workspace, where a Chrome instance was already open. (This was after i3 had fully loaded, not in my config.) Using new-window fixed it: i3-msg 'workspace 2; exec google-chrome --new-window https://example.com' – Eric Eskildsen Jan 10 '20 at 12:59
  • 1
    Assigning workspaces work more reliably for me. – joelostblom Feb 01 '20 at 09:48
  • how to place multiple windows in horizontal? – Akhil May 21 '20 at 13:05
  • how to open it in tabbed mode? – Akhil Jun 25 '20 at 11:03
25
# This is what I use in ie config
# custom variables for workspaces
set $ws1 "1< txt >"
set $ws2 "2> fm "
set $ws3 "3< Web >"
set $ws4 4
set $ws5 5
set $ws6 6
set $ws7 7
set $ws8 8
set $ws9 9


##==================================================##
#                         *** Workspace specific settings ***                     #
##=================================================##
# Assign Workspaces:
assign [class="Firefox"] $ws3
assign [class="Chromium"] $ws3
assign [class="Google-chrome-beta"] $ws3
assign [class="^Geany"] $ws1

NOTE: to apply config, you can use:

i3-msg reload

Lubo
  • 115
  • 5
head-shot
  • 351
  • 1
    This works for some applications, but not for others. I suspect it could be related with the string used in the class argument. Is there any way to know exactly to which class is an application associated? – Luís de Sousa Mar 25 '19 at 07:29
  • 5
    @LuísdeSousa you xprop | grep CLASS in terminal, your mouse pointer changes into a crosshair, you click on the program you want to get the class of and use the second variable that comes up in your terminal. Example result for Chrome: WM_CLASS(STRING) = "google-chrome", "Google-chrome" – kittenparry Mar 27 '19 at 03:34
  • @kittenparry. Thank you for the suggestion, but still using the string from the xprop command the windows are not assigned to the desired workspace. – Luís de Sousa Mar 28 '19 at 09:33
  • 10
    @LuísdeSousa some programs don't work with it like Spotify. For them you might write something similar to this for_window [class="Spotify"] move to workspace $ws10 where $ws10 is your workspace variable. – kittenparry Mar 28 '19 at 09:35
16

It's an autostart on workspace launch only, not an autostart when i3 starts but I thought it could be useful.

For example, to launch my web browser in the named second workspace "web" this is what I did in my config :

bindsym $mod+2 workspace 2:web; layout tabbed; exec [ $(ps h -C vimb | wc -l) = 0 ] && /usr/bin/vimb -s

When I hit key 2, my web browser starts but when it's already opened it didn't relaunch it, it just switch to the web workspace.

Note: the layout part can be annoying sometime, it's not a requirement.

For more details see my conf

tyjak
  • 161
  • 2
    You can do it much easily by assigning workspace. For example to move pidgin to 3-rd workspace on its every launch, I have in the config assign [class="Pidgin"] $tag3. – Hi-Angel Oct 23 '16 at 02:34
  • 1
    Assigning to a workspace is different. @tyjak's solution will cause an application to launch when you open a particular workspace. Assigning an application to a workspace will cause that application to be moved to a workspace whenever you open it. – Mike Nov 19 '16 at 08:29
11

I just got it working with this at the end of my ~/.config/i3/config file:

for_window [class="Firefox"] move container to workspace 2
exec --no-startup-id firefox
robru
  • 351
3

Just use assign [class="<use your program name here e.g. - Firefox>"] $workspace<eg. 5>. It is working for me.