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:
- https://www.cyberciti.biz/tips/howto-linux-unix-bash-shell-setup-prompt.html
- https://wiki.archlinux.org/title/Bash/Prompt_customization
- https://linuxconfig.org/bash-prompt-basics
#!/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
.
-
Write the script to your local machine
~/.bashrc
instead. -
Add the following volume.
services:
app:
volumes:
- ${HOME}/.bashrc:/home/node/.bashrc:ro,cached
# ...
-
Rebuild your devcontainer.
-
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"