Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Bhupesh-V
Last active August 17, 2020 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bhupesh-V/a6854ea193b0fce7a57d85ef0d133340 to your computer and use it in GitHub Desktop.
Save Bhupesh-V/a6854ea193b0fce7a57d85ef0d133340 to your computer and use it in GitHub Desktop.
scd : switch directories from anywhere to anywhere
#!/usr/bin/env bash
# How to use:
#
# 1. Start a new bash session
# 2. source the completions script "source scd-completions.bash" or Copy the script in /etc/bash_completion.d/
# for automatic loading
# 3. scd <search-string>[TAB][TAB]
#
# For any queries reach out : varshneybhupesh@gmail.com
_scd_completions(){
local cur=${COMP_WORDS[COMP_CWORD]}
local IFS=$'\n'
# handle space separated dir names
dqc='"'
while read -r file_path; do
[[ -d $file_path ]] && search_results+=( "$dqc$file_path$dqc" )
done < <( locate -er "/$cur$" )
if [[ ${#search_results} == 0 ]]; then
while read -r file_path; do
[[ -d $file_path ]] && search_results+=( "$dqc$file_path$dqc" )
# do loose search
done < <( locate -ebr "$cur" )
fi
COMPREPLY=("${search_results[@]}")
unset search_results
}
complete -F _scd_completions scd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment