Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

When starting XTerm the prompt starts at the first line of the terminal. When running commands the prompt moves down until it reaches the bottom, and from then on it stays there (not even Shift-Page Down or the mouse can change this). Rather than have the start of the terminal lifetime be "special" the prompt should always be at the bottom of the terminal. Please note that I have a multi-line prompt.

Of course, it should otherwise work as before (resizeable, scrollable, no unnecessary newlines in the output, and no output mysteriously disappearing), so PROMPT_COMMAND='echo;echo;...' or similar is not an option. The solution ideally should not be shell-specific.

Edit: The current solution, while working in simple cases, has a few issues:

  • It's Bash specific. An ideal solution should be portable to other shells.
  • It fails if other processes modify PS1. One example is virtualenv, which adds (virtualenv) at the start of PS1, which then always disappears just above the fold.
  • Ctrl-l now removes the last page of history.

Is there a way to avoid these issues, short of forking XTerm?

share|improve this question
    
Somehow, we need to introduce blank characters in Xterm's scrollbar buffer. –  SHW Sep 1 at 8:31
3  
Actually, the prompt can be easily moved back to the top at any time by executing clear command. –  werediver Sep 1 at 8:49
    
@SHW I was hoping there was a setting for this rather than a hack. Terminal hacks have a tendency to introduce very subtle bugs in my experience. –  l0b0 Sep 1 at 11:37
    
@werediver But I never want it to be on top. –  l0b0 Sep 1 at 11:38
1  
Since only the shell knows when it outputs a prompt, any solution must be in the context of the shell. Even forking XTerm won't help because XTerm doesn't know whether what it is asked to output is a prompt or not. For the terminal, the shell prompt is just another character sequence, no different from any other character sequence it might receive. –  celtschk Sep 12 at 15:13

1 Answer 1

If using bash, the following should do the trick:

TOLASTLINE=$(tput cup "$LINES")
PS1="\[$TOLASTLINE\]$PS1"

Or (less efficient as it runs one tput command before each prompt, but works after the terminal window has been resized):

PS1='\[$(tput cup "$LINES")\]'$PS1

To prevent tput from changing the exit code, you can explicitly save and reset it:

PS1='\[$(retval=$?;tput cup "$LINES";exit $retval)\]'$PS1

Note that the variable retval is local; it doesn't affect any retval variable you might have defined otherwise in the shell.

Since most terminals cup capability is the same \e[y;xH, you could also hardcode it:

PS1='\[\e[$LINES;1H\]'$PS1
share|improve this answer
    
How tput differ from many echo commands ? (Asking out of curiosity) –  SHW Sep 1 at 11:40
    
@l0b0, probably doesn't deserve a separate answer. I hope celtschk won't mind I edited his/her answer. –  Stéphane Chazelas Sep 1 at 11:45
    
'\[$(tput cup "$LINES")\]' works beautifully. Thanks! –  l0b0 Sep 4 at 7:37
    
There is an issue with tput: It seems to reset $exit_code. Fixed by using \[\e[$LINES;1H\]. –  l0b0 Sep 8 at 22:40
1  
@l0b0: I've now added a version using tput that preserves the exit code. –  celtschk Sep 11 at 17:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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