Skip to content

Instantly share code, notes, and snippets.

@ronaldsuwandi
Last active July 30, 2021 07:54
Show Gist options
  • Star 40 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save ronaldsuwandi/10013651 to your computer and use it in GitHub Desktop.
Save ronaldsuwandi/10013651 to your computer and use it in GitHub Desktop.
My personal fish shell config for OS X. Supports git branch (fast git checks), virtualenv (if installed) and Kubernetes current context
function ls --description 'List contents of directory'
command ls -lFG $argv
end
function subl --description 'Launches sublime text in a new window'
command subl -n $argv
end
function code --description 'Launches visual code studio in a new window'
command code -n $argv
end
function grep --description 'Colorful grep that ignores binary file and outputs line number'
command grep --color=always -I $argv
end
function gf --description 'Do a git fetch'
git fetch
end
function gdeletemergedcurrent --wraps git --description 'Delete all local branches that is already merged to current branch (excludes master and main)'
git branch --merged | grep -v "\*" | grep -v "master" | grip -v "main" | xargs -n 1 git branch -d
git remote prune origin
end
set -gx GOPATH $HOME/Go
set -gx GOROOT (go env GOROOT)
set PATH $HOME/bin $GOPATH/bin $HOME/.rbenv/shims /usr/local/bin /usr/local/sbin $PATH $GOROOT/bin ./node_modules/.bin
. $HOME/.config/fish/prompt.fish
# set -gx HOMEBREW_GITHUB_API_TOKEN #token here#
# Java
set -gx JAVA_HOME (/usr/libexec/java_home)
# Allow 256 colors in iTerm2 for pretty vim colors
set -gx CLICOLOR 1
set -gx TERM xterm-256color
# use custom function instead of fish built in git prompt due to performance
# (fish git prompt uses diff which can be slow)
function git_prompt
set -l git_status_origin (command git status -s -b 2> /dev/null)
printf ''
set -l is_repo (string join0 $git_status_origin)
if test -z $is_repo
# git status returns error (not a git repo)
printf ''
else
echo $git_status_origin | string match --regex '\[.*ahead.*\]' --quiet
set -l ahead $status
echo $git_status_origin | string match --regex '\[.*behind.*\]' --quiet
set -l behind $status
set -l branch (echo $git_status_origin | string replace -r '## ([\S]+).*' '$1' | string replace -r '(.+)\.\.\..*' '$1')
# simply check for modified/deleted/rename, match only 1
echo $git_status_origin | string match --regex '[MDR ][MDR ] .*' --quiet
set -l git_dirty $status
# simply check for ?? in the list of files, match only 1
echo $git_status_origin | string match --regex '\?\? .*' --quiet
set -l git_untracked $status
if test "$git_dirty" -eq 0
printf ' (%s%s' (set_color red)
else
printf ' (%s%s' (set_color yellow)
end
# Use branch name if available
if test -n "$branch"
printf $branch
else
# for new branch, git branch will return nothing, use branch name from git status
set -l git_status_no_commits_branch (echo $git_status_origin | string replace '## No commits yet on ' '')
printf $git_status_no_commits_branch
end
if test "$git_untracked" -eq 0
printf '%s*' (set_color purple)
end
if test -s .git/refs/stash # stash exists - check on .git/refs/stash file
printf '%s$' (set_color green)
end
# if local repo is ahead, show up arrow
if test "$ahead" -eq 0
printf '%s↑' (set_color cyan)
end
# if local repo is behind, show down arrow
if test "$behind" -eq 0
printf '%s↓' (set_color magenta)
end
printf '%s)' (set_color normal)
end
end
function kubectl_context
if test -e ~/.kube/config; and type -q kubectl
set -l current_context (kubectl config current-context)
printf ' %s[' (set_color normal)
printf '%s%s' (set_color cyan) $current_context
printf '%s]' (set_color normal)
else
printf ''
end
end
function fish_prompt
# check for iterm2 shell integration
if test -e {$HOME}/.iterm2_shell_integration.fish ; and source {$HOME}/.iterm2_shell_integration.fish
printf ' '
end
printf '%s%s%s' (set_color $fish_color_cwd) (prompt_pwd) (set_color normal)
printf '%s%s ☕️ ' (git_prompt) (kubectl_context)
set_color normal
end
@henno
Copy link

henno commented Nov 13, 2016

Can you think any reason why, after applying these changes (without GO and JAVA lines), one gets this output when trying to autocomplete a command's parameter in Fish:
screen shot 2016-11-13 at 10 10 22

@ronaldsuwandi
Copy link
Author

@henno that's weird, i've never seen that before, perhaps trying upgrading fish and iterm?

@gertcuykens
Copy link

could be wrong but i think all -gx can be replaced with -x because it only need to be exported and it doesn't need to be available as a global variable in the script itself?

@sidharthv96
Copy link

@henno The issue is with the grep function. Remove it and it's working fine for me

@ronaldsuwandi
Copy link
Author

updated git prompt to be much more efficient. Reduces the number of git command calls from 5->1

before

~/w/demo (6.1.1-post*↓) ☕️  time fish_prompt
~/w/demo (6.1.1-post*↓) ☕️
________________________________________________________
Executed in  454.87 millis    fish           external
   usr time   33.16 millis    4.14 millis   29.03 millis
   sys time   69.11 millis   10.09 millis   59.02 millis
~/w/demo (6.1.1-post*↓) ☕️ fish --profile /tmp/profile -c fish_prompt; sort -nk1 /tmp/profile | tail -n 10
270	1434	-> printf '%s%s%s' (set_color $fish_color_cwd) (prompt_pwd) (set_color normal)
289	453989	-> printf '%s%s ☕️ ' (git_prompt) (kubectl_context)
562	574	---> source /usr/local/Cellar/fish/3.3.1/share/fish/functions/prompt_pwd.fish
3057	3057	------> command grep --color=always -I $argv
3178	3178	------> command grep --color=always -I $argv
74231	74231	----> git show-ref stash --quiet
77551	77551	-----> git symbolic-ref --short HEAD 2>/dev/null
94570	94570	----> git diff-files --no-ext-diff --quiet
98522	98522	----> command git status -s -b - ^ 2> /dev/null
100950	100950	-----> git ls-files --exclude-standard -o

after

~/w/demo (6.1.1-post*↓) ☕️  time fish_prompt
~/w/demo (6.1.1-post*↓) ☕️
________________________________________________________
Executed in  137.06 millis    fish           external
   usr time   20.18 millis    4.41 millis   15.77 millis
   sys time   33.62 millis    9.83 millis   23.79 millis
~/w/demo (6.1.1-post*↓) ☕️ fish --profile /tmp/profile -c fish_prompt; sort -nk1 /tmp/profile | tail -n 10
131	1255	--> prompt_pwd
135	135	----> string replace -r '^'"$realhome"'($|/)' '~$1' $PWD
210	129566	-> printf '%s%s ☕️ ' (git_prompt) (kubectl_context)
263	1629	-> printf '%s%s%s' (set_color $fish_color_cwd) (prompt_pwd) (set_color normal)
787	798	---> source /usr/local/Cellar/fish/3.3.1/share/fish/functions/prompt_pwd.fish
2901	2901	------> command grep --color=always -I $argv
2933	2933	------> command grep --color=always -I $argv
2942	2942	------> command grep --color=always -I $argv
3113	3113	------> command grep --color=always -I $argv
115859	115859	----> command git status -s -b 2> /dev/null

@ronaldsuwandi
Copy link
Author

updated to remove all grep calls and use fish' built in string matcher which is faster than grep. Now the bottleneck is only on git status

~/w/demo (6.1.1-post*↓) ☕️ fish --profile /tmp/profile -c fish_prompt; sort -nk1 /tmp/profile | tail -n 10
72	72	----> echo $git_status_origin | string match --regex '\[.*ahead.*\]' --quiet
86	118	---> set -l is_repo (string join0 $git_status_origin)
87	87	--> set_color $fish_color_cwd
112	102796	---> set -l git_status_origin (command git status -s -b 2> /dev/null)
131	131	----> string replace -r '^'"$realhome"'($|/)' '~$1' $PWD
135	712	--> prompt_pwd
198	104113	-> printf '%s%s ☕️ ' (git_prompt) (kubectl_context)
233	246	---> source /usr/local/Cellar/fish/3.3.1/share/fish/functions/prompt_pwd.fish
265	1082	-> printf '%s%s%s' (set_color $fish_color_cwd) (prompt_pwd) (set_color normal)
102684	102684	----> command git status -s -b 2> /dev/null

@ronaldsuwandi
Copy link
Author

upgraded to latest git and it's even faster now

~/w/demo (6.1.1-post*↓) ☕️  fish --profile /tmp/profile-oldgit -c fish_prompt; sort -nk2 /tmp/profile-oldgit | tail -n 10
64        200     ---> set -l tmp (string replace -r '^'"$realhome"'($|/)' '~$1' $PWD)
274     285     ---> source /usr/local/Cellar/fish/3.3.1/share/fish/functions/prompt_pwd.fish
151     768     --> prompt_pwd
48      867     ---> if test -z $is_repo...
272     1151    -> printf '%s%s%s' (set_color $fish_color_cwd) (prompt_pwd) (set_color normal)
40040   40040   ----> command git status -s -b 2> /dev/null
111     40151   ---> set -l git_status_origin (command git status -s -b 2> /dev/null)
27      41174   --> git_prompt
200     41435   -> printf '%s%s ☕️ ' (git_prompt) (kubectl_context)
23      42659   > fish_prompt

@ronaldsuwandi
Copy link
Author

Fixed git prompt for repo without origin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment