11

I know that its possible to set profile for new tab, but what if i need to switch profile for current tab?

Evzin
  • 151

2 Answers2

12

From Changing Konsole colors in KDE using the shell the solution is quite simple but cover color

konsoleprofile colors=WhiteOnBlack
# or
konsoleprofile colors=GreenOnBlack

Where the value come from menu Settings > Edit Current Profile… > select Appearance tab.

Tmux

You need to wrap the command in the passthrough escape sequence inside tmux session, otherwise it won't do anything:

printf '\033Ptmux;\033\033]50;konsoleprofile colors=GreenOnBlack\007\033\\'

Here is are tiny helpers I put in my ~/.zshrc:

# Konsole color changing
theme-night() {
  switch-term-color "colors=TomorrowNightBlue"
}
theme-light() {
  switch-term-color "colors=Tomorrow"
}
switch-term-color() {
  arg="${1:-colors=Tomorrow}"
  if [[ -z "$TMUX" ]]
  then
    konsoleprofile "$arg"
  else
    printf '\033Ptmux;\033\033]50;%s\007\033\\' "$arg"
  fi
}

Credits

  • 1
    konsoleprofile command modifies the current profile. It doesn't switch it – smac89 Jan 20 '24 at 03:27
  • As @smac89 noted this does not change the profile used for the current tab but a color scheme used by the profile already active. This results in all other tabs using the same profile start to use the new color scheme as well (global change) which is totally undesirable. – Piotr Dobrogost Nov 12 '24 at 14:11
1

As noted by Luis Bocanegra in his answer this can be done by calling setProfile method of org.kde.konsole.Session Konsole's dbus interface.

Example using qdbus-qt6:

qdbus-qt6 $KONSOLE_DBUS_SERVICE $KONSOLE_DBUS_SESSION org.kde.konsole.Session.setProfile 'Profile 1'

Example using dbus-send:

dbus-send --dest=$KONSOLE_DBUS_SERVICE $KONSOLE_DBUS_SESSION --type=method_call org.kde.konsole.Session.setProfile string:'Profile 1'

For example to change Konsole's profile based on current directory in ZSH define the following function in either ~/.zprofile or ~/.zshrc:

chpwd () {
  case "$PWD/" in
    $HOME/some/directory/*)
      konsole_profile='Profile 1'
      ;;

$HOME/another/directory*)
  konsole_profile='Profile 2'
  ;;

$HOME/*)
  konsole_profile='home'
  ;;

*)
  konsole_profile='default'
  ;;

esac dbus-send --dest=$KONSOLE_DBUS_SERVICE $KONSOLE_DBUS_SESSION --type=method_call org.kde.konsole.Session.setProfile string:$konsole_profile }

Related:
Chapter 4. Scripting Konsole

How to check if $PWD is a subdirectory of a given path

Dennis Williamson's answer to the question How to change colors programmatically in Konsole based on current directory?