2

I used to have this plugin in bash which shows current time after every command I type. I added this few years back, and I don't recall how I added it. It's not listed in terminator plugins either. Now I'm moving to a new laptop and I need to install this plugin again.

Does any of you know about this? Thanks.

enter image description here

Bee
  • 155
  • 6
  • 1
    I am not sure what that plugin is called, but I did post an answer before on something similar that a friend of mine and I did: http://askubuntu.com/a/676362/231142 – Terrance May 05 '16 at 14:38
  • wow... this is unbelievable.. looks like I've been using an older version of "your script"... :) just found the script in my home after reading your comment. Thank you very much... – Bee May 06 '16 at 06:12
  • And Thank You for the kind words! Plus, you're very welcome! =) – Terrance May 06 '16 at 13:55

2 Answers2

4

This script will add a grey dashed line with the date and time at the end of the line, then it will change to red if you run as a root user:

enter image description here


Add the following lines to the bottom of both /home/<username>/.bashrc and /root/.bashrc files (NOTE: /root/.bashrc has to be edited with root capabilities like sudo gedit /root/.bashrc):

if [ -f "$HOME/.bash_ps1" ]; then
    . "$HOME/.bash_ps1"
fi

Copy and paste the following code to a new file called /home/<username>/.bash_ps1:

# Fill with minuses
# (this is recalculated every time the prompt is shown in function prompt_command):
fill="--- "

reset_style='\[\033[00m\]'

# determine if root or not
a=$(id -u)
if [ "$a" = 0 ]
then
    # for root
    status_style=$reset_style'\[\033[1;31m\]' # bold red; use 0;37m for lighter color
    command_style=$reset_style'\[\033[1;31m\]' # bold red
else
    # for other users
    status_style=$reset_style'\[\033[0;90m\]' # gray color; use 0;37m for lighter color
    command_style=$reset_style'\[\033[1;29m\]' # bold black
fi
prompt_style=$reset_style

# Prompt variable:

PS1="$status_style"'$fill $(date +"%m/%d/%y ")\t\n'"$prompt_style"'${debian_chroot:+($debian_chroot)}\u@\h:\w\$'"$command_style "

# Reset color for command output
# (this one is invoked every time before a command is executed):
trap 'echo -ne "\033[00m"' DEBUG

function prompt_command {
# create a $fill of all screen width minus the time string and a space:
let fillsize=${COLUMNS}-18
fill=""
while [ "$fillsize" -gt "0" ]
do
    fill="-${fill}" # fill with underscores to work on
    let fillsize=${fillsize}-1
done

# If this is an xterm set the title to user@host:dir
case "$TERM" in
    xterm*|rxvt*)
    bname=$(basename "${PWD/$HOME/~}")
    echo -ne "\033]0;${bname}: ${USER}@${HOSTNAME}: ${PWD/$HOME/~}\007"
    ;;
    *)
    ;;
esac
}

PROMPT_COMMAND=prompt_command

Next, create a link in the /root folder so that it calls this when switching to the root user:

sudo -s
cd /root
ln -s /home/<username>/.bash_ps1

After all is saved, now every time you open a new terminal window it should look like the image above. The timestamps appear after you press enter on each command you type into the terminal making it easier to scroll back and see when you ran that specific command.

Hope this helps!

Terrance
  • 41,612
  • 7
  • 124
  • 183
  • 1
    @janos Works well! I wrote the scripting a long time ago, and sometimes I forget about easier ways to do stuff. Thank you! =) – Terrance Dec 19 '17 at 23:21
1

This might be helpful. Another possibility can be oh-my-zsh.

I installed this, and I use the theme, rkj.

This is how my terminal looks,

┌─[luvpreet@DHARI-Inspiron-3542] - [~] - [2017-08-28 06:45:20]
└─[0] 

Firstly it shows current user and hostname(luvpreet@DHARI-Inspiron-3542). Then it tells the directory you are in(~). Then the current time(2017-08-28 06:45:20). Then the status code of the previous command you ran(0(successful)).

You can check it here. https://github.com/robbyrussell/oh-my-zsh There are a lot of other cool themes available there.

luv.preet
  • 5,787