Cygwin bash prompt is wrapping lines on the same line

58

19

I'm using Cygwin bash prompt, and for long commands the text will wrap around on the same line as opposed to going to the next line despite setting my PS1 to simply ' $'.

Here's a screenshot,
screenshot

wting

Posted 2011-05-14T00:17:24.683

Reputation: 1 022

12What's the TERM environment variable set to? For the Cygwin console, it should be cygwin. – ak2 – 2011-05-14T05:55:28.050

1@ak2 this fixed the problem for me, thank you. Cygwin on Mintty. – JoshuaD – 2013-11-20T17:55:32.360

Answers

61

I was already using MinTTY, and removing the newline in PS1 didn't help either. One piece of advice on this page did help. I executed this bash command:

kill -WINCH $$

In my case, running this once fixed the issue, even after logging out and back in. I'm not sure if this is always the case.

jtpereyda

Posted 2011-05-14T00:17:24.683

Reputation: 2 079

7@ivan_pozdeev, I just found you only have to do this is you resize while vim is open: vim gets the signal and redraws at the new size, but it isn't passed to its parent process and thus bash still thinks the size of the screen is whatever it was when vim opened. – akatakritos – 2015-01-16T16:29:33.260

this also worked for me – konqui – 2015-12-16T14:47:23.603

This worked for me as well, thanks @jtpereyda ! – Jason R. Mick – 2016-07-05T14:04:26.513

Thanks, this was definitely the problem for me--resizing the terminal while in vim. I feel like it should be easy enough to fix this bug but I don't know. – Iguananaut – 2017-01-11T14:08:57.670

@akatakritos I thought that bash would use the same values as displayed by stty size, is it possible that Cygwin's bash is behaving different in that regard compared than on Linux? – phk – 2017-03-29T20:54:16.563

Using "reset" failed to fix the problem, but this kill worked. I also believe I resized while in vim. – Fixee – 2019-02-20T17:58:28.080

Unbelievably simple and persistent command for such a long vexing problem! – radke – 2019-09-05T13:55:28.983

Link is broken. What is the issue, what does the specified command do, and how does it fix it? And why doesn't msys2 (or whatever) fix the bug after all these years? – cp.engr – 2020-01-30T02:54:30.700

1Judging by -WINCH, this signals the bash process that the terminal window was resized. So this should be done after each terminal window resize, I guess. – ivan_pozdeev – 2013-09-26T23:18:51.783

22

For me, the solution was to add the following lines to .bashrc:

PS1='\[\e[32m\]\u@\h:\W> \[\e[0m\]'
TERM=cygwin
export PS1
export TERM

Note that non-printable characters in the prompt must be enclosed in \[ ... \].

digory doo

Posted 2011-05-14T00:17:24.683

Reputation: 342

7As mentioned by @ak2 in a comment under original question, export TERM=cygwin is sufficient to fix the issue. – dregad – 2014-09-30T12:07:27.963

1it wasn't sufficient in my case. if PS1 contains escape sequences that aren't enclosed in \[...\], the wrapping problem would persist. setting the TERM env variable may be sufficient in your case, but i doubt it. – digory doo – 2014-10-01T07:22:05.170

For me this fixes the problem that the second line overwrites the first line, however unless I use exactly 80 width terminal, the cursor position and text offset are still wonky (using cygwin64 , mintty 2.3.7) – M.M – 2016-06-01T01:24:44.110

Adding \[ ... \] fixed problem for me. – Trismegistos – 2019-03-28T10:06:39.560

8

I had the same problem with MinTTY as well. The problem probably has something to do with the primary prompt (PS1).

The solution for me was removing the last 'new line' character from PS1 (right before the '$' sign):

user@host ~
$ echo $PS1
\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[33m\]\w\[\e[0m\]\n\$

user@host ~
$ export PS1='\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[33m\]\w\[\e[0m\]\$ '

user@host ~ $

see http://cygwin.com/ml/cygwin/2001-07/msg00140.html for reference.

To make this change persistent, add export PS1='[\e]0;\w\a]\n[\e[32m]\u@\h [\e[33m]\w[\e[0m]\$ ' to your ~/.bashrc file.

Yariv

Posted 2011-05-14T00:17:24.683

Reputation: 246

This did work for me, but in addition to removing the last newline I had to also restart the Cygwin terminal. – christosc – 2015-07-10T09:44:39.353

1Didn't work for me... – HDave – 2014-03-26T19:36:26.477

6

@jtpereyda's answer is certainly on the mark. But for some reason I couldn't let this go, and dug a little deeper.

Expanding on this comment, if you resize the terminal while in vim (or any other full screen application that takes control of the tty away from the shell), the resulting SIGWINCH is often not sent to the shell, so when it gets back control it doesn't know that the terminal has been resized.

When you resize your terminal it should call an ioctl(..., TIOCSWINSZ, ...) on the master pty that vim is running in. This in turn results in a killpg(SIGWINCH) on vim's process group.

The problem is that vim runs in its own process group distinct from the shell it was exec'd from, so the bash shell does not receive the SIGWINCH and does not adjust its lines/columns appropriately.

If you want a permanent workaround, add shopt -s checkwinsize to your .bashrc. That makes bash check the window size (ioctl(..., TIOCGWINSZ, ..)) after returning from each command, and update its lines/columns.

Iguananaut

Posted 2011-05-14T00:17:24.683

Reputation: 161

What's vim go to do with the question? OP is not using vim. – DavidPostill – 2017-01-11T16:11:18.360

2I meant to reference a different question that I think made the connection more obvious, but in short a possible cause of the OP's problem is opening a full terminal application like vim, resizing the terminal, and then exiting. As I explained the SIGWINCH is not seen by the shell so when you exit vim it still thinks the terminal is the previous size, resulting in various line wrapping issues. – Iguananaut – 2017-01-11T19:47:41.907

5

As commented by dregad and ak2, setting export TERM=cygwin in my ~/.bashrc file was enough to fix this problem for me.

blong

Posted 2011-05-14T00:17:24.683

Reputation: 161

2

Something is broken in your terminal settings (probably).
I guess you would have already tried exiting that session and restarting a new one.

While you don't get a solution for the Cygwin terminal, give MinTTY a try (its actually better).

nik

Posted 2011-05-14T00:17:24.683

Reputation: 50 788

Note that MinTTY is the default terminal for Cygwin since late 2011.

– Hugh W – 2017-03-22T15:43:34.417

1I see this problem in Cygwin across multiple machines, but MinTTY looks better and solves the wrapping problem. Two birds with one stone! – wting – 2011-05-14T21:10:43.243

1

As commented by akatakritos, you probably resized your terminal while vim was open.

When this happens, just resize the terminal one more time and the issue goes away.

olivieradam666

Posted 2011-05-14T00:17:24.683

Reputation: 111

thx! although i'm not using cygwin, this fixed the "wrapping on the same line" problem for me in bash--just unmaximize the terminal window, then maximize it again and the problem is gone :) – Nick Humphrey – 2019-06-13T08:46:15.460