Variable length console prompt
first thanks for taking the time to read this thread.
I have a question about the prompt... it is very easy to tune it for it to be colored and display path where you are etc...
But my problem is that when the path is too long I would prefer the code line to be on the folowing line...
11:00 me@host a/short/path > ls -ltr ./stuff
11:00 me@host a/very/very/very/long/path
> ls -ltr ./stuff
and to be honnest as I am very new in LINUX I don't know how to do this...
Would you kindly help on this ?
Your base prompt is PS1 so you can set it by typing:
So you can the greater than with:
(You need the \ in front of the sign because it is also a redirect character and the \ escapes it (tells the command to take it literally).
You might want to do something a little different like:
export PS1="[\u@\h \W]\$"
That gives you a shorter prompt that only includes your user name, hostname and the basename (last directory) of the path. It would look something like:
If you were in /var/spool/mqueue for example.
The reason you might want user and hostname in the prompt is to be sure you're doing something as the user you think you are on the host you think you are on. Many an admin has accidentally killed their system by doing something as root that they thought they were doing as a non-privileged user or doing it on a host other than the one they thought they were on.
The trouble here is, whatever code snippet you use in your .bashrc or .bash_profile file to create your custom prompt, it is only executed once, when the terminal is started. So, scripting a prompt that way won't work (beyond the first time).
You could terminate your path with a \n (a newline) regardless how long the path is, but I would understand if you said that that would not be too pleasing. I wouldn't prefer that either.
The best solution I know of (and there certainly could be another way!), would be to use the $PROMPT_DIRTRIM variable, which is a bash built-in, and which determines how many elements (separated by slashes) will be shown as the path in your prompt. So, as an example:
export PS1="\w \>"
The above will show the current working directory in your prompt. If you are 7-deep in subfolders, you'll have a
But: set the $PROMPT_DIRTRIM variable, let's say you set it to equal 3, and then you will have:
"/.../very/long/path >" -- you see, you now have /.../ followed by only 3 folders/in/the/path.
If you want to do this so it has effect all the time, just set the variable in /etc/profile or your ~/.profile so it will take effect at startup/login.
Hope this helps -- but if there's a way to dynamically alter the prompt as you describe, putting a newline at the end only when the path is very long, I'll be interested to learn of it.
Yes, it was for the OP. For example:
will now show the date as a prompt, but it shows the exact same date+time each time the prompt is shown, in the same console instance; the date/time doesn't advance. When one opens a new console, the date will again be fresh at first, but will not be updated further within the same console (i.e. it's not dynamic within a console instance).
With thanks to The Linux Online website:
Create a shell script (You must be using BASH)
Then edit your .bashrc to include `/usr/bin/addcr` for the default length of 20 characters
or `/usr/bin/addcr #` for any other value (replace # with any number you wish) immediately before the \$ in PS1
My .bashrc contains 3 lines that set the value of PS1. These would each need to be altered if the prompt is to be effective for any terminal. Otherwise some terminals will see the change while others simply use the original prompt.
Wrapping any command in a shell script, then executing the shell script by placing it in backticks (see my other post) makes it possible for anything to be dynamic including displaying the date using $(date).
The Linux Online website (http://www.linuxonline.com) has a very complete section on programming the bash prompt which includes using bash scripts called at the time the prompt is displayed. This makes the prompt dynamically alterable.
I didn't know how easily it could be accomplished until I looked for an answer to statquant's question.
Wahhhhhhhhhhhhoooooooooo awesome !!!
thank you guys for all this, I was offline 3 hours and I come back with the solution to my problem.
I am trying this now... keep you posted !!
|All times are GMT -5. The time now is 01:09 AM.|