How to solve the issue that a Terminal screen is messed up? (usually after a resizing)

Sometimes, a terminal screen is messed up, and when we use man ls to read the manpages, or press the UP arrow to go to previous commands in history, the screen will show characters not as the right place. (for example, treat the end of screen as some where in the middle of the screen).

The command reset is tried and it wouldn’t work. One way that works is to log out or close the window, and resize the window first, and then do ssh (or close that tab, and resize the window, and then open a new tab to get a new shell).

But this way, we will lose anything that we previously did, such as starting a virtual machine console, etc. So if we don’t close the shell, is there a way to fix this problem?

(this happened before right inside Fedora, and also for a Macbook ssh into a RHEL 5.4 box).

Update: I remember now how it happened in Fedora: I opened up a Terminal, and did a FreeVM to use a console of a Virtual Machine (a shell). I think it was size 80 x 25 and then after a while, I resized the Terminal to 130 x 50 approximately, and then the “inner shell” (of the VM) started to behave weird).

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

If you are using bash, check if “checkwinsize” option is activated in your session using

shopt | grep checkwinsize

If you don’t get

checkwinsize    on

then activate it with

shopt -s checkwinsize

Bash documentation says for “checkwinsize” attribute :

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

If you like the setting, you could activate checkwinsize in your ~/.bashrc.

  • To activate: shopt -s checkwinsize
  • To deactivate: shopt -u checkwinsize

Method 2

You can try Ctrl+L. It clears and/or redraws the terminal screen, depending on the program.

Method 3

I had the same problem and none of the above recipes worked for me because I believe that my bash never receives the SIGWINCH signals, which are trapped by its parent process.

I finally found a solution. I added to my .bashrc:

export PROMPT_COMMAND="resize &>/dev/null ; $PROMPT_COMMAND"

Now every time I get a new prompt, my window is re-adjusted.

Thanks to UKmonkey for the PROMPT_COMMAND improvement.

Method 4

adding these options to docker exec seemed to solve my problem

-e COLUMNS=$COLUMNS -e LINES=$LINES -e TERM=$TERM

Method 5

I face same problem from time to time using zsh on macOS. A simple invocation of reset command sets up the terminal to my liking again.

Method 6

I just wanted to add to what Arcadien had already mentioned. The checkwinsize enabling does the trick, but for me, what was needed was to reset the size of the window for it to work properly. I guess the checkwinsize was meant to eliminate this, but still, worth a shot. Just try changing the size of the window or un-maximizing and maximizing it post this option.

Method 7

Try stty sane. it should do what you need.

Method 8

I had the same problem as you and this is what I did:
I have a .profile setup in my user, so that is where I do all my changes.

There is a package called xterm which is available through apt-get I don’t know about yum. But I did a local install from source as I do not have installation privileges. LINK: http://invisible-island.net/xterm/#download

./configure --prefix=/the/path/you/want/to/install/to
make
make install

I exported the path in my profile and invoked it at the same place

export PATH=$PATH:/the/path/you/want/to/install/to
resize

So now every time I log in, the terminal size is set accordingly by resize.

Method 9

Well, that’s a common problem and there should be a simple solution.

If other measures like Ctrl + A Ctrl + L don’t work, try using stty:

  1. Ctrl + T: Open a new tab with a local shell.
    Or: Leave screen (Ctrl + A D to detach) and log out of your SSH session.
  2. stty -a | grep rows: Get the number of rows and columns.
    Example: speed 38400 baud; rows 33; columns 133; line = 0;
  3. Go back to your remote tab or log in and reattach screen. The stty output should be different.
    Example: speed 38400 baud; rows 47; columns 177; line = 0;
  4. Type in stty rows N and stty columns N where N is the “real” number (outside of screen).
    Example: $ stty rows 33; stty columns 133

If you use Terminator, you can skip the first three steps because it shows the number of columns and rows at the top.

You don’t have to resize your terminal windows to run into this. Admins usually log in from different computers and when the attached monitors differ in size (resolution), a window that’s maximized will have a different size on each computer. It’s actually rather difficult not to run into this frequently.

And the result isn’t just that some man pages aren’t showing up properly. It’s pretty much anything that uses a pager – the first few lines are often missing (for example). And don’t even think about using VIM in such a misaligned terminal. You want to highlight line 3 (V) in order to replace it, but when you do, the highlighted text is that of another line.

Method 10

if you are using expect

add this to the beginning of your script will work

#let pty slave device aware of WINCH signal
trap {
 set rows [stty rows]
 set cols [stty columns]
 stty rows $rows columns $cols < $spawn_out(slave,name)
} WINCH

more details and explanations at
https://unix.stackexchange.com/a/668254/109036

Method 11

Run resize after resizing your terminal window. It will update COLUMNS and ROWS to the correct values.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x