1

From what I understand, the "traditional" behaviour of terminal emulators is to chop long lines when the window is made narrower, while some terminals rewrap their contents on resize.

Is it possible for a Bash script (or as a last resort, a C program) to detect which of the two behaviours the terminal uses?

  • Passively or actively? I don't think there's a terminfo setting for that, but it should be detectable by displaying stuff, causing a resize and reading back from the screen. – Gilles Feb 28 at 20:51
  • @Gilles: Augh! That sounds really messy. So chopping Vs. rewrapping is a built-in non-changeable design decision made by each terminal software developer? – forthrin Mar 1 at 7:53
  • Yes, wrapping isn't a traditional feature. And those two aren't the only possible behaviors, I think some terminals not only remember the content of “chopped” lines but actually do allow setting the cursor position wherever there has been output, even if the target column is larger than the current width. – Gilles Mar 1 at 10:41
  • @Gilles: This is interesting to know. Thanks! So if you have some terminal content like an ASCII GUI, it's sort of impossible to gracefully handle resizing of terminal width, because you have no way of telling what your terminal does with lines when you resize? – forthrin Mar 1 at 15:22
-1

Why not just set the behavior if you are using bash?

For example, there are a few options:

shopt -s checkwinsize

will tell bash to checkwinsize and act appropriately. The man page notes:

If set, bash checks the window size after each command and, if necessary, updates the values of LINES and COLUMNS.

You can also check the number of columns and lines and then set them:

echo $COLUMNS; echo $LINES

If they are not what you want, you can set them:

COLUMNS=75;
LINES=20;
export COLUMNS LINES;

You can do all of that from within a bash script.

  • So you mean the terminal can be configured from the command line to either wrap lines or cut lines? I thought this was a built-in, non-changeable thing that varied for each terminal software. As per your suggestion, I tried shopt -s checkwinsize and shopt -u checkwinsize. My terminal wraps line in both cases. Further ideas? – forthrin Feb 28 at 17:10
  • @forthrin referencing this for detection: unix.stackexchange.com/questions/281661/… I am wondering, are you on a terminal in X windows (not sure that matters, but that is where I am testing). I get the feeling from your comment, you want to cut text off... see this page for an example: superuser.com/questions/188865/… – number9 Feb 28 at 17:20
  • I'm on macOS using the built-in Terminal, but I need a solution that works for any UNIX terminal. tput rmam makes the cursor return to the start of the same line of you type to the end of the line. However, if I resize the window horizontally, the lines are wrapping. It's this behaviour I'm interested in detecting (or setting, if setting it is even possible.) – forthrin Feb 28 at 17:24
  • @forthrin ahh. I do not have access to a mac to completely verify. That is now an interesting problem though.. I was under the impression shopt -s checkwinsize would take care of the terminal being resized... but in "Terminal" I am not sure if that works. I am assuming you need it to work in Mac "Terminal" also? – number9 Feb 28 at 17:59
  • 1
    @Hashim LINES and COLUMNS work the other way round: they provide information about the terminal size to applications. – Gilles Mar 28 at 12:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.