Welcome to the most active Linux Forum on the web.
Go Back > Blogs > linux-related notes
User Name


Just annotations of little "how to's", so I know I can find how to do something I've already done when I need to do it again, in case I don't remember anymore, which is not unlikely. Hopefully they can be useful to others, but I can't guarantee that it will work, or that it won't even make things worse.
Rate this Entry

Xterm's title displaying current directory and running process, plus bash prompt also an abbreviated $PWD

Posted 08-19-2021 at 08:19 PM by the dsc
Updated 08-19-2021 at 08:41 PM by the dsc

Just found out, adapting from this answer on unix-stackexchange.

I had to adapt it somewhat because I already had "history -a" running all the time, so it would display it by default, which I don't like. Instead, the current directory would be better. So, the relevant bits of my ~/.bashrc are probably these, I think:

shopt -s histappend histreedit histverify cdspell dirspell no_empty_cmd_completion cmdhist checkwinsize autocd

# [...]

shopt -s histappend

# [...]

# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options

# append to the history file, don't overwrite it
shopt -s histappend

# [...]

# this is a function that's used to "shrink" some string to a given length, which I use for $PWD, 
# and also for xterm's window title, as of now. Maybe it should be edited, removing the all-uppercase strings,
# since it brings the risk of some eventual new standard environment variable having the same name,
# and messing things up. I didn't create this function, but I've found it elsewhere, I can't remember where.

_PS1 () 
    local PRE= NAME="$1" LENGTH="$2";
    [[ "$NAME" != "${NAME#$HOME/}" || -z "${NAME#$HOME}" ]] &&
        PRE+='~' NAME="${NAME#$HOME}" LENGTH=$[LENGTH-1];
    ((${#NAME}>$LENGTH)) && NAME="${NAME:$[${#NAME}-LENGTH+4]}";
    echo "$PRE$NAME"

PS1='\u@\h:$(_PS1 "$PWD" 17)\$ '

# [...]

# and finally the actual adaptation, it just checks whether the command is "history," and if it's not, then it
# updates the title with the environment variable BASH_COMMAND appended to the shortened current directory,
# otherwise it's just the shortened PWD. It also apparently needs "sed" replacing the single-character elipsis
# for three dots, otherwise apparently it doesn't print nicely on xterm's title, at least on Openbox.
# If I could have the shortened PWD as a string, then it could be less expensively/more elegantly edited
# with endogenous string manipulation, like "${shortPWD//...}", but apparently it doesn't work like that if 
# I simply declare a string following the function itself, before PS1, the string isn't updated. Probably
# it can be set up to run along with "history -a" for every command, then it would work, I guess, haven't tried yet.
# EDIT: tried for a little while, failed miserably, it just got worse and worse the more I tried.

trap '[[ ! "${BASH_COMMAND}" == "history"* ]] && printf "\033]0;%s\007" "$(_PS1 "$PWD" 17 | sed "s||...|" )/${BASH_COMMAND//[^[:print:]]/}" || printf "\033]0;%s\007" "$(_PS1 "$PWD" 17 | sed "s||...|" )/"' DEBUG
Posted in Uncategorized
Views 179 Comments 0
« Prev     Main     Next »
Total Comments 0




All times are GMT -5. The time now is 03:08 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration