Score:-2

Ubuntu Terminal Default Positions

in flag

As many of you know, whenever you open a new terminal window on Ubuntu it gets located in the upper left part of the screen. The next one is located right below it. The third one is in the upper right, the fourth one in the bottom right part of the screen. I want to add that functionality to another Linux distribution which I'm using now. I just want to know how this is handled in Ubuntu.

Also that doesn't only work with gnome-terminal. It works with any terminal emulator that I've downloaded so far. So I highly think it's not an issue specific to the terminal emulator. Does anyone know how this is handled?

Score:2
it flag

RECOMMENDATION

I would recommend that you learn the tool you will be moving to. Almost all terminals have some sort of config file such as alacritty. That said, here are a few "tricks" which I've happened upon along the way."

HOW I DO IT

When you launch a terminal, there is an option which lets you set both it's size and it's X and Y offsets.

The option is --geometry, and it is available on many (most?) linux terminal emulators. Gnome terminal is no exception. As for an always solution, some developers just don't take the time to build in extra programmer/ superuser automation hooks..

The syntax to use the geometry option with our terminal is as follows:

$ gnome-terminal --geometry WxH+X+Y

Note that in the argument string, everything but the capitals is to be interpreted literally. The caps represent width measured in columns, Height measured in rows, and X & Y offsets from screen position (0,0).

I used to write out an command to save it to $HOME,the save to ~/.bashrc, which persists it. This is an idea I believe I got from this same site a long time ago, but now I use a method of my own devising when I need to script a script the functionality.

As a quick side-note: If i am changing myself in Ubuntu, I just create (or change to) another terminal profile. Each new l profile you make in the terminal gui "preferences" settings lets you specify the terminal's b-g color, transparency, etc. along with the previously mentioned settings.

I will also open vim and print the above command to a single line. Next, I will copy it vertically as many times as needed and configure each to a convenient size / position.

Finally I open a split with ~/.bash-aliases loaded to it's buffer and set an alias for each instance of the command.

Then I can just put a 3-4 letter name, like maybe midl in each time I need a guit terminal new process to launch in the middle of the screen.

Score:1
in flag

Thank you Nate. I used the idea you gave me and came up with that script that solves my problem. It's a python script that's specifically designed for my resolution which is 1920x1080. It also depends on a tool called wmctrl. Here is the script:

#!/usr/bin/python3

import os


def eliminate(iter):
    res = []
    for i in iter:
        if i:
            res.append(i)
    return res


workspace = "wmctrl -d | grep '*' | cut -d ' ' -f1 > /tmp/curr_ws"

os.system(workspace)
out_workspace = ""
with open("/tmp/curr_ws","r") as f:
    out_workspace =  f.read()
out_workspace = int(out_workspace)
print("DEBUG: Current workspace:", out_workspace)

positions = 'wmctrl -lG | grep "kali:" | grep "^........... {0}" | tr -s " " | cut -d " " -f 3,4 > /tmp/positions'.format(out_workspace)
sizes = 'wmctrl -lG | grep "kali:" | grep "^........... {0}" | tr -s " " | cut -d " " -f 5,6 > /tmp/sizes'.format(out_workspace)

os.system(positions)
out_positions = ""
with open("/tmp/positions","r") as f:
    out_positions =  f.read()
out_positions = eliminate(out_positions.split("\n"))

os.system(sizes)
out_sizes = ""
with open("/tmp/sizes","r") as f:
    out_sizes = f.read()
out_sizes = eliminate(out_sizes.split("\n"))

terminal_positions = [[110, 101, 854, 479], [973, 101, 854, 479], [110, 611, 854, 479], [973, 611, 854, 479]]
terminal_strings = ["gnome-terminal --geometry 105x25+100+45", "gnome-terminal --geometry 105x25+963+45",
                    "gnome-terminal --geometry 105x25+100+555", "gnome-terminal --geometry 105x25+963+555"]

parsed_positions = []
for i in range(0,len(out_positions)):
    parsed_positions.append([ int(out_positions[i].split(" ")[0]),int(out_positions[i].split(" ")[1]),int(out_sizes[i].split(" ")[0]),int(out_sizes[i].split(" ")[1]) ])
print("DEBUG: Positions:", parsed_positions)

def check(term_pos, parsed_pos):
    index = 0
    overlap = False
    for i in terminal_positions:
        overlap = False
        for j in parsed_positions:
            ax0 = term_left_start = i[0]
            ay0 = term_up_start = i[1]
            ax1 = term_left_end = i[0] + i[2]
            ay1 = term_up_end = i[1] + i[3]
            
            bx0 = pars_left_start = j[0]
            by0 = pars_up_start = j[1]
            bx1 = pars_left_end = j[0] + j[2]
            by1 = pars_up_end = j[1] + j[3]

            # if term_left_start >= pars_left_end or term_left_end <= pars_left_start or term_up_end <= pars_up_start or term_up_start >= pars_up_end:
            #     overlap = True

            if ((bx0 <= ax0) and (ax1 <= bx1)) or ((ax0 <= bx0 <= ax1) and (ax1 <= bx1)) or ((ax0 >= bx0) and (ax1 >= bx1 >= ax0)) or ((ax0 <= bx0 <= ax1) and (ax1 >= bx1 >= ax0)):
                if ((by0 <= ay0) and (ay1 <= by1)) or ((ay0 <= by0 <= ay1) and (ay1 <= by1)) or ((ay0 >= by0) and (ay1 >= by1 >= ay0)) or ((ay0 <= by0 <= ay1) and (ay1 >= by1 >= ay0)):
                    overlap = True

        if overlap:
            index += 1
        else:
            return index


    return -1


place = check(terminal_positions, parsed_positions)

if place == -1:
    os.system("gnome-terminal")
else:
    os.system(terminal_strings[place])

You have to include the location of the script in your PATH variable then assign a shortcut to a command that executes the script

Nate T avatar
it flag
Excellent! It looks good. The only small thing I see is _maybe_ logic for getting the length and width. The way it looks, they the operation and order may be backward. If I'm right, you'll never notice it so long as the low numbers x0 and y0 have values of zero. However, if this value moves in either direction, it will cause the display to go off. If this happens, just switch the operand-positions in the operator.
Nate T avatar
it flag
For example, `x[0] + x[1]` would be changed to `x[1] - x [0]`. I'm most likely wrong, since I've only looked at it for a couple minutes and you've probably been staring at it for who knows how long. I just knew if I didn't say something I would forget about it. And as soon as I didn't say anything, it would be right. So take it with a grain of salt. Not trying to break out into a code review, I just saw a maybe and thought I would say something. You should look into modifying to take arguments (maybe a device.conf?) And see about pack/ pub.
Nate T avatar
it flag
It solves a unique problem. One device preconfigured across several distros. I'm sure there's a tool out there already, because there are so many. But since there are so many, nobody knows about most of them, so the more the better! ...? ...! I
Muhammed Özen avatar
in flag
Thank you so much for the suggestion. Yes you are right and honestly I couldn't find any script that does that. I'm sure there's a lot out there but I never ran into any of them. The script also has another bug which is whenever I bring down the terminal but not close it, it'll assume it's still there and in order to prevent overlapping, it'll open the terminal in the next position. I have no idea about how to solve it but thank you so much for your contribution
Nate T avatar
it flag
There is a database in linux systems called `terminfo` or something similar which stores all of this info. It probsbly ends in a '.d'. I guarantee your answer will be there.
Muhammed Özen avatar
in flag
Thank you a lot again. I'll check it out
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.