Change konsole tab title from command line and make it persistent?
Asked Answered
D

8

22

How can I change the konsole tab title? By default, it's set to %u : %n, so it always changes automatically.

I can set it with:

qdbus org.kde.konsole $KONSOLE_DBUS_SESSION setTitle 1 "My Title"

But as soon as you run something in the console, it changes back to %u : %n. Of course. I can set it to %w to tell it to set the title to "Window Title set by shell", but later (if I don't explicitly set it), it will be empty. (Every new tab will be empty, unless I set it.)

The question is how to make it persistent (or how to switch profile via command line)? I can make another profile in which the title format is set to %w, and before I set my custom title, switch to that profile.

enter image description here

enter image description here

edit: I think I have found the answer

qdbus org.kde.konsole $KONSOLE_DBUS_SESSION setTabTitleFormat 0 "" qdbus org.kde.konsole $KONSOLE_DBUS_SESSION setTitle 0 "My Title"

Deanndeanna answered 11/11, 2013 at 2:6 Comment(1)
That doesn't work: dbus org.kde.konsole $KONSOLE_DBUS_SESSION setTabTitleFormat 0 "" Cannot find '.setTabTitleFormat' in object /Sessions/1 at org.kde.konsoleDowsabel
B
28

You may need to use this variant:

echo -ne "\033]30;test change title\007"

$ konsole -v
Qt: 4.8.6
KDE Development Platform: 4.13.3
Konsole: 2.13.2
Byrann answered 7/8, 2014 at 5:51 Comment(4)
Works for me konsole 16.08.1Ostracon
And it persists after initiating an SSH sessionTerpsichorean
Only works if tab title is "%w" and option to show window title has been enabled in configuration, see later answer by @falkoGoldsworthy
What's the difference between \033]2; and \033;30;? The latter seems faster and permanent, but is there any documentation on this?Dorie
T
24

Konsole -> Settings -> Configure Konsole ...

Enable option "Show window title on the titlebar" then run in console:

echo -ne "\033]2;test change title\007" 
Tatty answered 27/1, 2014 at 11:49 Comment(3)
can't find the menu you mentioned, i'm on centos 6.4 x64, and the command line doesn't work tried in bash and tcsh shell (btw our default shell is tcsh)Deanndeanna
oh the echo command is working, but i first need to set tab title format to %w (window title set by shell )Deanndeanna
set tab title format to %w (window title set by shell ) printf "\033]2;test change title\007" or echo "\033]2;test change title\007" or /bin/echo -ne "\033]2;test change title\007"Deanndeanna
H
15

konsole -p tabtitle='some title: %w'

For more options try konsole --list-profile-properties.

Hoax answered 3/12, 2015 at 14:11 Comment(3)
This is the best answer!Devoe
This opens a new window instead of changing the title of the current tab (version 17.12.3).Sparoid
Could you please tell me how to set konsole tabbar title height?Rental
G
12

Posting my own solution to this old question, because

  • OP solution does not work on newer(?) KDE systems due to the hard-coding of the service name to org.kde.konsole
  • the answers based on echo require a permanent change to konsole settings

After reading the Scripting Konsole chapter in the konsole documentation I wrote these bash functions which can be added to $HOME/.bashrc:

set-konsole-tab-title-type ()
{
    local _title="$1"
    local _type=${2:-0}
    [[ -z "${_title}" ]]               && return 1
    [[ -z "${KONSOLE_DBUS_SERVICE}" ]] && return 1
    [[ -z "${KONSOLE_DBUS_SESSION}" ]] && return 1
    qdbus-qt5 >/dev/null "${KONSOLE_DBUS_SERVICE}" "${KONSOLE_DBUS_SESSION}" setTabTitleFormat "${_type}" "${_title}"
}
set-konsole-tab-title ()
{
    set-konsole-tab-title-type "$1" && set-konsole-tab-title-type "$1" 1
}

Example 1: set both local & remote tab formats

$ set-konsole-tab-title test

Example 2: leave remote tab format unchanged

$ set-konsole-tab-title-type test

Example 3: leave local tab format unchanged

$ set-konsole-tab-title-type test 1

You can also use this function to set the tab title dynamically to %w for the echo solutions.

NOTE

The code above assumes KDE Plasma/Qt5. For KDE4/Qt4 you'll need replace qdbus-qt5 with qdbus

Goldsworthy answered 11/1, 2019 at 7:54 Comment(1)
Noted. Going to suggest KDE Framework Gear initiative teams for Konsole and KWIN work this into documents, and ci pipeline, and make .sh logic here into the Konsole Qt5/6/C++ binary logic. I have yet to try it but suspect this is key to resolving awkward shortfalls of Konsole/Plasma terminal vs others. Hope it can also give KWIN reactivity/response based on title condition as well (aside .desktop or .profile or property assignments direct from Konsole command).Nautch
K
5

Improvement of this answer by Stefan Becker. This script accepts multiple words for a title. You can use it to change both remote and local titles. Without parameters, it sets to the default title. I called it setTitle

#!/bin/bash
titleLocal=${1:-%d : %n}
titleRemote=${2:-(%u) %H}

set-konsole-tab-title-type()
{
    local _title="$1"
    local _type=${2:-0}
    [[ -z "${_title}" ]]               && return 1
    [[ -z "${KONSOLE_DBUS_SERVICE}" ]] && return 1
    [[ -z "${KONSOLE_DBUS_SESSION}" ]] && return 1
    qdbus >/dev/null "${KONSOLE_DBUS_SERVICE}" "${KONSOLE_DBUS_SESSION}" setTabTitleFormat "${_type}" "${_title}"
}

set-konsole-tab-title-type "$titleLocal" && set-konsole-tab-title-type "$titleRemote" 1

Change both titles:

setTitle "My Local Title" "My Remote Title" 

Change only local title:

setTitle "My Local Title" 

Change only remote title:

setTitle "" "My Remote Title" 

Change to defaults:

setTitle
Kleiman answered 19/4, 2021 at 12:26 Comment(2)
Is it possible to use HTML tags such as BOLD for some parts? For example "Feed the cat in two hours." where cat would be bold e. g. <b>cat</b>.Contravallation
It is not possible to use HTML tags.Kleiman
H
0

First install wmctrl if that is not in your system. It is for interacting with a EWMH/NetWM compatible X Window Manager.

  1. You can change the title of the active Konsole by typing
    wmctrl -r :ACTIVE: -N "New Title 1"

in the active Konsole. Title remains until you close the Konsole.

  1. You can also change the title outside the active Konsole. First list all your windows, type:
    wmctrl -l
    ... lines deleted ...
    0x05c00007  0 myMachine New Title
    ... lines deleted ...

Or if you also want pid, window x,y,w,h and WM_CLASS, type:

    wmctrl -l -p -G -x
    ... lines deleted ...
    x05c00007  0 2706379 360  106  1760 877  konsole.konsole  myMachine New Title
    ... lines deleted ...

Then find your target window's id (=first column) and type in any terminal:

`wmctrl -i -r 0x05c00007 -N "New Title 2"`

Word of warning: If you want to parse the output of wmctrl -l -p -G -x please not that the column WM_CLASS content can contain spaces. E.g. virtualbox gives: VirtualBox Machine.VirtualBox Machine

Homager answered 25/10, 2023 at 13:31 Comment(0)
T
-1
You may need to use this variant:

`echo -ne "\033]30;test change title\007"`


    $ konsole -v
    Qt: 4.8.6
    KDE Development Platform: 4.13.3
    Konsole: 2.13.2

I was never happy with setting the title for konsole windows, then I saw Tim's comment. This was a big improvement for me. Thank you, Tim!!

But that did not quite get me where I wanted to be.

I wanted to go beyond typing in the given command line example because I have a terrible memory and 3 weeks from now, I will not remember the character sequence of the variant.

I wanted to be able to create an alias that would allow me to do the following:

define an alias 'T' for my shell (tcsh in my case) so that I can enter:

    T titleName

and another alias 'DT' so that I can enter:

    DT titleName

The 'T' alias changes the window title to 'titleName' and the alias 'DT' changes the window title to '%d titleName' where %d is the directory name.

It took some experimenting. It is worth mentioning that I took a bad first step by trying to edit my .cshrc first. I could not quite get the backslash escaping to work correctly, not being sure when the escaping was taking place: when reading the .cshrc file or when running the alias. After a few minutes of frustration, I tried defining the alias first, character by character, in a cycle:

a) add a new character to the alias
b) echo the current alias by enter 'alias T' to verify the csh interpretation
c) add backslashes as needed
d) verify again.

This worked and I got my alias to work in a minute or two. Once the aliases were working, I just copied and pasted them into my .cshrc file and the aliases both worked. My working aliases are:

alias T echo -ne \"\033]30\;\$Z\007\"

and

alias DT echo -ne \"\033]30\;\%d\ \$Z\007\"

Note that I had to use the environment variable 'Z' to make the aliases 'variable'. Not too elegant, but it largely satisfies me. So after changing my .cshrc and sourcing the .cshrc file in the window shell, I do this

    set Z=Example
    T

or

    set Z=Example2
    DT

and I get my title set. When I change to a different task, I change Z to a new name and run my alias 'T' or 'DT' again.

Totemism answered 3/1, 2018 at 15:48 Comment(0)
M
-3

To set title "MyTitle" on your new tab the right syntax is:

konsole --new-tab -p "RemoteTabTitleFormat=MyTitle" ...

(tested on Konsole version 17.12.3, S.O. Kubuntu 18.04)

Mordred answered 10/11, 2018 at 15:56 Comment(1)
Incorrect answer. This opens a new tab instead of changing the window title of the current tab.Goldsworthy

© 2022 - 2024 — McMap. All rights reserved.