Sunday, September 25, 2016

Useful Tip: Changing command line prompt on Windows

I just came across a useful little trick this afternoon for making the Windows command line prompt (i.e. the one you get when running cmd, which shows the current directory) much shorter  (heck, you can set it to anything you like even!), so I thought I'd just note it here so that I can look it up again should I forget in future.

prompt [new_prompt_goes_here]

That's it! Just type this little command, and it will change the prompt that gets displayed.



But surely there must be more to this? And what about ...? Here are a bunch of important things to know about this (from what I've seen so far):

1) It's best to leave a space at the end of the prompt yourself, as it will not insert one for you

2) It doesn't like the ">" character appearing on the same line, even if it is wrapped with quotes.

3) If you wrap your prompt with quotes, they will appear in the prompt. In other words, it uses its input quite literally

4) Typing a single '$' character and then pressing enter doesn't work. It will sit there waiting for further input, suggesting that it can be used for escape codes (I'm not sure what those are yet though...)  As such, I'm not sure how you can get it to dynamically substitute stuff in there...

5) To make a prompt that shows a single '$' character (as seen in many tutorials for command line stuff), you'll need to use  $$.  So,
prompt $$

6) Pfft.... Linux/Posix-shells have had this ability for decades :P  (Cue-routine sounds of Windows bashing).  Yep, I know, and I've been using that stuff for years. Hence, that's why I'm trying to make some tweaks to make it nicer to use the terminal when dealing with directories that are 9-10 levels deep with "long_multi_underscore_names_part1-100" ;)

7) This change will only stay until you close the terminal, which shouldn't really be a surprise. It's just nice to have this option from time to time when you've got a really troublesome directory.

8) So, what's a quick and easy prompt that I suggest using on a "one off" basis?   See #5 above :)


----------------

EDIT: In trying to figure out what the magic $ codes are, I stumbled across a blog post which sheds a lot more light on terminal prompt customisation.

http://www.hanselman.com/blog/ABetterPROMPTForCMDEXEOrCoolPromptEnvironmentVariablesAndANiceTransparentMultiprompt.aspx

The key takeaways are this:
1) You can actually set a "PROMPT" environment variable instead, and this will change what your default prompt looks like.

2) The full list of magic codes (apparently you can find them via prompt /?) is as follows:
  $A   & (Ampersand)
  $B   | (pipe)
  $C   ( (Left parenthesis)
  $D   Current date
  $E   Escape code (ASCII code 27)
  $F   ) (Right parenthesis)
  $G   > (greater-than sign)
  $H   Backspace (erases previous character)
  $L   < (less-than sign)
  $N   Current drive
  $P   Current drive and path
  $Q   = (equal sign)
  $S     (space)
  $T   Current time
  $V   Windows XP version number
  $_   Carriage return and linefeed
  $$   $ (dollar sign)
  $+   zero or more plus sign (+) characters depending upon the
       depth of the PUSHD directory stack, one character for each
       level pushed.
  $M   Displays the remote name associated with the current drive
       letter or the empty string if current drive is not a network
       drive.

3) And for good reference, the full list of environment variables that can be substituted: http://libertyboy.free.fr/computing/reference/envariables/index.php


---------

Based on this post, I've ended up setting up a prompt that now looks like:
set prompt=$_---[$P]---$_$T$h$h$h$G

What this does is it creates a prompt that looks like this:
... previous output...               
                                     
---[C:\blenderdev\master2\blender]---
18:56:49> [new command goes here]    

Key points to note about this:
1) It is buffered by some blank lines  - this is useful for keeping the prompts/outputs separate, and helps prevent printf() slip-ups from invading into the prompt

2) The path is on a separate line, so that it can grow however long it wants

3) The current time is printed at the prompt. I decided to do this so that I can easily tell if I've remembered to recompile/restart the interpreter yet. (This has been a massive pain for the past few months on the projects I've been working on.... I'm keen to see how well this holds up)

4) The millisecond accuracy bit has been shaved off the time, by backspacing 3 times (2dp + the dot) since it's unlikely I can type that fast.


I'm now going to give this a try for a while to see how it goes. I'll edit this post later to note how well this experiment went :)

No comments:

Post a Comment