64

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).

  • What terminal emulator are you using? Sounds broken if reset doesn't resolve the issue. – jordanm Jan 17 '13 at 5:54
  • whatever Terminal program inside of Fedora... and the default Terminal program on Mac OS X Lion. – 太極者無極而生 Jan 17 '13 at 7:38
  • Please see update above for how it happened in Fedora – 太極者無極而生 Jan 17 '13 at 19:32
  • 3
    The magical incantation is ^Jreset^J, where ^J means pressing ctrl-J. Or (on graphical terminals) you can try the Reset button. Also, some programs just aren't prepared for terminal sizes changing underneath them (yes, ancient software is alive and kicking) or just misbehave badly when the terminal gets too small. – vonbrand Jan 21 '13 at 19:29
  • @vonbrand: after not finding that in any answer, I'd suggest you post it as one. This has helped me many times. Personally I found it on commandlinefoo. – 0xC0000022L Mar 23 '15 at 22:45
81

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
  • 5
    This doesn't happen to solve my problem, but this is a good general answer, so +1 anyway. I think my problem relates to Windows emulation(?) when ssh-ing into a Linux server. – geneorama Mar 19 '15 at 15:21
  • At first it seemed it didn't work when I used arrows up to see the history, but just after the first executed command it fixed everything. Using terminator in my case. Thanks +1 – kstenger Feb 9 '16 at 17:53
  • this worked for me, but only by turning off checkwinsize. for some reason LINES=24 was being set after each command even though my window (containing a picocom session) was much taller. – Michael Aug 9 '17 at 0:59
18

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

  • 2
    but it doesn't permanently solve the problem... – 太極者無極而生 Jan 17 '13 at 7:37
  • I think I meant was, if you press CTRL L, then the line is ok for the moment, but if you edit the line again (or on the next command line you type), then the problem is still there – 太極者無極而生 Dec 16 '16 at 10:29
8

adding these options to docker exec seemed to solve my problem

-e COLUMNS=$COLUMNS -e LINES=$LINES -e TERM=$TERM
  • 1
    were you the one who asked the question 5 years ago ? – Archemar Mar 15 '18 at 8:16
  • 2
    I can tell you he's not: "docker" doesn't really fit with "5 years ago". Docker wasn't yet released in january 2013 ;-) – jplandrain Aug 22 '18 at 9:44
7

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.

  • 2
    I personally would go with export PROMPT_COMMAND="resize &>/dev/null; $PROMPT_COMMAND" so that the old prompt command, whatever it my be will be preserved – UKMonkey Nov 23 '17 at 10:34
  • @UKMonkey: I updated the reply. Thanks! – Anthony Scemama Nov 23 '17 at 15:50
  • I would just run resize manually when needed. Interrogating the console for size can be slow. Doing it every command seems excessive, and usually you know when it has been resized. – Conrad Meyer Sep 22 '18 at 0:07
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.

  • 1
    Believe it or not, this was all it took for mine to be fixed. Funny because I tried everything else first. – taranaki Nov 30 '18 at 18:28
4

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.

  • So the OP mentioned that reset was not working for them, and this was in fact the point of the question.... – Stephen Rauch Apr 4 '17 at 4:00
  • OP has mentioned about it not working on Linux flavours. I faced the issue on MacOS Sierra and reset worked in this case. I added this answer to help anyone, who faces same problem on a MacOS and is not aware about reset command. – nonocut Apr 6 '17 at 10:29
  • reset works on Windows Putty with Ubuntu 16.04! – musbach Jul 5 '17 at 18:36
  • reset worked on ubuntu 16.04 zsh – A.Wan Jul 9 '18 at 21:52
2

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.

0

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

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.

protected by Community Mar 11 at 17:37

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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