Skip to content

Instantly share code, notes, and snippets.

@esafak
Last active November 17, 2025 20:46
Show Gist options
  • Select an option

  • Save esafak/685618841211be0d946759e5e52aa0f8 to your computer and use it in GitHub Desktop.

Select an option

Save esafak/685618841211be0d946759e5e52aa0f8 to your computer and use it in GitHub Desktop.
A nushell `create_left_prompt` to print the git branch
def create_left_prompt [] {
# Create a relative path segment
let relative_path = if ($env.PWD | str starts-with $env.HOME) {
$env.PWD | path relative-to $env.HOME
} else {
$env.PWD
}
let path_segment = if (is-admin) {
$"(ansi red_bold)($relative_path)"
} else {
$"(ansi xterm_springgreen1)($relative_path)"
}
# get the current branch / commit ref
let git_ref = (try { git branch --show-current e> /dev/null | str trim } catch { "" })
let git_ref = if ($git_ref == "") {
# Check if we're in a git repo by trying to get the SHA
let sha = (try { git rev-parse --short HEAD e> /dev/null | str trim } catch { "" })
if ($sha != "") {
$"HEAD@($sha)"
} else {
""
}
} else {
$git_ref
}
let git_segment = if ($git_ref != "") {
$"(ansi reset) (ansi yellow)($git_ref)"
} else {
""
}
$"(ansi reset)($path_segment)($git_segment)"
}
$env.PROMPT_COMMAND = { || create_left_prompt }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment