shell


Alias “cd” so that it will automatically autocomplete and attempt to move to directory


I'm wondering if anyone can do this. Say I have this folder structure:
Folder A
Folder Apple
Folder Orange
If I am currently in Folder A, I'd like it so that if I type "cd Ap" and hit enter, it'll automatically put me in the "Apple" subfolder. Essentially, it would attempt to autocomplete and open the folder based off the partial input.
If I am currently in Folder A, and I type "cd ap" and hit enter (lowercase "a"), I would get an error because it couldn't autocomplete to an actual subfolder name. Is this possible? I'm working in Korn.
I'm not quite going to answer your question, but I'll get close. It seems to me like hitting the tab key isn't the hurdle for you, it's the capitalization. I know the feeling, it's like choosing between camelCase and inconvenient typing.
I've only done this in bash, my apoligies. If I recall, bash and ksh are rather close, so I'm hoping it will work for you.
set completion-ignore-case on turns on case-insensitive completion in bash. Naturally this goes into any startup scripts you may want it in.
Good luck, tell us if it works in ksh !
Here's a ksh function (untested)
cd () {
typeset prefix=$1
typeset destination=""
for f in *; do
[[ -d "$f" ]] || continue
case "$f" in
"$prefix"*) destination="$f"; break ;;
esac
done
if [[ -z "$destination" ]]; then
print -u2 "error: can't find directory with prefix '$prefix'"
else
command cd "$destination"
fi
}
With ksh, Esc\ is the equivalent of bash tab-completion.
For Bash, you can add the following to your ~/.bashrc. By default, it will do case-insensitive matching. It's a bit long, but it should handle anything you throw at it (except for trying to autocomplete cd ../my_direc from a symlink directory (see here for more info on that).
If you use this script and leave it as case-insensitve, you might as well also add bind 'set completion-ignore-case on' to your ~/.bashrc so that TAB-completion is also case-insensitive.
cd() {
# Attempts to autocomplete the directory name
#
# If it fails to find a match, it'll still execute the input, in case the argument was
# something like "-".
case_insens=1 # set to one if you want it to try case-insensitive matching
# for exact matches, cd immediately
if [ -d "$1" ]; then
builtin cd "$1"
return
fi
# deal with no arguments passed
if [ $# -eq 0 ]; then
builtin cd
return
fi
# first loop for case-sensitive (since we prefer a case-sensitive match)
# for more on this globbing, see: bit.ly/1CZ9qym
for element in "$(dirname "$1")"/{*,.[!.]*,..?*}; do
# skip if this result is not a directory
[ -d "$element" ] || continue
if [[ "$(basename "$element")" == "$(basename "$1")"* ]]; then
# if there's no ambiguity, switch to that folder
if [ $(find -L "$(dirname "$1")" -maxdepth 1 -name "$(basename "$1")*" -type d 2>/dev/null | wc -l) -gt 1 ]; then
echo "'$1' matches multiple results: "
echo "$(find -L "$(dirname "$1")" -maxdepth 1 -name "$(basename "$1")*" -type d 2>/dev/null)"
# try to cd anyway
builtin cd "$1" &> /dev/null
unset case_insens element
return
else
builtin cd "$element"
unset case_insens element
return
fi
fi
done
if [ $case_insens -eq 1 ]; then
#case-insensitive argument
ci_arg="${1,,}"
else
builtin cd "$1"
unset case_insens element
return
fi
#Case-insensitive loop
for element in "$(dirname "$1")"/{*,.[!.]*,..?*}; do
# skip if this result is not a directory
[ -d "$element" ] || continue
ci_element_name="$(basename "${element,,}")"
if [[ "$ci_element_name" == "$(basename "$ci_arg")"* ]]; then
# if there's no ambiguity, switch to that folder
if [ $(find -L "$(dirname "$element")" -maxdepth 1 -iname "${ci_element_name}*" -type d 2>/dev/null | wc -l) -gt 1 ]; then
echo "'$ci_arg' matches multiple results: "
echo "$(find -L "$(dirname "$element")" -maxdepth 1 -iname "${ci_element_name}*" -type d 2>/dev/null)"
# try to cd anyway
builtin cd "$1" &> /dev/null
unset ci_arg case_insens ci_element element
return
else
builtin cd "$element"
unset ci_arg case_insens ci_element element
return
fi
fi
done
# we still haven't found a match, so pass the (faulty) argument to the cd command
builtin cd "$1"
unset ci_arg case_insens ci_element element
}
Example Usage
cd ~
cd deskt

Related Links

in shell how to combine rows between two strings into one row
How to Build a maven project using script file?
How to execute SWI-prolog command from shell?
Weird C-shell return code for evaluating 1-1
hadoop.sh cannot locate/access TEZ jars: No such file or directory error
Redirecting file descriptors
Extract words from a List in C Shell
What is the function of shell
tmux with C-shell (tcsh) alias are lost
Chaining 4 unix commands together and breaking the command to additional lines
Redirecting stderr
cat: not found when I execute ksh script
My command prompt terminal won't change?
Cygwin can't execute shell script
Reading from kshell output in same file
Can OpenSSL verify software integrity as GPG does?

Categories

HOME
scikit-learn
turing-machines
cobalt
gdb
forms
jmeter
apache-jena
serialization
apiblueprint
requirements
r-googlesheets
javacc
tvos
google-cloud-datalab
mvc5
angularfire2
teechart
intercom
symmetricds
xmonad
delphi-xe5
lilypond
named-entity-recognition
gmock
android-sharedpreferences
angularjs-components
android-maps
soot
iis-express
sqsh
zend-debugger
erb
modalviewcontroller
sim-toolkit
xcode7.3
watchman
git-rewrite-history
modernizr
gdata
comparison-operators
linqpad
congestion-control
rfc3339
serde
hammer.js
yii2-api
git-flow
java-websocket
janus
swingworker
chromium-os
actor-platform
dotnetnuke-7
mongodb-php
mta
i386
ng-grid
deep
aurora
toran-proxy
qt5.2
google-books
.bash-profile
finalbuilder
canvg
aspectj-maven-plugin
code-documentation
procedural-programming
cross-join
angular-gettext
uncaught-typeerror
named-parameters
parallel-testing
malformedurlexception
memset
android-framework
http-patch
jammer
cocoalibspotify-2.0
virtual-pc
wimax
sentestingkit
uimanageddocument
oracle-enterprise-linux
usertype
relative
workflow-services
matlab-load
catransform3d
quotation-marks
timertask
facebook-iframe
office-2007
glui
feasibility
mathematical-notation
sun
todos
business-model

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App