Coloring directory name in ksh

Coloring directory name in ksh

英雄似剑 发布于 2021-11-27 字数 1570 浏览 790 回复 5 原文

In my current situation, it is not unusual for me to have several UNIX computers I connect to, as several different users depending on the situation, and to traverse through various directories on the machines doing things. I use ksh through the entire thing.

I was fiddling with my prompt recently, and I was able to get it to change some colors depending on my current username and current server. However, what I would also want is for it to change colors based on my current directory. For example, if I were in directory "foo", the prompt should be yellow, but if I were in directory "bar", the prompt would be magenta. In both cases, subdirectories should also count, so a simple substring check should be enough.

The problem I ran into, however, is that when I run my .profile script, it properly colors the directory--but it no longer dynamically updates whenever I switch to another directory--and I'm not sure how before I did all the branching, I was able to get it to print my current working directory correctly even after I switched directories.

I did some googling, and find information for bash, but ksh seems to be largely ignored. As I cannot figure out how to do this on my own, I must bring it to the Stack Overflow community, to add it to future knowledge. Thus, with my long-winded explanation, the "quick version" of my question is as follows:

In ksh, how can I set up my prompt to display the current working directory and color the text based on where the current working directory is? Is it even possible?

如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。

扫码加入群聊

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

笑忘罢 2022-06-07 5 楼

I use this:

function chdir
{
   cd "$@"
   CWDH=${PWD%/*}
   PS1="($_time)$hname:${CWDH##*/}/${PWD##*/} ->"
   export PS1
}
alias cd=chdir
chdir .

Ignore the time and hname, but the rest should work for you. Changing colors is going to be terminal dependent. You need to know the escape codes for each color for the terminal you will be using. If you know you only ever use an xterm, it will be easier.

鹿港巷口少年归 2022-06-07 4 楼

Since nobody actually answered the part about the color, here's how I'd do it. I can't color the text, so I've also included a screenshot of my terminal.

See Wikipedia's page on ANSI escape codes for the full list of color codes and print --man and printf --man for the details of printing escape sequences in ksh.

(Note that ksh93v, currently in beta, will contain a prompt language based (I think) on bash's, which will make this sort of thing somewhat easier, though I think you'll probably still need to use something like this function to do complex conditional prompts like this one.)

> cat ~/scripts/prompt    
function prompt
{
    set -eu
    typeset c=
    case $PWD in
        */foo) c=3;; # yellow
        */bar) c=5;; # magenta
    esac
    print "E[3${c}m$PWDE[m > "
}
PS1='`prompt`'
> . ~/scripts/prompt
/Users/adavies > cd foo  
/Users/adavies/foo > cd ../bar
/Users/adavies/bar > 

enter image description here

心奴独伤 2022-06-07 3 楼

To display the current directory in ksh, put this in your .profile file:
export PS1="$PWD "
That will dynamically update when you change directory without mucking around with functions.

青萝楚歌 2022-06-07 1 楼

Why not using zsh? It is based on ksh, and it is much more powerful. In zsh you can write chpwd function that is implicitly called every time you change directory. In this function you can check your current directory and set PS1 to whatever you want.

Alternatively (even in ksh) you can create an alias for cd command:

change_my_ps() {
  PS1=...
}
better_cd() {
  builtin cd "$@"
  change_my_ps
}
alias cd=better_cd

Something like this. I'm not sure it is proper, I don't remember ksh syntax.