0

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?

5
  • I think you are asking about a terminal emulator program (that terminals in a window). However I have no idea which one you are asking about. Sep 17, 2023 at 19:04
  • 1
    The value printed by tty is assigned by the kernel, it's always the lowest unused value at the time of assigning. That is, this value is unique across the operating system at any given time; across multiple users, multiple different terminal emulators, multiple windows and multiple tabs of each. If there's only one user, only one terminal emulator, only one window, no other process keeping a tty line open, and you open the tabs one by one and you don't shuffle them then the numbering "accidentally" matches the tab's visual position.
    – egmont
    Sep 17, 2023 at 19:25
  • 1
    "How to know to where return later": trial and error :) "What is the next/previous one of the current tab": Ctrl+PageUp/Dn shortcut key.
    – egmont
    Sep 17, 2023 at 19:29
  • While I'm certainly not aware of your workflow, it seems that you often have more terminal tabs that you can easily keep track of. You should try to generally organize your workflow, e.g. make use of multiple windows as well, or using a tiling window manager, or whatnot. A command based solution, like your failed attempt with tty, or as it could be (but is not) implemented by talking to gnome-terminal-server via dbus, would fail you whenever you can't issue a command in a given terminal (e.g. it's running a command that you can't interrupt).
    – egmont
    Sep 17, 2023 at 19:42
  • 1
    I suggest to give each terminal tab a unique title. It could be a random string generated at the beginning of your .bashrc, or – what I personally like to use – the number from the output of tty. The latter can be placed in bash's PS1 prompt using \l, surrounded by the escape sequence that modifies the terminal's title, surrounded by \[ \] because bash's prompt needs that for nonprintable stuff. This random string can help you get back to that tab faster (although it's not the terminal's position; which might actually be useful, it doesn't change if you close a preceding tab).
    – egmont
    Sep 17, 2023 at 19:46

2 Answers 2

2

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 horizontally, 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 create). 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

0
1

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.

0

You must log in to answer this question.

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