1. Overview

Workspaces, also known as virtual desktops, is a feature provided by desktop environments and window managers to manage and organize the desktop more efficiently. It provides a way to group running applications, thereby providing a way to quickly switch between tasks and projects.

In this tutorial, we’ll learn how workspaces work and how to switch between them using CLI. Additionally, we’ll also discuss how to switch workspaces in window managers.

2. Virtual Desktops and Linux

There’s a plethora of desktop environments and window managers in the Linux ecosystem, each providing its implementation of virtual desktops. In addition, they also name these virtual desktops as tags, activities, views, tabs, and so on. However, their goal is the same, which is to provide different views of the desktop.

Every desktop environment is built on top of a window manager. As the name suggests, a window manager controls the geometry and visuals of windows. Apart from that, it’s also responsible for the management of virtual desktops.

In the Linux userland, most window managers provide some kind of a CLI front-end to interact with the window manager. For instance, i3 provides i3-msg for this purpose.

Conversely, it’s not always the case, as seen in popular desktop environments like GNOME. GNOME doesn’t have any dedicated built-in tool that lets us change workspaces. So, we rely on 3rd-party tools.

Furthermore, most popular distributions have transitioned to Wayland. It works entirely differently from the X Window System. So, the traditional tools written for the X Window System don’t work in Wayland.

In the next section, we discuss the xdotool, which works for most desktop environments using the X Window System.

3. X Window System

xdotool is a CLI utility that lets us simulate keyboard and mouse actions for the X Window System. Therefore, it’s suitable for automation and shell scripting.

By default, it may not be installed on our machine. However, it’s available on most official package repositories:

# Debian and derivatives
$ sudo apt install -y xdotool

# Fedora and derivatives
$ sudo dnf install -y xdotool

# OpenSUSE and derivatives
$ sudo zypper install --non-interactive xdotool

# Arch and derivatives
$ sudo pacman -Sy --noconfirm xdotool

Once it’s installed, let’s test it:

$ xdotool type "Baeldung on Linux"
Baeldung on Linux

3.1. Switch to a Workspace

We use the set_desktop action to switch to a workspace:

$ xdotool set_desktop 2

In the command, we specified 2 as the argument for set_desktop, which switches to the third workspace. We should keep in mind that the workspace index starts from 0, which corresponds to workspace 1.

3.2. Switch to a Workspace Relatively

In the same way, we specify the –relative option to switch to the next and previous workspaces relative to the current workspace. It expects a value that is either a negative or a positive integer:

$ xdotool set_desktop --relative 1

Since 1 is a positive number, it switches us to the next workspace relative to the current one. For instance, if we’re in workspace 2, then it takes us to the third workspace.

In contrast, we provide a negative value to switch to a previous workspace:

$ xdotool set_desktop --relative -- -2

The command takes us two workspaces back from the current one. So, if we’re in workspace 3, it takes us to workspace 1.

Additionally, the  signifies the end of options and the beginning of positional arguments. Thus, it prevents misinterpretation of options that start with a hyphen.

4. Wayland

Recently, a few desktop environments like GNOME and COSMIC have transitioned to Wayland. In Wayland, the windows and workspace management is carried out by the compositor. Therefore, the tools developed for the X Window System won’t work in it.

In addition, there is no silver bullet tool for the job because each Wayland compositor provides its own implementation of the feature. For instance, the Sway compositor implements this feature in its swaymsg tool.

However, if we’re on GNOME, we can use wmctrl. wmctrl is a tool that lets us manage windows and virtual desktops on EWMH-compatible (Extended Window Manager Hints) X Window Manager. Apart from that, it also works on the recent versions of GNOME that use Wayland.

wmctrl is available on most official package repositories:

# Debian and derivatives
$ sudo apt install -y xdotool

# Fedora and derivatives
$ sudo dnf install -y xdotool

# OpenSUSE and derivatives
$ sudo zypper install --non-interactive xdotool

# Arch and derivatives
$ sudo pacman -Sy --noconfirm xdotool

Now, let’s verify it:

$ wmctrl --version

4.1. Switching Workspaces

We use the -s option followed by a workspace number to switch to that workspace:

$ wmctrl -s 1

Like xdotoolwmctrl counts from 0. So, the command above switches to workspace 2.

Moreover, wmctrl is very limited. There is no option to change workspaces relatively.

5. Window Managers

xdotool and wmctrl are suitable for desktop environments and window managers that don’t provide built-in tools for switching workspaces. However, most window managers have built-in implementation for such tasks.

5.1. i3

i3 comes with the i3-msg tool. We use the workspace action to switch workspaces:

$ i3-msg workspace <n>

Similarly, we can go to the next or previous workspaces as well:

# Go to the next workspace
$ i3-msg workspace next;

# Go the previous workspace
$ i3-msg workspace prev;

5.2. BSPWM

BSPWM comes with the bspc client that avails this feature:

$ bspc desktop -f ^1

In the command, we can replace ^1 with the desktop number. Additionally, we can also go to the next or previous desktops:

# Go to the next desktop
$ bspc desktop -f next;

# Go to the previous desktop
$ bspc desktop -f prev;

Moreover, we can switch to the last active workspace as well:

$ bspc desktop -f last

5.3. Sway

Sway is a Wayland-based replacement for i3. It’s completely compatible with the i3 config. Like i3-msg, Sway comes with swaymsg:

$ swaymsg workspace number <n>

We can replace <n> with a workspace number to switch to that workspace. In contrast, we also replace it with next and prev to switch to the next or previous workspace, respectively:

# Switch to next workspace
$ swaymsg workspace next;

# Switch to prev workspace
$ swaymsg workspace prev;

6. Conclusion

Virtual desktops have been an essential component of a productive user experience since the dawn of Linux on the desktop. Different utilities in the Linux userland let us manipulate and switch workspaces.

In this article, we learned how to automate switching workspaces from the command line using tools like xdotool and wmctrl. Apart from that, we also discussed several window managers, where we switch workspaces using their built-in tools.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.