I’m trying to change the default profile as well the current profile of running konsole sessions with a script. How do I achieve this? Any help will be appreciated.
As @kswede mentioned, you cannot “change current profile of a running konsole session” - a Konsole “profile” describes how a session looks but also how to create it, so once it has been created it doesn’t make sense to change it.
What you may want to do is to change the appearance of a profile that is currently running?
Shouldn’t there be a dbus way of changing profile in current session, as konsole offers an option called “Switch Profile” which lets one choose the profile for current session.
-
To change the profile in the current session run:
qdbus $KONSOLE_DBUS_SERVICE $KONSOLE_DBUS_SESSION org.kde.konsole.Session.setProfile "Profile 1" -
To switch the profile on all active sessions (windows/tabs/splits) you can do it like this:
#!/usr/bin/env bash PROFILE="Profile 1" for instance in $(qdbus | grep org.kde.konsole); do for session in $(qdbus "$instance" | grep -E '^/Sessions/'); do qdbus "$instance" "$session" org.kde.konsole.Session.setProfile "$PROFILE" done done -
To change the default profile for new instances you can create a script that changes the value of
DefaultProfilein the config file$HOME/.config/konsolerc[Desktop Entry] DefaultProfile=Profile 1.profile -
Bonus: To change the color scheme of the current session (replace Breeze with other konsole color scheme name) run:
konsoleprofile "colors=Breeze"
Thank you, it worked like a charm! For default profile I am using kwriteconfig to change the value which is working as well.
Thank you all for your inputs. Have a great day!
Thank you! This is almost perfect. Changing the profile in all active sessions works, and I successfully changed the DefaultProfile value in $HOME/.config/konsolerc by adding this line to your script:
sed -i "s/^DefaultProfile=.*/DefaultProfile=$PROFILE.profile/g" "$HOME/.config/konsolerc"
However, the changes to konsolerc are not directly applied to active Konsole sessions, meaning new tabs will still use the previous default profile until Konsole is restarted. Unfortunately I couldn’t find a way to force Konsole to reload konsolerc, so not sure if this is solvable without updates to Konsole itself.
Edit: Also tried using kwriteconfig5 --file ~/.config/konsolerc --group "Desktop Entry" --key DefaultProfile "$PROFILE.profile" which also correctly changes the DefaultProfile value but has the same problem: New tabs are still opened in the previous profile until Konsole is restarted.
Hmm yeah, experienced that when adding konsole color scheme support to kde-material-you-colors, adding this to the shell config should work:
function konsole_sync_current_profile() {
if [ -z "${KONSOLE_DBUS_SERVICE}" ]; then
return
fi
local saved_profile
local current_profile
saved_profile="$(kreadconfig6 --file konsolerc --group 'Desktop Entry' --key DefaultProfile | sed 's/.profile//')"
current_profile="$(qdbus "$KONSOLE_DBUS_SERVICE" "$KONSOLE_DBUS_SESSION" org.kde.konsole.Session.profile)"
if [[ -n "$saved_profile" ]] && [[ "$saved_profile" != "$current_profile" ]]; then
echo "Setting profile: $current_profile -> $saved_profile"
qdbus "$KONSOLE_DBUS_SERVICE" "$KONSOLE_DBUS_SESSION" org.kde.konsole.Session.setProfile "$saved_profile"
fi
}
(konsole_sync_current_profile &)
Thanks for the quick response! Unfortunately your suggested function doesn’t seem to work, new tabs are still using the previous default profile. Let me post the full script I used here to make sure I didn’t do something wrong (I only changed kreadconfig6 to kreadconfig5 because I don’t have KDE 6 yet, maybe that’s why it’s not working?):
#!/usr/bin/env bash
konsoleswitch () {
local PROFILE="$1"
for instance in $(qdbus | grep org.kde.konsole); do
for session in $(qdbus "$instance" | grep -E "^/Sessions/"); do
qdbus "$instance" "$session" org.kde.konsole.Session.setProfile "$PROFILE"
done
done
# Change DefaultProfile value
sed -i "s/^DefaultProfile=.*/DefaultProfile=$PROFILE.profile/g" "$HOME/.config/konsolerc"
}
function konsole_sync_current_profile() {
if [ -z "${KONSOLE_DBUS_SERVICE}" ]; then
return
fi
local saved_profile
local current_profile
saved_profile="$(kreadconfig5 --file konsolerc --group 'Desktop Entry' --key DefaultProfile | sed 's/.profile//')"
current_profile="$(qdbus "$KONSOLE_DBUS_SERVICE" "$KONSOLE_DBUS_SESSION" org.kde.konsole.Session.profile)"
if [[ -n "$saved_profile" ]] && [[ "$saved_profile" != "$current_profile" ]]; then
echo "Setting profile: $current_profile -> $saved_profile"
qdbus "$KONSOLE_DBUS_SERVICE" "$KONSOLE_DBUS_SESSION" org.kde.konsole.Session.setProfile "$saved_profile"
fi
}
konsoleswitch "$@"
(konsole_sync_current_profile &)
For me, expanding the script like this has worked:
for instance in $(qdbus | grep org.kde.konsole); do
for session in $(qdbus "$instance" | grep -E '^/Sessions/'); do
qdbus "$instance" "$session" org.kde.konsole.Session.setProfile "$PROFILE"
done
for window in $(qdbus "$instance" | grep -E '^/Windows/'); do
qdbus "$instance" "$window" org.kde.konsole.Window.setDefaultProfile "$PROFILE"
done
done
The script can also be easily modified to additionally change Yakuake profiles (don’t forget to change ~/.config/yakuakerc):
for instance in $(qdbus | grep -E "org.kde.(yakuake|konsole)"); do
for session in $(qdbus "$instance" | grep -E '^/Sessions/'); do
qdbus "$instance" "$session" org.kde.konsole.Session.setProfile "$PROFILE"
done
for window in $(qdbus "$instance" | grep -E '^/Windows/'); do
qdbus "$instance" "$window" org.kde.konsole.Window.setDefaultProfile "$PROFILE"
done
done