LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-16-2006, 03:28 AM   #1
sadarax
Member
 
Registered: Sep 2005
Distribution: Ubuntu
Posts: 252

Rep: Reputation: 30
Change Directory Command Tabbing Issue


Is there a way within the bash shell to make the 'cd' (change directory) command and tabbing only present you with directories as options?

What I mean is this, say my current working directory has these contents:

Code:
gprename-1.24/
gprename_0.92-1_all.deb
gprename-1.24.tar.bz2
install.txt
gprename-icons-1.0.tar.bz2
When I use the 'cd' command with a 'g' and then hit tab (literally 'cd g' then tab twice), I expect it to only show me directory options in the list.

In fills out the 'g' with 'gprename' and shows me:

Code:
gprename_0.92-1_all.deb     gprename-1.24.tar.bz2
gprename-1.24/              gprename-icons-1.0.tar.bz2
Now, there is only one directory there. I want to know if I can make cd only give me the option of using directories when I double tab.

BTW, I am sure this has been posted about in threads already, but the search criteria is too general for me to find anything.
 
Old 01-16-2006, 04:05 AM   #2
WindowBreaker
Member
 
Registered: Oct 2005
Distribution: Slackware
Posts: 228

Rep: Reputation: 40
Now I may be wrong, but I don't think bash will do that. Remember that in linux directories are a type of file. They also have an inode number and are treated [almost] the same.

I know one thing that alters how tab completion works is the permissions bit mask of the file name. If there are the following two files in a directory:
Quote:
-rw-r--r-- 1 pablo users 0 2006-01-16 02:00 test
-rwxr-xr-x 1 pablo users 0 2006-01-16 02:00 test2*
And you type in : ./te[TAB] , then it will fill in test2 , not test, because it (bash) knows that test is not executable, but test2 is.

If you find a way post it.
 
Old 01-16-2006, 06:23 AM   #3
scott_R
Member
 
Registered: Jul 2003
Location: Brighton, Michigan, USA
Distribution: Lots of distros in the past, now Linux Mint
Posts: 748

Rep: Reputation: 31
This is extracted from Mepis' /etc/bash_completion file, but you'll need to alter it to fit your system. (And obviously, a bit of knowledge of bash scripting)


# This meta-cd function observes the CDPATH variable, so that cd additionally
# completes on directories under those specified in CDPATH.
#
_cd()
{
local IFS=$'\t\n' cur=${COMP_WORDS[COMP_CWORD]} i j k

# try to allow variable completion
if [[ "$cur" == ?(\\)\$* ]]; then
COMPREPLY=( $( compgen -v -P '$' -- "${cur#?(\\)$}" ) )
return 0
fi

# Use standard dir completion if no CDPATH or parameter starts with /,
# ./ or ../
if [ -z "${CDPATH:-}" ] || [[ "$cur" == ?(.)?(.)/* ]]; then
_filedir -d
return 0
fi

local -r mark_dirs=$(_rl_enabled mark-directories && echo y)
local -r mark_symdirs=$(_rl_enabled mark-symlinked-directories && echo y)

# we have a CDPATH, so loop on its contents
for i in ${CDPATH//:/$'\t'}; do
# create an array of matched subdirs
k=${#COMPREPLY[@]}
for j in $( compgen -d $i/$cur ); do
if [[ ( $mark_symdirs && -h $j || $mark_dirs && ! -h $j ) && ! -d ${j#$i/} ]]; then
j="${j}/"
fi
COMPREPLY[k++]=${j#$i/}
done
done

_filedir -d

if [[ ${#COMPREPLY[@]} -eq 1 ]]; then
i=${COMPREPLY[0]}
if [ "$i" == "$cur" ] && [[ $i != "*/" ]]; then
COMPREPLY[0]="${i}/"
fi
fi

return 0
}
if shopt -q cdable_vars; then
complete -v -F _cd $nospace $filenames cd
else
complete -F _cd $nospace $filenames cd
fi
 
Old 01-17-2006, 02:50 PM   #4
sadarax
Member
 
Registered: Sep 2005
Distribution: Ubuntu
Posts: 252

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by scott_R
This is extracted from Mepis' /etc/bash_completion file, but you'll need to alter it to fit your system. (And obviously, a bit of knowledge of bash scripting)

_cd()
{
local IFS=$'\t\n' cur=${COMP_WORDS[COMP_CWORD]} i j k
...
complete -F _cd $nospace $filenames cd
fi
Thanks for the code. I looked over my /etc/bash_completion file. In the _cd() section, the code in my file is the same as the code you gave me. So, we still have not anywhere yet.

I used Mepis in the past so that may be where I remember the proper cd command completion from.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Tabbing in Fluxbox hardknox Linux - Software 2 02-19-2006 03:38 PM
Change directory command gizmobay Linux - Newbie 4 11-23-2005 06:11 PM
Resetting the tabbing in Konsole KissDaFeetOfSean Linux - Newbie 0 06-15-2005 09:37 PM
chmod command to change all files in directory paul_mat Linux - Newbie 2 01-10-2005 09:05 PM
mountig - change ro to rw, delete directory, change rw to ro ?? itsjustme Linux - General 6 10-17-2003 11:48 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration