7

I want to execute a command in a bash script that will resize the terminal window. In xterm

xterm -geometry=30x30

will create a new xterm window, and

lxterminal --geometry=30x30

will create a new lxterminal window.

Is there a way to resize either terminal without opening a new window? This is what it would look like:

if [ $TERMINALTYPE=xterm ]; then
    DASH="-"
else
    DASH="--"
endif

echo -e $TERMINALTYPE" "$DASH"geometry 30x30"

BUT: If there is a command that is a nice one-liner that will work with either terminal (vt100 escape sequence maybe) then that is even better.

Blue Ice
  • 381

3 Answers3

7

There are two steps to this. First, you need to tell xterm to allow it to happen at all, because by default it ignores requests by hosted programs to resize the window. Add this to your ~/.Xdefaults:

xterm*allowWindowOps: true

For this to take effect, you either have to log out, or run:

xrdb ~/.Xdefaults

Then launch a new xterm, and in your bash script:

echo -ne "\e[8;30;30t"
Paul
  • 7,194
4

Just want to share:

apt-get install xterm

Use the following resize command where 20 = no of rows, 40 = no of columns:

resize -s 20 40

Change the value of rows and columns as per requirement.

muru
  • 207,228
1

In addition to @Paul's correct answer:

You could try this without editing ~/.Xdefaults, by running:

xterm -xrm 'xterm*allowWindowOps: true'

Then, in new window:

printf "\e[8;%d;%dt" $[LINES+5] $[COLUMNS+5]

.Xdefaults or .Xresources

Depending on your installation, you may have to store this on .Xresources instead of .Xdefaults.

FILES

Xrdb does not load any files on its own, but many desktop environments use xrdb to load ~/.Xresources files on session startup to initialize the resource database, as a generalized replacement for ~/.Xdefaults files.

Then after

sed -e '$axterm*allowWindowOps: true' -i.bak .Xresources 
xrdb <.Xresources

Then...

Some bind samples

You could run this or add to your .bashrc:

# Some bind for 'Shift' + <direction> key and allowWindowOps resource
bind -x '"\e[1;2C"':'printf  "\e[8;%d;%dt" $LINES $((COLUMNS+5))'
bind -x '"\e[1;2D"':'printf  "\e[8;%d;%dt" $LINES $((COLUMNS-5))'
bind -x '"\e[1;2B"':'printf  "\e[8;%d;%dt" $((LINES+3)) $COLUMNS'
bind -x '"\e[1;2A"':'printf  "\e[8;%d;%dt" $((LINES-3)) $COLUMNS'

to be used with respectively: Shift+Right   ,   Shift+Left   ,   Shift+Down   or   Shift+Up

Note: As this in bind, this will work in interactive command environment (aka not under vim nor less, read or any work in progress)