Any Bash features you'd like to see?

William Park opengeometry-FFYn/CNdgSA at public.gmane.org
Mon May 16 05:11:15 UTC 2005


On Fri, May 13, 2005 at 04:19:53PM -0400, William Park wrote:
> On Fri, May 13, 2005 at 03:39:14PM -0400, Sy wrote:
> > Unless I totally missed reading on this functionality..  I'd like to
> > be able to type a partial command which exists in my history, and then
> > press up/down to cycle through the entries in history which partially
> > match what I typed.
> > 
> > cd /foo
> > echo bar
> > 
> > echo <up>  # becomes echo bar
> > cd <up> #becomes cd /foo
> 
> man bash
> man readline
>     - history-search-forward
>     - history-search-backward
>     - .inputrc

For searching through the history, above should be enough, eg.

    C-p: history-search-backward
    C-n: history-search-forward

which can be bound to some other keys of your choice.

For searching through previously visited directories, it's more
complicated.  You can easily build up a large list.  So, you need 
    - some random access into "partial match", or
    - moving sequentially for few last directories that you can actually
      remember.

The key insight is "Programmable Completion" (man bash).  To make long
story short, put this in ~/.profile.

    _cd ()
    {
	local cur=${COMP_WORDS[$COMP_CWORD]}

	COMPREPLY=( $(compgen -W "`dirs`" -- $cur) )
    }
    cd () { pushd "${1:-$HOME}" ; }
    alias b='pushd'		# swap current and previous
    alias p='popd'		# pop the current, and go back to previous

    complete -o dirnames -F _cd cd
    complete -d popd pushd

1.  - 'cd' will push the new directory onto stack, 
    - 'p' will pop and move to previous directory.
    - 'b' will toggle between current and previous, ie. swap the 2 items
      on the stack.

2.  When you press <Tab>, eg.
	cd /usr<TAB>
    it will try to complete on '/usr' from 'dirs' output.  If no
    completion is possible, then only the directories will be listed.

Main problem is that if your 'dirs' has
    /usr/local/src/bash ~
then typing
    cd /<Tab>
will return
    cd /usr/local/src/bash
This is okey if you wanted to go to '/usr/local/src/bash', but not if
you wanted some other directory under '/', like '/var/log'.  For the
latter case, you have to type more chars, eg.
    cd /v<Tab>
    
-- 
William Park <opengeometry-FFYn/CNdgSA at public.gmane.org>, Toronto, Canada
ThinFlash: Linux thin-client on USB key (flash) drive
	   http://home.eol.ca/~parkw/thinflash.html
--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list