114

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

7
  • What terminal emulator are you using? Sounds broken if reset doesn't resolve the issue.
    – jordanm
    Jan 17, 2013 at 5:54
  • whatever Terminal program inside of Fedora... and the default Terminal program on Mac OS X Lion. Jan 17, 2013 at 7:38
  • Please see update above for how it happened in Fedora Jan 17, 2013 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, 2013 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, 2019 at 16:26

11 Answers 11

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
7
  • 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, 2015 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 9, 2016 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, 2017 at 0:59
  • zsh: command not found: shopt . ??
    – Jdeep
    Nov 5, 2020 at 7:40
  • 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 5, 2020 at 19:19
26

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

2
  • 5
    but it doesn't permanently solve the problem... Jan 17, 2013 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, 2016 at 10:29
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.

2
  • 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, 2017 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. Sep 22, 2018 at 0: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.

7
  • So the OP mentioned that reset was not working for them, and this was in fact the point of the question.... Apr 4, 2017 at 4: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 6, 2017 at 10:29
  • reset works on Windows Putty with Ubuntu 16.04!
    – musbach
    Jul 5, 2017 at 18:36
  • 1
    reset worked on ubuntu 16.04 zsh
    – A.Wan
    Jul 9, 2018 at 21:52
  • 1
    Yes this is one way of doing it. And if not echo -e \\033c is another option.
    – Pryftan
    Dec 13, 2019 at 16:14
15

adding these options to docker exec seemed to solve my problem

-e COLUMNS=$COLUMNS -e LINES=$LINES -e TERM=$TERM
2
  • 1
    were you the one who asked the question 5 years ago ?
    – Archemar
    Mar 15, 2018 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, 2018 at 9:44
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
  • 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, 2018 at 18:28
6

Try stty sane. it should do what you need.

2
  • 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, 2019 at 16:13
  • This messes up keybindings of the emcs
    – alper
    Jul 17, 2020 at 14:48
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.

2
  • 1
    CentOS doesn't seem to have resize - use "stty rows 66" to do the trick.
    – Russ
    Oct 24, 2019 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!. Apr 27, 2020 at 21:43
4

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

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.

2

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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .