1

I have customized my command prompt to display current directory with some color and full path. Now I want to set title of my window to only directory name (not full path). Can you please advise how to achieve that in korn shell. Thanks.

1

The way I found to achieve this is by creating an alias to the cd. I put the following code inside the .profile file which did the trick

xcd() { cd $*; echo -ne "\033]0;$(basename $PWD)\007"; }
alias cd='xcd'

Thanks a lot to Kusalananda for the solution which I was actually looking for:

cd() { command cd "$@"; echo -ne "\033]0;${PWD##*/}\007"; }
  • 1
    You may get rid of the alias if you name the function cd. Inside the function, use builtin cd instead of cd. You can also use "${PWD##*/}" in place of that call to the external basename utility. You should also use "$@" instead of $* to transfer the arguments to the built-in cd properly. – Kusalananda Jan 10 '17 at 9:02
  • Hi Kusalananda, thanks for your inputs. I modified the cd as below, cd() { builtin cd $@; echo -ne "\033]0;${PWD##*/}\007"; } but now my alias which sets the home directory is not working. alias cda='cd ${TEST_DIR}/app/linux/' running cda gives me the error: $ cda builtin: /test/dir/app/linux/: not found – Forever Learner Jan 10 '17 at 9:21
  • 1
    Ok, use command cd "$@" instead of builtin cd $@. You do need the double quotes. – Kusalananda Jan 10 '17 at 9:26
  • and thanks for $@ and PWD## trick. I implemented those changes. Upvoted your comment. xcd() { cd $@; echo -ne "\033]0;${PWD##*/}\007"; } alias cd='xcd' – Forever Learner Jan 10 '17 at 9:27
  • Awesome Kusalananda. command cd "$@" did the trick. :) Upvoted :) Thanks a ton for taking time and helping me out. – Forever Learner Jan 10 '17 at 9:31
0

According to this site : How to change the title of an xterm: examples for different shells

ksh provides little in the way of functions and expansions, so we have to insert the escape string in the prompt to have it updated dynamically. This example produces a title of username@hostname: directory and a prompt of ksh$ .

case $TERM in
    xterm*)
        HOST=`hostname`
        PS1='^[]0;${USER}@${HOST}: ${PWD}^Gksh$ '
        ;;
    *)
        PS1='ksh$ '
        ;;
esac

However, $PWD produces the full directory path. We can remove the prefix of $HOME/ from the directory using the ${...##...} construct. We can also use ${...%%...} to truncate the hostname:

HOST=`hostname`
HOST=${HOST%%.*}
PS1='^[]0;${USER}@${HOST}: ${PWD##${HOME}/}^Gksh$ '

Note that the ^[ and ^G in the prompt string are single characters for ESC and BEL (can be entered in emacs using C-q ESC and C-q C-g).

  • Thanks Felandral for searching this. I have already tried this, it changes the prompt string too PS1 and I am interested in changing the title to the current directory while keeping the prompt string as it is. – Forever Learner Jan 5 '17 at 16:13
  • This works (I tried on my terminal), you just need to replace ksh$ (at the end of the string) by the PS1 you want. And don't forget to use ESC and Ctrl+g to type the escape characters ^[ and ^G. – Félicien Jan 5 '17 at 17:27
  • it is not working, my PS1 string is ,export PS1=$(print '[\033[31m${PWD}\033[00m]\n$ '), I want to try the tile as just basename of PS1. Can you please advise – Forever Learner Jan 9 '17 at 6:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.