9

I couldn't find anything with a quick Google search, nor anything on here, saving for this. However, it doesn't do the trick. So how exactly do you resize the terminal using Python?

asked Jun 20, 2011 at 23:23
3
  • Oh, right. I'm using a Linux (Ubuntu 10.10, if it matters). And now to display my ignorance: '*nix'? Commented Jun 20, 2011 at 23:38
  • 1
    hehe no problem en.wikipedia.org/wiki/Unix#Branding Commented Jun 20, 2011 at 23:41
  • What exactly do you mean by resize? Do you mean the pty/tty knowledge of the terminal size, or an actual terminal application (e.g. xterm) size (geometry)? Commented Jun 21, 2011 at 3:12

2 Answers 2

28

To change the tty/pty setting you have to use an ioctl on the stdin file descriptor.

import termios
import struct
import fcntl
def set_winsize(fd, row, col, xpix=0, ypix=0):
 winsize = struct.pack("HHHH", row, col, xpix, ypix)
 fcntl.ioctl(fd, termios.TIOCSWINSZ, winsize)

But to change the actual window size you can use terminal escape sequences, but not all terminals support or enable that feature. If you're using urxvt you can do this:

import sys
sys.stdout.write("\x1b[8;{rows};{cols}t".format(rows=32, cols=100))

But that may not work on all terminals.

answered Jun 21, 2011 at 3:35
Sign up to request clarification or add additional context in comments.

1 Comment

I can report that this works on Ubuntu's Terminal, but only if the Terminal app window is not maximized.
1

If you install xdotool, you can change the size of the terminal window with something like this:

import subprocess
import shlex
id_cmd='xdotool getactivewindow'
resize_cmd='xdotool windowsize --usehints {id} 100 30'
proc=subprocess.Popen(shlex.split(id_cmd),stdout=subprocess.PIPE)
windowid,err=proc.communicate()
proc=subprocess.Popen(shlex.split(resize_cmd.format(id=windowid)))
proc.communicate()

PS. On Ubuntu xdotool is provided by a package of the same name.

answered Jun 21, 2011 at 0:11

1 Comment

this seems resize x11 gui window, not resize (virtual) terminal window in pty

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.