57

So that I could resize the window to a certain size from within Terminal.

Dan J
  • 5,721

5 Answers5

86

Yes. Terminal supports escape sequences for manipulating windows, including the size and position, layering, and minimizing. Dimensions can be expressed in pixels or characters. See Xterm Control Sequences for details (search for “Window manipulation”; if you’re not familiar with the notation, “CSI” stands for “Control Sequence Introducer”, which is ESC [).

For example, this shell command will set the window to 100x50 characters:

printf '\e[8;50;100t'

Minimize the window for a few seconds, then restore it:

printf '\e[2t' && sleep 3 && printf '\e[1t'

Move the window to the top/left corner of the display:

printf '\e[3;0;0t'

Zoom the window:

printf '\e[9;1t'

Bring the window to the front (without changing keyboard focus):

printf '\e[5t'

Enabling the Control Sequences in Terminal Emulators

Some terminal emulators ignore these control sequences by default and require configuration to enable them.

To enable these in XTerm, set the following resource to true:

allowWindowOps

To enable these in iTerm2, deselect the following preference:

Preferences > Profiles > [profile] > Terminal > Disable session-initiated window resizing

Chris Page
  • 8,011
  • This is just perfect! – Maic López Sáenz Apr 06 '12 at 22:34
  • 3
    From now on you are my god! – d12frosted Jan 05 '15 at 07:43
  • 2
    This doesn't seem to work on Yosemite with iterm2 – Jistanidiot May 11 '15 at 20:35
  • This works great but it seems Terminal does not accept resizings that would make the window only partially visible on the screen. I just tried printf '\e[8;100;100t' and this truncated to 79x100, just what could be fit. My problem is that I need to bypass the mouse just for this case of enlarging Terminal beyond the screen limits ... – phs Oct 03 '15 at 08:22
  • @phs Why do you need the window to be larger than the screen? – Chris Page Oct 03 '15 at 09:23
  • There are many reasons. E.g. to not fold long lines. Or not have ncurses-based legacy code complain that the window is too small. Or you want to increase font size with 'Cmd +' and not change number of lines. Or .. Now, can Terminal be told to resize beyond screen limits? – phs Oct 03 '15 at 17:06
  • @phs Not via the escape sequences or by increasing the font size. Only the user resizing the window can do that. If you need to do that, consider filing a bug report: https://developer.apple.com/bug-reporting/ – Chris Page Apr 04 '17 at 23:39
  • @phs I should have asked a more specific question: why do you need to resize the window larger than the screen via escape sequences? Note that you should be able to use application scripting (e.g., using AppleScript) to layout windows, especially if you don't need to do it via escape sequences sent to the terminal through the tty device. – Chris Page Apr 04 '17 at 23:42
  • I already explained why the question is legit. I am happy with an answer like 'sorry Terminal.app can't do that' – phs Apr 05 '17 at 06:16
  • @phs I'm not questioning the legitimacy of the question, nor am I saying that Terminal can't do that. I'm asking for more detail to understand the requirements for your use-case. – Chris Page Apr 14 '17 at 00:27
  • 1
    This also works with iTerm2. – TextGeek Jun 01 '17 at 19:13
  • Does not work in my Terminal (macOS 10.14, Mojave). – Gab Apr 26 '19 at 09:59
  • @Gabriel That’s not enough information to go on. You should probably create another Q&A question detailing exactly what you did and what result you got. – Chris Page May 08 '19 at 13:22
  • I tried this command in Terminal: % printf '\e[8;50;100t'

    This is what I get on the command line (nothing else happens) e[8;50;100t%

    Since its not an XTerm, there are no configurations regarding escape sequences.

    However, the osascript command mentioned below works fine; just put it in a bash script.

    – Gab May 10 '19 at 12:20
  • @Gabriel create another Q&A asking how to get it to work, detailing exactly what you did, what you expected to happen as a result, and what result you got. Include the OS version. When I Copy and Paste the command from your comment, it works fine for me: it resizes the window to 100x50. – Chris Page May 10 '19 at 21:55
  • Awesome answer! – Zaya Sep 24 '19 at 04:08
  • I've been using the escape codes for setting 132 columns and 80 columns for years (starting from actual physical ANSI terminals!), but I had never seen this. What a revelation. – Mars Mar 13 '21 at 17:53
15

Use /usr/X11/bin/resize.

resize -s 30 80 will give you 30 rows and 80 columns.

resize -s 30 0 will give you 30 rows and full columns.

resize -s 0 80 will give you full rows and 80 columns.

lhf
  • 7,278
  • 1
    This solution is not limited to MacOS. It is terminal-based, so it should work on all terminals. I use this to resize PuTTY windows on Windows running bash shells with TERM=xterm. – DrStrangepork Dec 22 '14 at 17:44
  • 4
    This doesn't seem to work on Yosemite in iterm2. – Jistanidiot May 11 '15 at 20:35
  • This works for me with Apple Terminal but not iTerm2. However, sending the escape sequence \e[8;24;80t works with both. – TextGeek Jun 01 '17 at 19:18
  • Really? I can't seem to get that sequence (\e[8;24;80t) to work under iTerm2 (v3.1.5). – Krishen Greenwell Dec 13 '17 at 01:54
  • 1
    @KrishenGreenwell Refer to the iTerm2 documentation. By default it ignores these control sequences. There’s a preference to control whether they are ignored. – Chris Page Dec 21 '18 at 23:55
  • 2
    This solution used to work, but I think, the X11 commands are gone. Maybe, they can be installed by installing X11, but I don't want to install X11 just to have the resize command. – Gab Apr 26 '19 at 09:48
  • Can confirm that this solution unfortunately doesn't work on MacOS Ventura 13.0 – qwerty Feb 22 '23 at 17:19
10

You could always use AppleScript:

setwidth() { osascript -e "tell app \"Terminal\" to tell window 1
set b to bounds
set item 3 of b to (item 1 of b) + $1
set bounds to b
end"; }
Lri
  • 105,117
  • This only works for the currently active terminal window/tab. For this to reliably work, get the current tty device pathname and locate the terminal tab that matches. – Chris Page Dec 21 '18 at 23:53
  • See also https://superuser.com/a/576357 – lhf Apr 26 '19 at 11:35
2

My apologies for necro-reviving an old question, but I thought this answer might be useful for others.

From tedsmith3rd, here's a bash function to move a Terminal window. The magic of terminal command sequences.

function bumpNjump() {
  local xDimension="" yDimension="" width="" height="" OPTIND

while getopts 'x:y:w:h:' thisArg do case "${thisArg}" in x) xDimension="${OPTARG}" ;; y) yDimension="${OPTARG}" ;; w) width="${OPTARG}" ;; h) height="${OPTARG}" ;; esac done

if [ -n "${width}" -a -n "${height}" ] then printf '\e[4;'${height}';'${width}'t'; fi

if [ -n "${xDimension}" -a -n "${yDimension}" ] then printf '\e[3;'${xDimension}';'${yDimension}'t' fi }

Combine that with this termsize.bash script to get the current geometry, and you have an easy way to control your Terminal window.

#!/bin/bash
# based on a script from http://invisible-island.net/xterm/xterm.faq.html
exec < /dev/tty
oldstty=$(stty -g)
stty raw -echo min 0

printf "\e[13t" > /dev/tty IFS=';' read -r -d t -a pos

xpos=${pos[1]} ypos=${pos[2]}

Window (including chrome) size in pixels

printf "\e[14;2t" > /dev/tty IFS=';' read -r -d t -a size

hsize=${size[1]} wsize=${size[2]}

stty $oldstty

echo "bumpNjump -x $xpos -y $ypos -h $hsize -w $wsize"

Eljay
  • 171
  • This looks useful, though doesn't work for me on MacOS 10.5.6 - it prints out the control characters:

    bumpNjump -x 10 -y 20 -w 800 -h 300

    \E[4;300;800t\E[3;10;20t

    Does it work for you?

    – Scot Sep 24 '20 at 23:45
  • 1
    Ah - it works in iTerm if you disable the option shown in an earlier answer:

    To enable these in iTerm2, deselect the following preference:

    Preferences > Profiles > [profile] > Terminal > Disable session-initiated window resizing

    – Scot Sep 25 '20 at 00:10
-3

Actually you know, moving an resizing windows with a mouse is horribly slow.

I've been using this app SizeUp for a very long time now. It basically resizes any application window by using your keyboard command.

You can do the following (my custom keyboard commands below):

  • make the window full screen (control + option + command + m)
  • move a window 1/2 screen size to the left or right (control + option + command + / )
  • move a window 1/4 screen size to any corner (control + option + shift + ///)
  • move windows between screens
  • move windows between spaces

I think this might do the trick and also help with other window management.