DEV Community 👩‍💻👨‍💻

Cover image for How to add a custom prompt to windows console

Posted on • Updated on

How to add a custom prompt to windows console

Cmder is an awsome package of software which replaces the default console cmd.exe or powershell on windows.

If you are in a git repo, you see the current branch in the prompt, colored in white if its clean or red if dirty. There are similar features for yarn, npm, ...

Cmder Screenshot

Sometimes you want to add the output of to the prompt too. This can be done like this:

In my case i wanted to see on which terraform workspace i am. with terraform you can do this by running terraform workspace show in your terraform directory. this would output something like

λ terraform workspace show

To do this you have to create a clink extension. Assuming that you installed cmder in C:\Program Files\Cmder you create a file called terraform.lua in C:\Program Files\Cmder\vendor\clink-completions


local color = require("color")

local function is_terraform()
    for dir in io.popen([[dir ".\" /b /ad]]):lines() do 
        if dir == ".terraform" then
            return true

    return false

local function terraform_prompt_filter()
    if is_terraform() then
        f = assert (io.popen ("terraform workspace show"))
        for line in f:lines() do
            terraform_context = '(workspace: ' .. line .. ')'
            if line == "production" then
                prompt_color = color.RED
            elseif line == "development" then
                prompt_color = color.GREEN
                prompt_color = color.YELLOW
        end -- for loop
        terraform_context = color.color_text(terraform_context, prompt_color)
        clink.prompt.value = clink.prompt.value:gsub("\n", terraform_context .. "\n")
        return false

clink.prompt.register_filter(terraform_prompt_filter, 1)

this code checks if it is currently in a terraform directory by looking if the directory .terraform exists. it colors the workspace output in green if you are on development workspace, red if you are on production and yellow on all others. adapt the code for your needs.

i am sure that the lua code can be optimized, i am open for comments and enhancements.


Top comments (3)

rupankarghosh profile image

Cmder was the best terminal for custom prompt with zsh before Hyper. I love the git integration and syntax highlighting without any configuration. But now cmder became a bit obsolete.

c33s profile image
Julian Author

thaks for the tip. just wanted to give it a try but hyper has this terrible squirrel installer which collides with my sandboxing system.

why it has become so uncommon to install an app directly in C:\Program Files\Appnam shiped as msi file and additional as zip file -.- *sigh*

bitsmag profile image

Thanks :) That helped a lot !

🌚 Life is too short to browse without dark mode