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

How to invoke time command with parameter and same behavior as without parameter?
C programming: Redirect the output of an encryption program into a .txt file [closed]
command line argument in shell
How do you grep a file and get the next 2 lines with a tab?
How to compare 2 files and extract information [closed]
How to sort the file without delimiter and extract the min and Max value
Formatting calendar in Linux Terminal
How to find which users logged in in the last 30 minutes UBUNTU 16.04
Fetch data from a file
Can I automate ssh connection? ssh user#server.company.com
Oozie— java: command not found - shell action
How to execute system command with an argument that contains spaces
Why the shell doesn't write the good hour and give me the year?
shell script with dynamic variable
Trying to get files from today date and result is displaying yesterday date files as well (Unix Korn Shell)
Nagios plugin that runs at 2pm daily

Categories

HOME
batch-file
sonarqube
nvd3.js
formal-languages
angular2-material
serialization
solaris
owl-carousel
google-apps
mips
hugo
interpolation
mvc5
octave
watson-conversation
naivebayes
modbus-tcp
google-content-api
enterprise-miner
cgbitmapcontext
gulp-watch
exit
gpio
appirater
intersystems-cache
bringtofront
mql
vex
dotspatial
testbed
playstation-portable
rselenium
browser-extension
baidu-map
hivemq
amazon-ses
connection-pool
uiimage
linter
configurationmanager
google-cse
singleinstance
video-conferencing
subnet
otp
quicktime
type-theory
google-photos
cronexpression
docpad
angular2-testing
meteor-packages
grails-plugin
custom-lists
seek
expectations
nupic
contentcontrol
character-replacement
tfs-workitem
poppler
.bash-profile
iced-coffeescript
vacuum
graphics2d
cwrsync
actionfilterattribute
procedural-programming
document-repository
delayed-execution
cosine-similarity
webmatrix-3
android-cookiemanager
mruby
cbind
ray
twitter-bootstrap-rails
websphere-esb
cfcache
koala
amslidemenu
gd-graph
ebay-lms
beaker-testing
octal
traceability
onload-event
insert-id
alice
pdfviewer
patricia-trie
sentestingkit
xcode3.2
dajaxice
jsr286
urlrewriter
gobject-introspection
http-daemon
nhprof
updatesourcetrigger
drupal-fivestar
entitykey
mathematical-typesetting
great-circle

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