When using the terminal in a deep folder structure sometimes the prompt can take up most of the line. Is there any way in which I can trim the working directory? I know I can do
PS1="\W >"
to only print the current directory and not the full path, but is there a way to have something like:
/home/smauel/de...ther/folder >
If you are using bash4 (Ubuntu 9.10 and newer has bash4), the easiest option is to just set the PROMPT_DIRTRIM variable. e.g.:
For one similar to João Pinto's example, (that'll work in older bash versions and ensures that the path component is never longer than 30 characters), you could do something like this:
Create a small python script which implements the desired trimming logic.
Example:
~/.short.pwd.py
Now test it, from a terminal:
If you are ok with the result just append the command to your
~/.bashrc
.Another way around that problem is to include a line break into PS1, so that the working directory and the actual prompt appear on separate lines, for example:
Add this to the bottom of your
~/.bashrc
Admittedly this could probably be cleaner, but I wanted to get a crack at it.
Expected output for directories more than three layers deep.
Expected output for directories from Desktop and back.
Based on Cris Sullivan's answer, but keeping the
~
for the home folderJust to update slightly (for Python3) and enhance the selected answer to add colours to the prompt as per a BASH prompt (in Linux Mint 18.3 anyway):
More on colour codes in a BASH Terminal here. There's probably some way of finding out what colours your Terminal uses automatically, but I haven't got a clue what that might be.
With the shebang line the
export
line for inclusion in .bashrc then becomes:NB1 these "\e" escape codes must always be enclosed in "\[ ... \]", otherwise line-returns get completely messed up.
NB2 to get your full path at any time just go
of course...
This small addition to @joão-pinto's excellent answer adds in the virtual environment name when you run the
workon
command.I like this one most,
PS1="[\W]\\$ "
this prompt shortens all names except the current line this: