Skip to content

Instantly share code, notes, and snippets.

@eddiecorrigall
Last active August 9, 2021 19:14
Show Gist options
  • Save eddiecorrigall/2adf9f56a5846a003d01bdebd59886c8 to your computer and use it in GitHub Desktop.
Save eddiecorrigall/2adf9f56a5846a003d01bdebd59886c8 to your computer and use it in GitHub Desktop.
Custom Devcontainer Command-Prompt

Devcontainer: Custom Shell

Does your fresh devcontainer terminal feels sluggish when you cd into a /workspaces repository? You are not alone. Turns out that what ever injects the command prompt (possibly the VS Code extension) uses slow code. Want to fix that? Replace it with something else.


Here is a ~/.bashrc that will do just fine. Edit on your devcontainer, and open a new bash terminal.

Useful Resources:

#!/bin/bash

########
# GitHub
########

function git_branch() {
    local branch
    branch="$(git symbolic-ref --short HEAD 2> /dev/null)"
    if [[ -n "$branch" ]]; then
        echo -n "$branch"
        return 0
    fi
    return 1
}

function git_name() {
  git config user.name 2> /dev/null
}

function git_has_diff() {
  git diff --quiet HEAD 2> /dev/null
}

########
# PROMPT
########

export WHITE='\[\033[1;37m\]'
export LIGHT_GREEN='\[\033[0;32m\]'
export LIGHT_BLUE='\[\033[0;94m\]'
export LIGHT_BLUE_BOLD='\[\033[1;94m\]'
export RED_BOLD='\[\033[1;31m\]'
export YELLOW_BOLD='\[\033[1;33m\]'
export COLOUR_OFF='\[\033[0m\]'

function prompt_command() {
  local P=()
  P+="[\$?]"
  P+=" "
  P+="${LIGHT_GREEN}\$(git_name || echo -n \$USER)"
  P+="${WHITE}"
  P+="${LIGHT_BLUE_BOLD}\w"
  P+=" "
  P+="${LIGHT_BLUE}("
  P+="${RED_BOLD}\$(git_branch)"
  P+="${YELLOW_BOLD}\$(git_has_diff || echo -n '✗')"
  P+="${LIGHT_BLUE})"
  P+="${COLOUR_OFF}"
  P+="\$"
  P+=" "
  export PS1=${P[@]}
}

export PROMPT_COMMAND='prompt_command'

Want to persist this? I recommend mounting your local machine ~/.bashrc to your devcontainer by editing file: .devcontainer/docker-compose.yml.

  1. Write the script to your local machine ~/.bashrc instead.

  2. Add the following volume.

services:
  app:
    volumes:
      - ${HOME}/.bashrc:/home/node/.bashrc:ro,cached
# ...
  1. Rebuild your devcontainer.

  2. Confirm this makes your console experience faster.


If you are on OSX, and you want to use the bashrc natively, you will also need to create a ~/.bash_profile file and source ~/.bashrc.

#!/bin/bash

[ -f "$HOME/.bashrc" ] && source "$HOME/.bashrc"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment