DEV Community

Brandon Weiss
Brandon Weiss

Posted on

3

PromptConfig, craft a custom terminal prompt with JSON

PromptConfig lets you describe your prompt expressively in JSON and then it gets compiled to Bash. A very simple prompt might look like this:

{
  "prompt": ["character", "space"],
  "components": [
    {
      "key": "character",
      "value": "",
      "color": "magenta"
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

And it will generate Bash that looks like this:

function _promptconfig_prompt() {
  local prompt=''
  prompt+='\[\e[38;5;5m\]'
  prompt+='❯'
  prompt+='\[\e[0m\]'
  prompt+=' '
  PS1=$prompt
}

PROMPT_COMMAND="_promptconfig_prompt; $PROMPT_COMMAND"
Enter fullscreen mode Exit fullscreen mode

Why?

Bash is hard. It’s an arcane language and environment with a steep learning curve. Most programmers wind up using it on a very superficial level, figuring out just enough to get done what they need to do.

Unfortunately, customizing your prompt correctly requires a disproportionately high understanding of how Bash and the terminal work relative to what you’re trying to actually do. The internet is filled with recommendations that will break your prompt in subtle ways that aren’t immediately obvious and later you might not understand are caused by your custom prompt. I’m speaking from personal experience. 😭

It shouldn’t be that complicated and hopefully now it isn’t.

Take a look and let me know what you think!

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (2)

Collapse
 
bdmorin profile image
Brian

"Bash is hard." said the /bin/fish developers..

a few years later, fish shells are as arcane as bash prompts. lol.

Thanks for writing this, I'm going to try it.

Collapse
 
brandonweiss profile image
Brandon Weiss

Yeah, I realize “hard” is of course very subjective. I’ve always found it very difficult and most of the people I know seemed to agree. I use it infrequently enough that whatever I might learn when I use it I quickly forget. Debugging Bash while building this was really painful for me. 😭

I used Fish for a few years! I found the scripting much simpler, but it created another problem for me which is that tools that provide shell integrations always assume Bash, or maybe ZSH. A few provide Fish support but most don’t. After a while the headache of tools not working quite right or needing special patches was more trouble than it was worth.

My pleasure! Please let me know if you run into any issues or if it’s missing something! Someone recently pointed out they use styles like bold/underline/etc. in their prompt, so I might add support for that soon.

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️