0

How to make my bash prompt display something at the end of the line (with the cursor positioned as usual at the beginning of the same line)? Specifically I want to display an abbreviated single character prompt such as $ or > after which I can type my command. But I want to be able to see the current working directory at the end of the line, where it is not "in my face".

I don't care what happens when I type a really long command. It can overwrite the current working directory or whatever else my prompt displays there.

Alternately I don't mind if the prompt causes the current working directory to show at the top right or top left, or bottom right/left of the screen. I just don't want it in my face at all times but I want it to be visible.

2

I was able to set PS1 to something that prints the current path at the end of the prompt line:

PS1='\[$(for ((i=1; i < COLUMNS - ${#PWD} - 3; ++i)) ; do printf " " ; done ; printf "$PWD")\]'

But it has several problems I wasn't able to solve:

  1. The path disappears when you press Backspace
  2. Despite the fact the whole expression is enclosed in zero-width marker \[...\], once you print also the $ or >, the cursor moves to the far right.

I've seen people using PROMPT_COMMAND=pwd instead to show the path, but it reduces the number of lines containing your commands in the terminal.

Another option is to configure your terminal or screen/tmux to display the path somewhere. For example, the following works in xterm:

PS1='\[\e[m\e]0;\W\a\]$ '

The sequence \e[m\e]0;...\a sets the title of the window. In screen, it's "\033k\033\0134\033kTITLE\033\0134".

  • genius! that's almost perfect for my usage (too bad about not being able to add a $ or >); i don't mind it disappearing with a backspace since it's optional information anyway. I will accept after a while just to let all answers come in. Thank you :-) – user2297550 Jul 1 at 11:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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