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. – nonopolarity Jan 17 '13 at 7:38
  • Please see update above for how it happened in Fedora – nonopolarity Jan 17 '13 at 19:32
  • 4
    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
  • 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 unix.stackexchange.com/questions/105958/… – qneill May 2 '19 at 16:26

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
| improve this answer | |
  • 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
  • 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

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

| improve this answer | |
  • 3
    but it doesn't permanently solve the problem... – nonopolarity 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 – nonopolarity Dec 16 '16 at 10:29

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.

| improve this answer | |
  • 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
  • 1
    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

adding these options to docker exec seemed to solve my problem

| improve this answer | |
  • 1
    were you the one who asked the question 5 years ago ? – Archemar Mar 15 '18 at 8:16
  • 4
    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
  • Will probably break eventually: # Option ā€œ-eā€ is deprecated and might be removed in a later version of gnome-terminal.# – Zoe Jun 3 at 18:32

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.

| improve this answer | |
  • 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
  • 2
    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
  • Yes this is one way of doing it. And if not echo -e \\033c is another option. – Pryftan Dec 13 '19 at 16:14

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.

| improve this answer | |
  • 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

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

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

| improve this answer | |

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.

| improve this answer | |
  • 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 at 21:43

Try stty sane. it should do what you need.

| improve this answer | |
  • 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 at 14:48

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