How to know what is the current tab – number or position – for any Window Terminal?
For Ubuntu and Fedora if is opened a Window Terminal through ctrl + alt + t then is possible open a new tab through shift + ctrl + t. Suppose exists a Window Terminal with 5 tabs. If possible go to any of them through alt + # (where #
can be 1-5) …
Now, if the Window Terminal has more tabs such as 9,10 …
Question
- How to know what is the current tab? Its number or position.
It to handle the following situations:
- How to know to where return later (suppose that the current tab is 8, and we go to 3, and later is need it to return to 8). So is need it know/get 8.
- What is the next/previous one of the current tab (suppose that the current tab is 7 and is need it go to the next tab, 8). So is need it know/get 7.
I tried the tty
command, but if the current tab is 5, it shows /dev/pts/4
. As you can see N-1
. Until here can be applied a simple math …. and some times shows the expected direct value, it such as /dev/pts/5
. I don’t know why this difference. So the returned value is not always accurate.
Even more, if in other workspace exists other Window Terminal with some tabs, if is executed the tty
command, appears a random number, it normally would be the continuation of the highest tab + 1 of the previous Window Terminal. So if in the first Window Terminal has 5 tabs in the second Window Terminal for its 1rst tab the tty
command shows /dev/pts/5
(N-1) or directly /dev/pts/6
. But is expected /dev/pts/1
, so if new tabs are opened, the correlation should based starting from 1.
Observation consider if any tab is moved (drag and drop) to other position, the "command" should reflect the new position/number
Note Even if the tty
would be not the correct command, what command would accomplish this goal?
I’m assuming you’re using gnome-terminal
, as that’s the default you’d use in Gnome, which is the default desktop environment on these platforms. (and probably, because you’d have said if you used a different terminal emulator!)
There’s no such command to the best of my knowledge. The program (in your case, primarily the shell) executed in a tab has very little knowledge of that tab. It’s not supposed to! Also, there’s a layer of indirection between "tab" and "virtual console" (the gnome terminal server, which can technically be used to show some ptty in one, multiple, or no tab at all), so, hm, the assumption that you’re always in one specific tab simply doesn’t work in general. It might apply in the cases you care about, though.
What you can do is use the $GNOME_TERMINAL_SCREEN
environment variable to get info about the running terminal emulator session. It contains a dbus path, but as far as a "quick" introspection¹ tells us that we can get a list of open tabs², can execute commands in tabs, but that’s it.
So, atop of no such command existing, it seems what you want is not possible.
Adressing what you wanted to achieve:
suppose that the current tab is 8, and we go to 3, and later is need it to return to 8
You could set the current title of your tab, manually (right click on the tab’s title, "Set Title…") so that you know which is where
But honestly, this all sounds like you’re a "power user", using a lot of virtual terminals, and gnome-terminal
is maybe not the tool to manage all these for you. tmux
can have multiple so-called panes that can be displayed at a time, and you can have multiple windows (not to be confused with windows in the X11/wayland sense) containing panes, which you can rename, shift around, reorder, open, close… to your hearts desire. All this happens within a single gnome-terminal instance.
tmux
is a bit confusing when one comes from the graphical world (like, everyone born after 1987, I guess), but its Getting Started Guide is actually OK, when you read it from the top to the bottom and don’t try to jump into the middle of it. You can do clever things like "hey, I remember there was a pane where I’m running nvim
in, can you search all windows for that, please?".
Maybe try it out. Install tmux, then run tmux
in your gnome-terminal. You’re greeted by your normal shell and a strange little status line.
Run top
to get a constantly running system load monitor. Because we want to remember this is the window with the system monitor, we hit ctrl+b, followed by ,. Watch the status line! It now asks us for a new name for this window. I suppose "system monitoring" works. Up to you. Ok, nice, there seems to be keyboard shortcuts for things. I can’t remember very many keyboard shortcuts. So I prefer the tmux command interface: Press ctrl+b, followed by :. You can now type in commands. I type in split -h
, and hit Enter (there’s also tab auto-completion). Zack, now you have two panes in your window (split h
orizontally, by the way). I want to monitor the free space on my disks, so in that new pane I run watch df -h
. Nice.
Now I want a new tmux "window". I remember the key combination for that, ctrl+bc (c
like c
reate). I run my favorite editor in that (in my case, that would be nvim
, but in your case, it might be emacs
, vim
, vi
, nano
, ed
… I don’t judge.).
Because I want to remember what I was doing here, I rename my window. But this time, I don’t use the keyboard shortcut (ctrl+b), but simply run tmux rename-window "config edit"
or something).
Now, I can do this game for a while and have hundreds of windows in my session. The status bar lists these, but does that really help? sure, using ctrl+bf I can now search for the window where I started to write a letter to my grandma before my editor got slow, I checked on the system monitor, then started editing some config files… you get the idea.
¹ dbus-send --session --print-reply --type=method_call --dest=org.gnome.Terminal "$GNOME_TERMINAL_SCREEN" org.freedesktop.DBus.Introspectable.Introspect
² dbus-send --session --print-reply --type=method_call --dest=org.gnome.Terminal /org/gnome/Terminal/screen org.freedesktop.DBus.Introspectable.Introspect
Konsole
uses environment variable. I have no idea for other terminal emulators.
#↳ set | grep -i kon
KONSOLE_DBUS_SERVICE=:1.955
KONSOLE_DBUS_SESSION=/Sessions/2
KONSOLE_DBUS_WINDOW=/Windows/1
KONSOLE_VERSION=221203
It looks like you need to use the SESSION
/ WINDOW
tuple to identify a unique window, and session
= tab.
Consider tmux
for a more powerfull tool to do this.