115

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

nonopolarity
  • 3,069
  • What terminal emulator are you using? Sounds broken if reset doesn't resolve the issue. – jordanm Jan 17 '13 at 05:54
  • whatever Terminal program inside of Fedora... and the default Terminal program on Mac OS X Lion. – nonopolarity Jan 17 '13 at 07:38
  • Please see update above for how it happened in Fedora – nonopolarity Jan 17 '13 at 19:32
  • 7
    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
  • 1
    My problems stemmed from bash miscalculating the length of the prompt because it had color codes in it; I was missing the escape characters \[ and \] - see https://unix.stackexchange.com/questions/105958/terminal-prompt-not-wrapping-correctly – qneill May 02 '19 at 16:26
  • I don't think this warrants an answer but... Instead of reset you can also try echo -e \\033c. I have also noticed that if you open up a curses program (e.g. vim) and then quit it restores the terminal. The former I got from the keyboard and console how-to a while ago and the latter I discovered on my own. – Pryftan Dec 13 '19 at 16:17

11 Answers11

125

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
Arcadien
  • 1,585
  • 7
    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
  • 1
    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 09 '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 09 '17 at 00:59
  • zsh: command not found: shopt . ?? – Jdeep Nov 05 '20 at 07:40
  • @NoahJ.Standerson as stated, this is for bash, not zsh – Arcadien Nov 05 '20 at 18:01
  • 1
    @Arcadien . I actually found the solution . It was not zsh's fault but my theme powerlevel10k that show some info on the right side. It was caused because of that . If anyone facing the same problem see this – Jdeep Nov 05 '20 at 19:19
  • By the way, checkwinsize is enabled by default in Bash 5 – M Imam Pratama Oct 31 '21 at 07:26
  • This doesn't work for tmux panes. – Martin Braun Jan 24 '24 at 11:14
26

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

bahamat
  • 39,666
  • 4
  • 75
  • 104
19

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.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
  • 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
  • 2
    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 00:07
16

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.

nonocut
  • 261
  • So the OP mentioned that reset was not working for them, and this was in fact the point of the question.... – Stephen Rauch Apr 04 '17 at 04:00
  • 3
    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 06 '17 at 10:29
  • reset works on Windows Putty with Ubuntu 16.04! – musbach Jul 05 '17 at 18:36
  • 1
    reset worked on ubuntu 16.04 zsh – A.Wan Jul 09 '18 at 21:52
  • 1
    Yes this is one way of doing it. And if not echo -e \\033c is another option. – Pryftan Dec 13 '19 at 16:14
  • reset didn't work for me on Ubuntu 18.04 running Byobu. echo -e \033c worked. @Pryftan how does this work? – Simon Bagley Sep 09 '20 at 13:42
  • If using zsh, then ttyctl -f will help with automatically applying a reset when you return to a prompt. – Phil P Dec 03 '20 at 00:41
15

adding these options to docker exec seemed to solve my problem

-e COLUMNS=$COLUMNS -e LINES=$LINES -e TERM=$TERM
Oran Agra
  • 151
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.

saketrp
  • 1,323
  • 1
  • 10
  • 9
  • 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
6

Try stty sane. it should do what you need.

phil294
  • 905
  • This is helpful some of the time true. It sometimes doesn't help all the situation though. It will incidentally turn on echo as if you did stty echo. – Pryftan Dec 13 '19 at 16:13
  • This messes up keybindings of the emcs – alper Jul 17 '20 at 14:48
5

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

4

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.

basic6
  • 6,255
  • 1
    CentOS doesn't seem to have resize - use "stty rows 66" to do the trick. – Russ Oct 24 '19 at 13:25
  • Thanks man! I had a nasty issue with any ncruses programs used in gnu-screen. Your trick with stty rows 33; stty columns 133 solved my issue!. – Serafin Rusu Apr 27 '20 at 21:43
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.

TPS
  • 2,481
2

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

rustyx
  • 319
  • This is a good answer, and the only one that worked for me, using less in a remote SSH console. – davidA Jan 27 '23 at 02:34