0
votes

Assume the following:

  • I have multiple gnome-terminal windows open
  • each window has multiple tabs open

I know that I opened the file ".bashrc" in vim somewhere. So a tab with the title ".bashrc (~) - VIM" is in one of the gnome-terminal windows. This tab might or might not be active tab in the window.

Is there any way to identify the gnome-terminal window, that holds that tab and to switch to it (preferably activating the tab)?

More general:

Is there any way to identify and activate a gnome-terminal window by the title of one of it's tabs?

or:

Is there any way to identify and activate a gnome-terminal window by the program currently executed in on of its tabs?

1
Do you want to do this from the terminal?kvantour
@kvantour Terminal or maybe some gnome3 extension. I thought I might be able to get such info from d-bus, and then roll my own solution. I investigated with d-feet without success.Ralf

1 Answers

0
votes

Revisiting this question I found a solution (or hack) to get what I want. Gnome Terminal implements the org.gnome.Shell.SearchProvider2 dbus interface. This interface can be used to search for terminal tabs that match a given term. By using a empty string as search term, all terminal tabs are matched and the ids are returned. With this ids it is possible to get the names (aka titles) of the tabs.

I hacked together the following script to collect the information and using fzf to select the tab I'm looking for. Than this tab is activated.

#!/bin/bash

# get UUIDs of all gnome-terminal tabs
get_term_ids()
{
    # Call search with empty string ... will match all
    dbus-send --session --dest=org.gnome.Terminal --print-reply=literal \
        /org/gnome/Terminal/SearchProvider \
        org.gnome.Shell.SearchProvider2.GetInitialResultSet \
        array:string:"" \
        | tail -n 1 | sed 's/^  *//;s/ *]$//;s/  */,/g'
}

# get "uuid term-title" list
get_term_titles()
{
    ids="$(get_term_ids)"
    dbus-send --session --dest=org.gnome.Terminal  --print-reply=literal \
        /org/gnome/Terminal/SearchProvider \
        org.gnome.Shell.SearchProvider2.GetResultMetas \
        array:string:${ids%,} \
        | grep '^ *\(id\|name\) ' \
        | sed -e '/^  *id /{N;s/^ *id  *variant *\([^ ]*\) *)\n/\1/}' \
        -e 's/  *name  *variant  */ /;s/  *)$//'
}

# activate a term identified via uuid
activate_term()
{
    dbus-send --session  --dest=org.gnome.Terminal  --print-reply \
        /org/gnome/Terminal/SearchProvider \
        org.gnome.Shell.SearchProvider2.ActivateResult \
        string:$1 array:string:'' uint32:0 >/dev/null
}

result="$(get_term_titles | fzf --with-nth 2..)"
if [ -z "$result" ]; then
    exit 1
fi

activate_term "${result%% *}"