Bash – How to change the target directory for tab completion

autocompletebashshell-script

I found a short script to enable note-taking on onethingwell, which includes this:

function n { nano ~/n/$1.txt }

I'd like to extend it to do tab completion, so that if I want to edit the pre-existing note 'foobar' (~/n/foobar.txt) I can type foo[TAB] and have it automatically complete to foobar, rather than having to type in 'foobar' every time. The problem is that bash's built-in tab completion seems to be centered around the current directory, so if I'm in a folder that isn't ~/n, then my tab completion fails — or, worse, completes to the wrong thing, since perhaps I have a file named 'footastic' in ~/snafu/ (meaning I type $ n foo[TAB], and instead of seeing $ n foobar, I look at my input and see: $ n footastic.

So, after all that preamble, here's the question: if it's possible, how can I tell bash that after I type 'n', for just this once I want it to assume that we're completing using the files in ~/n for completion, rather than using the files in the current directory?

If you understood all that e-mumbling, congratulations, and if you can answer that, thank you very much!

Best Answer

This might get you there...

  1. Enable custom tab completion in your .bashrc

    shopt -s progcomp

  2. Create a compgen function and add it to .bashrc after your notes functions.

    _notes() {
    local cur
        cur=${COMP_WORDS[COMP_CWORD]}
        COMPREPLY=( $(compgen -f $HOME/n/$cur | cut -d"/" -f5 ) )
        }
       complete -o filenames -F _notes n

This works for me - at the basic level. I still haven't solved it for nested directories...

References:

  1. most of it from here: http://sixohthree.com/867/bash-completion

  2. Completion builtin details here: http://www.gnu.org/software/bash/manual/html_node/Programmable-Completion-Builtins.html

Related Question