Skip to main content
2 of 4
edited tags
terdon
  • 252.3k
  • 69
  • 480
  • 718

Compact bash prompt when using a long name folder

In a system with Ubuntu 14.04 and bash, I have the PS1 variable ending with the following contents:

\u@\h:\w\$

so that the prompt appears as

user@machinename:/home/mydirectory$

Sometimes, however, the current directory has a long name, or it is inside directories with long names, so that the prompt looks like

user@machinename:/home/mydirectory1/second_directory_with_a_too_long_name/my_actual_directory_with_another_long_name$

This will fill the line in the terminal and the cursor will go to another line, which is annoying.

I would like instead to obtain something like

user@machinename:/home/mydirectory1/...another_long_name$

Is there a way to define the PS1 variable to "wrap" and "compact" the directory name, to never exceed a certain number of characters, obtaining a shorter prompt?

BowPark
  • 5.2k
  • 13
  • 54
  • 78