65

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

2
  • 13
    What's the TERM environment variable set to? For the Cygwin console, it should be cygwin.
    – ak2
    May 14, 2011 at 5:55
  • 1
    @ak2 this fixed the problem for me, thank you. Cygwin on Mintty.
    – JoshuaD
    Nov 20, 2013 at 17:55

7 Answers 7

68

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.

8
  • 2
    Judging 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. Sep 26, 2013 at 23:18
  • 10
    @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. Jan 16, 2015 at 16:29
  • this also worked for me
    – konqui
    Dec 16, 2015 at 14:47
  • 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
    Jan 11, 2017 at 14:08
  • @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
    Mar 29, 2017 at 20:54
28

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 \[ ... \].

5
  • 8
    As mentioned by @ak2 in a comment under original question, export TERM=cygwin is sufficient to fix the issue.
    – dregad
    Sep 30, 2014 at 12:07
  • 2
    it 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
    Oct 1, 2014 at 7:22
  • 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
    Jun 1, 2016 at 1:24
  • Adding \[ ... \] fixed problem for me. Mar 28, 2019 at 10:06
  • 1
    would it be possible to add some information/background on what this does?
    – CervEd
    Jun 2, 2021 at 14:21
9

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.

2
  • 1
    Didn't work for me...
    – Dave
    Mar 26, 2014 at 19:36
  • This did work for me, but in addition to removing the last newline I had to also restart the Cygwin terminal.
    – christosc
    Jul 10, 2015 at 9:44
8

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

1
  • I wonder about the root cause. Is there a bug report or configuration file that can be referenced here?
    – blong
    Aug 23, 2020 at 20:24
8

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

3
  • What's vim go to do with the question? OP is not using vim.
    – DavidPostill
    Jan 11, 2017 at 16:11
  • 2
    I 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
    Jan 11, 2017 at 19:47
  • 1
    See superuser.com/questions/283236/…
    – Iguananaut
    Jan 11, 2017 at 19:48
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).

2
  • 1
    I see this problem in Cygwin across multiple machines, but MinTTY looks better and solves the wrapping problem. Two birds with one stone!
    – wting
    May 14, 2011 at 21:10
  • Note that MinTTY is the default terminal for Cygwin since late 2011.
    – Hugh W
    Mar 22, 2017 at 15:43
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.

1
  • 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 :) Jun 13, 2019 at 8:46

You must log in to answer this question.

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