How to add current date/time to clink/cmder prompt

11

4

I'm a rather happy user of clink via the great cmder package. The only thing I miss is an easy way to set the command prompt to display the current date/time (and last exit code, history number, as in bash... etc).

Could someone point me to the right direction on how to do this?

Zsolt Botykai

Posted 2014-01-16T13:58:56.243

Reputation: 627

Can you just use the Windows date /t? Also, Windows stores the last exit code in %errorlevel%, while sh uses $?. Try those. – Bob – 2014-01-16T14:11:04.317

But how to integrate that output to the prompt? I don't want to run date /t every time before running some long-running command. – Zsolt Botykai – 2014-01-16T14:33:54.560

Oh. Go see prompt /?, there's a date and time option in there. Again, that's standard Windows command prompt - I have no idea what clink or cmder do. – Bob – 2014-01-16T14:43:17.743

Answers

8

Try this prompt settings (example only, it's show how you can call any console application inside "prompt printing"). Note! It works in ConEmu only.

prompt $p$s$e]9;7;"cmd /c echo (%DATE% %TIME%)"$e\$g

But, as Bob said, there is an easier way:

prompt $p$s$d$s$t$s$g

And for cmder you should edit the supplied init.bat as that defines the prompt settings.

Maximus

Posted 2014-01-16T13:58:56.243

Reputation: 19 395

For cmder, the suggested way to edit prompt settings is in %CMDER_ROOT%\config\user-startup.cmd and not init.bat. – wegry – 2016-06-30T13:23:37.323

On win7 with Cmder, adding $t did the trick for me : @prompt $E[1;32;40m$P$S{git}{hg} $t$S$_$E[1;30;40m{lamb}$S$E[0m – Benj – 2016-07-21T09:32:09.870

@wegry this %CMDER_ROOT%\config\user-startup.cmd file didn't seem to have any effect on my configuration. Maybe was I doing it wrong. – Benj – 2016-07-21T09:33:03.980

@Benj It didn't seem to work for me at first either, but a restart later it did? I'm mystified by what made start working. – wegry – 2016-07-21T12:51:28.377

@wegry Well, let's say both methods are OK... – Benj – 2016-07-22T09:12:18.430

2in 2018 for me worked prompt cmd /c echo ($t$s) :$p$g to add to the user-profile.cmd file (windows10). – Edwin – 2018-09-12T14:19:45.320

10

The answer provided by Maximus is no longer valid for cmder 1.3+

You have to create a .lua file (for ex. my_prompt.lua) inside your cmder config folder with your customized definition (source).

Below my customization:

function custom_prompt()
  cwd = clink.get_cwd()
  prompt = "\x1b[1;32;40m{cwd} {git}{hg} \n\x1b[1;30;40m{time}\n{lamb} \x1b[0m"
  new_value = string.gsub(prompt, "{cwd}", cwd)
  add_time = string.gsub(new_value, "{time}", os.date("%x - %X"))
  clink.prompt.value = string.gsub(add_time, "{lamb}", "λ")
end

clink.prompt.register_filter(custom_prompt, 1)

And this is the resulting prompt

C:\
03/25/17 - 20:56:14
λ

You can find more customization options for the time output in the Lua manual


update for comment reported error

function time_prompt()
    os.setlocale ("", "time")
    local cwd = clink.get_cwd()
    local prompt = "\x1b[1;32m{cwd} {git}{hg} \n\x1b[30m{time}\n{lamb} \x1b[0m"
    local new_value = string.gsub(prompt, "{cwd}", cwd)
    local add_time = string.gsub(new_value, "{time}", os.date("%x - %X"))
    clink.prompt.value = string.gsub(add_time, "{lamb}", "λ")
end

Gruber

Posted 2014-01-16T13:58:56.243

Reputation: 255

Maximus answer is correct, but this is the best answer! – AuthorProxy – 2017-05-24T10:28:13.363

D:\Tools\cmder\config\my_prompt.lua:1: unexpected symbol near char(255) and the prompt looks like: 1:26.51)"←\\ – Devil's Advocate – 2018-03-12T16:14:14.963

mmm I don't know what is that, but checking again my lua file it's not exactly as the old code I posted, tell me if using the one I'm updating now is working. – Gruber – 2018-03-12T21:56:52.977

Comment from @Edwin in Maximus' answer worked for me. prompt cmd /c echo ($t$s) :$p$g in user-profile.cmd – Devil's Advocate – 2019-07-10T21:07:37.100

This alone doesn't change anything on my git bash prompt in cmder. Is cmder supposed to read and execute all .lua files in the config folder at startup? – AsGoodAsItGets – 2020-01-28T17:36:36.467

@AsGoodAsItGets the above script is to modify the cmd.exe shell with clink via lua programming, it has nothing to do with the bash shell. Check the bash documentation for prompt customizing. – Gruber – 2020-01-29T01:31:22.470

@Gruber it didn't do anything to my cmd prompt either, but that's probably because I already have the powerline prompt which comes with its own .lua files. What worked for me is what I wrote below in my answer. Maybe it will help someone in the same situation. – AsGoodAsItGets – 2020-01-29T12:38:18.163

0

One line modification for cmder. Put it to cmder\config\my_config.lua

function my_prompt_filter()
    cwd = clink.get_cwd()
    prompt = "\x1b[1;32;40m{cwd}{git}{hg} $> \x1b[33;40m"
    new_value = string.gsub(prompt, "{cwd}", cwd)
    clink.prompt.value = string.gsub(new_value, "{lamb}", "λ")
end

clink.prompt.register_filter(my_prompt_filter, 1)

result:

C:\Users\user1 $>
C:\Users\user1 $> date
The current date is: 02.02.2018
C:\Users\user1 $>

Stan Gabenov

Posted 2014-01-16T13:58:56.243

Reputation: 1

0

None of the solutions here worked for me, so I ended up with adding the following line in my .bashrc:

alias myprompt='export PS1="\[\e]9;9;"\w"\007\e]9;12\007\]\[\033]0;$MSYSTEM:${PWD//[^[:ascii:]]/?}\007\]\[\033[32m\]\u@\h \[\033[33m\]\w\[\033[36m\]`__git_ps1`\[\033[0m\] \D{%T}\nλ "'

That last part \D{%T} is what shows the current time (don't care about the date, as usually I need to know how much time has ellapsed since I started a task).

Of course, this doesn't automatically change the prompt on all git bash terminals. You have to execute the myprompt command on the default prompt to change it. I tried to just do the above export inside the .bashrc file but I was getting an error. Maybe someone will have a better idea on how to get around that.

AsGoodAsItGets

Posted 2014-01-16T13:58:56.243

Reputation: 111