PromptConfig, craft a custom terminal prompt with JSON

brandonweiss

Brandon Weiss

Posted on October 29, 2018

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!

💖 💪 🙅 🚩
brandonweiss
Brandon Weiss

Posted on October 29, 2018

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related