DEV Community

Benjamin DeCoste
Benjamin DeCoste

Posted on

5

Customizing nushell prompt

By default, nushell's prompt shows your working directory on the left, and the date & time on the right. It looks like this:

nushell default prompt

As someone who was trying nu after using zsh for a long time, I wanted to replicate my old prompt, which showed me my working directory, current kubernetes cluster, and current k8s namespace:

zsh prompt

Nushell configures your prompt in its environment script ($nu.env-path). If you open this file you will see two functions at the very top, create_left_prompt and create_right_prompt. you can see the default implementation for the date



def create_right_prompt [] {
    let time_segment = ([
        (date now | date format '%m/%d/%Y %r')
    ] | str join)

    $time_segment
}


Enter fullscreen mode Exit fullscreen mode

I created a new function for my k8s prompt. I liked having the time on the prompt, so I left that (thought I didn't feel like I needed the date).



def kube_prompt [] {
    let k_prompt =  ([(kubectl ctx -c), (kubectl ns -c)] | str trim | str join '/')
    let d_prompt = ([(date now | date format '%r')] | str join)
    $"\(($k_prompt)\) ($d_prompt)"
}


Enter fullscreen mode Exit fullscreen mode

Just below, we can edit the let-env PROMPT_COMMAND_RIGHT to look like:



let-env PROMPT_COMMAND_RIGHT = { kube_prompt }


Enter fullscreen mode Exit fullscreen mode

Restarting your shell and you will see the new prompt

nushell with k8s prompt

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

AWS Security LIVE!

Hosted by security experts, AWS Security LIVE! showcases AWS Partners tackling real-world security challenges. Join live and get your security questions answered.

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. ❤️