Difference between revisions of "Linux shell/Bash prompt PS1, settings and history"
Line 3: | Line 3: | ||
Edit vi ~/.bashrc | Edit vi ~/.bashrc | ||
# Uncomment <code>#force_color_prompt=yes</code> | # Uncomment <tt>#force_color_prompt=yes</tt><br><code>sed -i -E 's/^#(force_color_prompt=yes)/\1/g' ~/.bashrc</code> | ||
# Find <code>if [ "$color_prompt" = yes ]; then</code> statement | # Find <code>if [ "$color_prompt" = yes ]; then</code> statement | ||
# then comment out <code>#PS1=</code> and add following code in bold | # then comment out <code>#PS1=</code> and add following code in bold | ||
if [ "$color_prompt" = yes ]; then | if [ "$color_prompt" = yes ]; then | ||
'''parse_git_branch() { | '''parse_git_branch() { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'; }''' | ||
'''PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "''' | '''PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "''' | ||
#PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ' # | #PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ' #default colour prompt | ||
else | else | ||
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ ' | PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ ' |
Revision as of 21:26, 11 October 2017
This are steps to set up bash prompt showing git branch. This has been tested in Ubuntu 14 LTS
Edit vi ~/.bashrc
- Uncomment #force_color_prompt=yes
sed -i -E 's/^#(force_color_prompt=yes)/\1/g' ~/.bashrc
- Find
if [ "$color_prompt" = yes ]; then
statement - then comment out
#PS1=
and add following code in bold
if [ "$color_prompt" = yes ]; then parse_git_branch() { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'; } PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ " #PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ' #default colour prompt else PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ ' fi
It will similar to
Reload shell without logging out
. ~/.bashrc source ~/.bashrc exec bash exec "$BASH"
Differences
- source ~/.bashrc will preserve your current shell. Except for the modifications that reloading ~/.bashrc into the current shell (sourcing) makes, the current shell and its state are preserved, which includes environment variables, shell variables, shell options, shell functions, and command history.
- exec bash, or, more robustly, exec "$BASH"[1], will replace your current shell with a new instance, and therefore only preserve your current shell's environment variables (including ones you've defined ad-hoc). In other words: Any ad-hoc changes to the current shell in terms of shell variables, shell functions, shell options, command history are lost.
[1] exec bash could in theory execute a different bash executable than the one that started the current shell, if it happens to exist in a directory listed earlier in the $PATH. Since special variable $BASH always contains the full path of the executable that started the current shell, exec "$BASH" is guaranteed to use the same executable.
Bash coloured autocomplete of symlinks
Use bash --version | grep release
to find out what version of Bash you are using.
To configure it add lines below to your ~/.inputrc
or system-wide /etc/inputrc
Bash 4.3 readline adds a variable that enables color for tab completion to show different colors for executable files, directories, etc., during tab completion. Readline in the upcoming Bash 4.4 adds a variable which enables colour to indicate the matching portion of the string during tab completion.
set colored-stats on #bash 4.3 set colored-completion-prefix on #bash >=4.4
You can see the values of these variables using
bind -v | grep color
Bash autocomplete the common string with ellipses
Add to your ~/.inputrc
or system-wide /etc/inputrc
file
set completion-prefix-display-length 2
When you TAB to autocomplete the common string if it's longer than 2 characters will be replaced with (...) ellipses
Bash key binding
- Readline This is what allows for all bash keybindings, colouring etc..
Resources
- CustomizingBashPrompt Ubuntu wiki
- Git Bash Prompt repo project Great informative Git prompt