4

How can one set the name of a Konsole window in KDE? I know that we can change the names of the tabs, and the Konsole window title is preconfigured to use the name of the current tab, but this is not what I would like to do.

I have one Konsole window with three tabs that I always keep open (vimwiki, cmus, and a custom Python script) which I would like to be easy to find among my sea of other Konsole windows, each with its own set of tags.

As a workaround name all tabs to the name that I would like for the window, but that has many drawbacks. A simple way to rename the entire window would be best. I could also use a different terminal emulator for this 'special' window, but I really like Konsole.

3

5 Answers 5

4

you can add a bash function at the end of your ~/.bashrc

title() {
  echo $'\033]30;'$*$'\007'
}

(after saving .bashrc remember to type in the console: . ~/.bashrc to add the function to your shell, or else you can open a new terminal)

You can use the new function like this:

title this is my console
2
  • 1
    Is this valid in 2021 for KDE Plasma 5+?
    – Cymatical
    Commented Sep 19, 2021 at 20:29
  • Just tried (July 2022) in Konsole on KDE 5.18.4, still works.
    – dotancohen
    Commented Jul 27, 2022 at 15:15
3

It seems, at least in recent versions of Konsole, the title cannot be changed. You can change the tab, though, for example by the following ANSI sequence:

echo $'\033]30;NewName\007'

Update:

For inspiration, I tried another approach. I created a file ~/konsole-name.sh:

function kname {
    name=$(grep $WINDOWID .knamerc)
    name=${name#*$'\t'}
    if [[ $name ]] ; then
        qdbus org.kde.konsole $KONSOLE_DBUS_SESSION \
            org.kde.konsole.Session.setTitle 1 $name > /dev/null
    fi
}

function kname-set {
    sed -i "/^$WINDOWID\t/d" .knamerc
    echo $WINDOWID$'\t'"$1" >> .knamerc
    kname
}

And I added the following to my .bashrc:

. ~/konsole-name.sh && kname

Then, when a new konsole is started, I can just type kname-set THE-ONE-TRUE-KONSOLE in the first window. All newly created tabs will be named the same. You can give any konsole its "name" by calling the function in its first tab.

You might need to clobber the file .knamerc on logout.

The rest is left as an exercise to the reader :-)

5
  • Relevant: bugs.kde.org/show_bug.cgi?id=179142
    – terdon
    Commented Jan 23, 2014 at 16:25
  • Thank you choroba, but as I mention in the OP, setting all the tabs' titles is not practical and has too many drawbacks.
    – dotancohen
    Commented Jan 24, 2014 at 14:34
  • @dotancohen: Check the update.
    – choroba
    Commented Jan 24, 2014 at 22:51
  • Is this valid in 2021 for KDE Plasma 5+?
    – Cymatical
    Commented Sep 19, 2021 at 20:27
  • @Cymatical: I have no idea. I moved to LXDE.
    – choroba
    Commented Sep 19, 2021 at 20:45
2

Have the Konsole's Title show the text of the active Konsole Tab

For the title to reflect the text of the currently selected Konsole tab, Konsole must be configured with the "Show window title on the titlebar" option disabled (i.e., unchecked). If this is not done, the code in set-konsole-active-tab-text.sh, near the bottom of this answer, will only be able to change the Konsole tab text, but the Konsole title will not be affected and will retain its value when the code is executed.

In fact, if you see this behavior, the tab changing but the title staying fixed at its former value, it is a fairly good indication that the "Show window title on the titlebar" configuration option is set for the current Konsole instance.

To set this property, open the Konsole General Configuration using the the Settings/Configure Konsole menu item. Make sure that the top "General" item is selected on the left and under the General settings on the right and then either set or confirm that the "Show window title on the titlebar" configuration item is unchecked.

Set the Konsole Title to a Fixed String (independent of tabs)

On the other hand, if you desire to simply set the Konsole Title and keep it independent of the text in the currently selected tab, you can set/leave the "Show window title..." option checked. Then, you can use the following code snippet to set the Konsole Title to a fixed setting and keep it independent of any prior/subsequent changes to Konsole tab text settings for all current and newly created tabs in the window:

set-konsole-title.sh

#!/usr/bin/env bash

# Script: set-konsole-title.sh
# Author: Michael Goldshteyn
# Last modified: 2022-07-26

# Prerequisites
# Requires KDE Plasma 5 or higher
# The "Show window title on the titlebar" configuration option
#   must be enabled (checked "on," in Konsole Config/General)

# Note: If no argument is passed to the script, the Konsole title
#   will get set to an empty string.
konsole_title="$1:-"

# Use an ANSI escape code to change the Konsole Title
printf $'\033]2;%s\007' "$konsole_title"

Title/Tab Text association

I would like to reiterate that thef following method, assuming "Show window title on the titlebar" is unchecked as previously described, not only allows for programmatic tab text changes but also dynamically ties the title of the Konsole window to the text found in the currently active tab. This implies that the Konsole title will be dynamic, changing as you move between tabs, updated to reflect the text of the tab made active during tab activation and thereafter staying in sync with the text of this tab (as long as it stays active).

The following method provides the ability to set the tab text of the currently active tab programmatically, perhaps to a fixed string, and then have the Konsole title optionally reflect the text as displayed in the currently active tab.

Bonus Tip

The initial text for a newly opened tab can be specified in the Konsole's Tabs Configuration Section - under the Settings menu, select Configure Konsole as for the prior point and select the Tabs configuration section on the left-hand side of the Configure - Konsole dialog box that opens.

Cont...

If you notice that your Konsole title is (strangely) reverting to the display of the default tab text, it is most likely due to the following script not being run for a newly opened tab (to change its text to the text you prefer).

Programmatically Changing the Text of the Active Konsole Tab

Here is a script showing a way to accomplish this, beginning with KDE Plasma 5, as a fully commented stand-alone Bash shell script you can add to your collection of useful utility scripts:

set-konsole-active-tab-text.sh

#!/usr/bin/env bash
set -x

# Script: set-konsole-active-tab-text.sh
# Author: Michael Goldshteyn
# Last modified: 2022-07-26

# A Bash script that can be used to set the text of the currently
# active tab of a Konsole process, optionally updating the Konsole
# window's title in the window caption as a side-effect, and keeping
# the two in sync.

######################### Prerequisites #########################
# KDE Plasma 5 or higher
#
# Optional:
# The "Show window title on the titlebar" option must be disabled
#   (i.e., unchecked) under the Konsole Configuration/General
#   section, for the Konsole title text to stay synchronized with
#   the text of the currenly active Konsole tab.
#
# Link to the Stack Exchange (Super User) answer with the latest
#   version of this code: https://superuser.com/a/1733814/52267
#################################################################

set_konsole_tab_text()
{
  local -i starting_pid
  local -i pid_to_check
  local -i ppid_to_check
  local exe_filename_of_parent_proc
  local -i tab_owner_konsole_pid
  local dbus_service_name
  local dbus_path
  local -i role
  local tab_text

  # If no tab text is passed in, the tab text will be set to the
  # empty string
  tab_text="$1"

  # The following code is necessary even if no tabs are visible,
  #   because Konsole suppresses the display of tabs when only
  #   a single tab is present. At least one tab is always open
  #   while a Konsole process is running. When more than one
  #   tab is open, the tabs will be shown and the text of all
  #   displayed tabs will become visible.

  # Find the PID of the Konsole process that owns the currently
  #   selected tab, by walking up the process ancestor chain of
  #   the shell process running in the context of the current
  #   tab (i.e., whatever process is executing this code).
  # Important Note: We may need to iterate up the process chain
  #   multiple times, if the following code is being executed
  #   within a subshell (e.g., if this code is placed into a
  #   shell script that gets executed within a separate (child)
  #   instance of Bash).

  # Get the process id of the current (shell) process
  starting_pid="$$" 
  pid_to_check="$starting_pid"
  # Seek out our Konsole ancestor process
  until [[ -n $tab_owner_konsole_pid ]]; do
    # Get the PID of the parent of the current process in the chain
    ppid_to_check="$(ps -p "$pid_to_check" -o ppid= | tr -d ' ')"
    # Get the name of the associated executable (i.e., app filename)
    exe_filename_of_parent_proc="$(ps -p "$ppid_to_check" -o comm=)"
    # Is it an instance of the Konsole app?
    if [[ "$exe_filename_of_parent_proc" == konsole ]]; then
      # Eureka - we found it!
      tab_owner_konsole_pid="$ppid_to_check"
    else # No, keep searching up the process ancestor chain
      pid_to_check="$ppid_to_check" # ..., from the parent process
    fi
  done

  # Construct the name of the qdbus service using the above
  dbus_service_name="org.kde.konsole-$tab_owner_konsole_pid"
  # The $KONSOLE_DBUS_SESSION environment varable gets set
  #   automatically by Konsole (and gets exported to the child
  #   shell associated with the tab).
  # This value should be used for the dbus_path argument to qdbus
  dbus_path="$KONSOLE_DBUS_SESSION"

  # Use this for the role arg of the setTitle() method, below
  role=1 

  # Set the tab text using a dbus method call
  qdbus "$dbus_service_name"\
        "$dbus_path"\
        setTitle "$role" "$tab_text"
}

# Set the text of the currently active Konsole tab to the text
# passed as an argument to this script
set_tab_text "$@"
0

Maybe this one can help you

qdbus org.kde.konsole $KONSOLE_DBUS_SESSION setTitle 1 "My Title"
3
  • Is this valid in 2021 for KDE Plasma 5+?
    – Cymatical
    Commented Sep 19, 2021 at 20:28
  • Not sure, dont remember what the command does. 7+years ago
    – Simon1901
    Commented Sep 21, 2021 at 7:28
  • 1
    No, this is not valid, it will only change the tab labels, but not the caption Commented Jul 19, 2022 at 15:15
0

If you are willing to, you can use gnome-terminal, which allows you to set a custom title. You can set a custom title under:

  • edit -> current profile -> title and command -> initial title
  • and in the drop down click: "goes before initial title"

This inital title is appended to your tab title, which you can also customize via:

  • Terminal -> set title.

gnome-terminal also allows you to switch tabs easily with alt+1, alt+2, etc!

1
  • 1
    Gnome terminal has since removed this functionality unfortunately.
    – ohmu
    Commented Feb 17, 2016 at 16:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .