A dynamic terminal prompt in linux.

A while back, I was frustrated while working on the terminal. I have nested folders which go down two or more levels. And the folders are explicitly named and therefore when I access them in a terminal, the prompt ends up taking more than half of the window width and the commands end up being wrapped around. There should be a better way.

I tried creating a dynamic prompt that would check the length of the prompt and compare it with the window width. If the length of the prompt was greater than half of the window width, it would reduce the prompt to just username@host:$ instead of the usual username@host:/.../dir/.../$ saving on space.

So, far I've only been able to add a couple of commands in by .bashrc that will check the length of the prompt, when prompted, and change the prompt if the length of the prompt is greater than a certain value, in this specific case more than 40 characters in length.

if ["$(expr length rahulporuri@astronut:$(pwd))" -gt 40]; then
    PS1 = '${debian_chroot:+(debian_chroot)}\u@\h\$\'
else
    PS1 = '${debian_chroot:+(debian_chroot)}\u@\h:\w\$\'
fi

is the snippet of code i added in my .bashrc. Everytime i want to change the length of the prompt, I do "$ source .bashrc" and the prompt length is corrected appropriately. the \u refers to the username, \h for host machine and \w for working directory.

I did try to compare this value with the current window width, the command to which I don't remember now, but it wasn't working. I should've been better at documenting stuff. Or maybe I did document it but forgot to add the reference in my .bashrc. I need to go back and check my notebooks now...

Popular posts from this blog

Farewell to Enthought

Arxiv author affiliations using Python

Elementary (particle physics), my dear Watson