KDE Tip: Automatically setup Yakuake upon login

By Niels on .

Yakuake is a tremendously useful terminal that hides itself outside of your sight until called with F12, and loved by many powerusers. It allows you to create tabs, label tabs, split screens, and much more.

One of its lesser known features is the D-Bus API that it export, allowing you to almost entirely control all of its features through a script or the command line directly. One of the greatest things this API allows you to do is to set up your tabs, labels the way you would like it to. This allows you to create tabs that are used commonly, even containing commands that you commonly run, such as top for instance.

Start by ensuring that you have qdbus:

$ which qdbus
/usr/bin/qdbus

Then create a script in KDE's autostart directory:

  • cd ~/.kde/Autostart
  • touch setup_yakuake
  • chmod +x setup_yakuake

If you do not have yakuake starting automatically, you can also add it to KDE's autostart directory:

  • Open Dolphin and go to ~/.kde/Autostart.
  • Split the view by pressing F3, point the other panel to applications://.
  • Lookup the Yakuake under System and copy it to your autostart directory.
  • Right-click the created .desktop-file, go to Permissions in its Properties-dialog and select Is executable.

Open ~/.kde/Autostart/setup_yakuake in your favorite text editor and start by writing the following boilerplate code:

#!/bin/bash

function instruct {
    cmd="qdbus org.kde.yakuake $1"
    eval $cmd &> /dev/null
    sleep 0.5
}

Now you can start using instruct to open new tabs:

instruct "/yakuake/sessions org.kde.yakuake.addSession"
instruct "/yakuake/sessions org.kde.yakuake.addSession"
instruct "/yakuake/sessions org.kde.yakuake.addSession"
instruct "/yakuake/sessions org.kde.yakuake.addSession"

This creates 4 tabs, with index identifiers such as 0, 1, 2, 3. With those id's you can set the tab titles according to your preferences like this:

instruct "/yakuake/tabs org.kde.yakuake.setTabTitle 0 ROOT"
instruct "/yakuake/tabs org.kde.yakuake.setTabTitle 1 htop"
instruct "/yakuake/tabs org.kde.yakuake.setTabTitle 2 Raspberrypi"
instruct "/yakuake/tabs org.kde.yakuake.setTabTitle 3 logwatch"

For each tab a shell-session has been created which we can also interact with. Confusingly these have identifiers starting at 1, so the four tabs live at: 1, 2, 3, 4. We can now put in commands that are "typed" into the shell sessions as if it were you typing:

instruct "/Sessions/1 org.kde.konsole.Session.sendText 'sudo su'"
instruct "/Sessions/2 org.kde.konsole.Session.sendText 'htop'"
instruct "/Sessions/3 org.kde.konsole.Session.sendText 'ssh root@rpi'"
instruct "/Sessions/4 org.kde.konsole.Session.sendText 'tail -f /var/log/messages'"

That will only type text, not press enter for them. If you want some of the commands to be executed too, that works like this:

instruct "/Sessions/2 org.kde.konsole.Session.sendText \$'\n'"
instruct "/Sessions/4 org.kde.konsole.Session.sendText \$'\n'"

Once you configured your script to your liking you can test it by restarting Yakuake - so that it becomes empty - and by running your script manually. Once everything is fine you can log out and login again, and your terminal should be set up the way you wanted.