How to Position a Bash Terminal

📅 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

By default, gnome-terminal opens in the center of the Linux GUI desktop.

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.

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

Creates a taller terminal in the center of the desktop even though no coordinates are specified. Terminal position is automatically adjusted for centering when coordinates are missing.

 

gnome-terminal --geometry 80x50+0+0

Creates a default-sized terminal (80×24) flush with the upper left corner. Note that the terminal is positioned under the panel if it exists at the top. The terminal still follows GUI rules. The +0+0 takes into account any panels that might exist at that location to avoid concealing the title bar of the terminal.

gnome-terminal --geometry 80x1+300+0

This is the smallest height that we can create. Even though the option specified one row (80×1), it creates a minimum of two rows. The +300 means move it 300 pixels from the left, and the +0 means position the terminal flush at the top just below any panel that might exist.

gnome-terminal --geometry 80x30-150+300

If we use a negative coordinate for X as in -150, then the terminal window will be offset 150 pixels from the right of the desktop. The +300 still positions the terminal 300 pixels from the top.

gnome-terminal --geometry 80x30-150-400

And if we use a negative coordinate for Y as in -400, then the lower left corner of the terminal will be positioned 400 pixels from the bottom of the desktop (outside of any panels).

gnome-terminal --geometry 80x60-150-400

A terminal window will never be positioned off-screen. If the terminal window size is greater the given pixel distance from the specified edge, then the (X,Y) coordinates will be adjusted to fit the entire window on the desktop. In this example, the height of the window at 60 rows is greater than the 400 pixel Y offset, so the terminal is aligned flush with the bottom of the screen despite our wishes to align it 400 pixels above the bottom.

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:

Dynamically calculated (x, y) coordinates from a script are used to position this window.

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!

 

 

, ,

  1. Leave a comment

Leave a comment