-1

I used to have an alias for cd in csh, which can show the current time and directory at the beginning of the cmdline and trigger the ls command. The effect is like this:

[10:24] /home/cambridgelv/Desktop/cd ..
Desktop  Documents  Downloads  Music
[10:24] /home/cambridgelv/cd Desktop
abc.doc  def.jpg
[10:25] /home/cambridgelv/Desktop/

Does anybody have any idea?

2 Answers2

1

Let me answer it by myself. I thought maybe I aliased the set prompt command into cd before, so we can do this separately. Refer to this answer: https://stackoverflow.com/a/33037878/11768989 You can customize the prompt in any way. Mine is look like this:

set prompt = '%{\e[35;40;1m%}[%T @%m]%{\e[0m%} %~/'
alias cd "cd \!:1; ls"
  • I mean that the one-line command maybe is ```alias cd "set prompt = '%{\e[35;40;1m%}[%T @%m]%{\e[0m%} %~/';cd \!:1; ls"```, which is meaningless. – Cambridge Lv Jun 17 '22 at 01:20
0

The easiest would be to hook in to the special cwdcmd alias; this gets run every time the current directory changes.

alias cwdcmd 'printf "[%s] " `date +%H:%m`'

For example, where > is the prompt:

> sleep 1

> cd /
[14:06] > sleep 1
> cd ~
[14:06] >

You can also alias cd with something like:

alias cd 'cd $* && printf "[%s] " `date +%H:%m`'

Where $* expand to the command arguments /dir in cd /dir. However, this also prints the directory in square brackets for some reason that I don't quite understand. The cwdcmd alias is better anyway.

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
  • Actually, this isn't a full answer, since it doesn't do the `ls`; I forgot about that. Getting the `ls` in there is a bit more effort as you want the date *before* the prompt and the `ls` *after* the prompt. – Martin Tournoij Jun 15 '22 at 12:22