📅 January 20, 2024
“I want to be fancy and open a terminal at a specific (x, y) coordinate.”
When we open a Bash terminal, such as gnome-terminal, it will open smack dab in the center of the screen. This is fine for a single terminal, but when opening several, they stack on top of each other (at least they do in Linux Mint).
Is there a way to manually specify where a terminal opens in addition to its size?
Indeed there is!
gnome-terminal
gnome-terminal (as well as xterm) supports the – -geometry option.
gnome-terminal --geometry COLUMN_COUNTxROW_COUNT+Xcoordinate+Ycoordinate xterm -geometry COLUMN_COUNTxROW_COUNT+Xcoordinate+Ycoordinate
Note: xterm uses a single dash in -geometry while gnome-terminal option uses two dashes in – -geometry.
- COLUMN_COUNTÂ – Terminal width in characters. The number of columns the terminal will have. Default is 80 columns.
- ROW_COUNT – Terminal height in characters. How many rows the terminal will allow before scrolling begins. Default is 24 rows.
- Xcoordinate – The X offset in pixels from the upper left corner of the desktop.
- Ycoordinate – The Y offset in pixels from the upper left corner of the desktop.
The (X, Y) coordinates position the upper left corner of the terminal on the desktop in pixels, not characters. By default, coordinates originate from the upper left corner of the desktop minus an panels. By using negative values, we can position from the lower right corner of the desktop minus any panels.
Usage Examples
These commands can be executed from another terminal or from a script. A new terminal window will open. It does not reposition the current terminal.
gnome-terminal --geometry 80x24
Create a default terminal in the center of the desktop. When no coordinates are specified, the terminal will be centered automatically.
gnome-terminal --geometry 80x50
gnome-terminal --geometry 80x50+0+0
gnome-terminal --geometry 80x1+300+0
gnome-terminal --geometry 80x30-150+300
gnome-terminal --geometry 80x30-150-400
gnome-terminal --geometry 80x60-150-400
Auto-Calculate Dimensions
“How can I get the screen resolution to position a terminal for various displays?”
This might be needed if your script is ported to systems with monitors of varying resolutions. Rather than hard-coding coordinates that might exceed those of smaller displays, let the script compute where to put a terminal window…or two or three. There are a number of ways to get the monitor resolution from the terminal, but none of them are straightforward. The idea is the grab the width and height and store them into variables that are then used as part of the gnome-terminal geometry options.
xrandr | fgrep '*'
This is probably the easiest technique for modern Linux systems. It requires xrandr and passes the result to fgrep so it returns the only line containing an asterisk, which contains the monitor resolution and refresh rate. You should see a line similar to this:
2560x1440 59.97*+ 49.99
We are not finished. All we did was get the monitor resolution and refresh rate. (Current refresh rate is indicated with the asterisk.) Any desktop panels are not taken into account. We need to parse the string to extract just the width and height. Here is one way in a script:
#!/bin/bash width=$(xrandr | fgrep '*' | awk '{print $1}' | cut -d'x' -f1) height=$(xrandr | fgrep '*' | awk '{print $1}' | cut -d'x' -f2) echo " WIDTH: $width" echo "HEIGHT: $height"
The variables $width and $height will contain the width and height returned by xrandr. To actually use the values with the geometry, we can do something like this in a script:
#!/bin/bash width=$(xrandr | fgrep '*' | awk '{print $1}' | cut -d'x' -f1) height=$(xrandr | fgrep '*' | awk '{print $1}' | cut -d'x' -f2) gnome-terminal --geometry 80x24+$(( $width / 8 ))-$(( $height / 2 ))
Bash math using $(( )) will return integer results rounded off, which is what we want since pixel offsets must be whole numbers. The example above divides the X resolution by 8 to position the window that many pixels from the left while the Y resolution is divided by 2 to position the terminal from the bottom (approximately center vertically from the lower left corner of the terminal). The result is this:
Regardless of using hard-coded values or Bash math, trial and error is still required to make sure the terminal is positioned as desired. Personally, hard-coded values work fine and avoid added complexity since the window will always be adjusted to show its entirety on the desktop.
Conclusion
The geometry option is a very convenient feature of gnome-terminal to control the position of a terminal window — especially when launched from a script. A script can open multiple windows and position each in the same location based upon unique size and coordinates for each.
Have fun!