English 中文(简体)
Writing a custom bash-completion rule
原标题:

I have directories full of files with the same prefix, which I want to be able to quickly open in vim. For example, I might have:

$ ls *
bar:
bar_10  bar_20  bar_30

foo:
foo_10  foo_20  foo_30

What I want is to be able to be in one of these directories and type:

$ vim <TAB>

and it autocomplete to:

$ vim bar_

To achieve this I am happy to have a file per directory called ".completion" which has "bar_" in it.

The issue I have is I would like the following behaviour:

  * "vim <TAB>"  -->  "vim bar_"           // no space
  * "vim bar_1"  -->  "vim bar_10 "        // space

Where | is the cursor, so if a file matches, add the space on the end. If we re matching the prefix, don t add a space.

The best I have so far is this behaviour minus the adding a space at the end. I ve tried all sorts of things, all to no avail. The following is what I have:

_vim()
{
    local cur opts
    local -a toks

    cur="${COMP_WORDS[COMP_CWORD]}"

    if [ -f .completion ]; then
        opts=`cat .completion`

        if [[ ${opts} = ${cur} ]]; then
            toks=( $(compgen -f ${cur} | sed -e  s/$/ / ) )
        else
            if [[ -z ${cur} ]]; then
                toks=( $(compgen -W "${opts}" -- ${cur}) )
            else
                toks=( $(compgen -f ${cur} | sed -e  s/$/ / ) )
            fi
        fi
    else
        toks=( $(compgen -f ${cur} | sed -e  s/$/ / ) )
    fi

    COMPREPLY=( "${toks[@]}" )
}

complete -F _vim -o nospace vim

Any ideas on how I can get it to add the space after the file name completion, but not after the prefix completion would be greatly appreciated.

最佳回答

The trailing space that sed is adding is getting dropped. Try this:

saveIFS=$IFS
IFS=$ 
     # this will allow filenames with spaces (but not filenames with newlines)
toks=( $(compgen -f -- "${cur}" ))    # the -- protects against filenames that start with a hyphen
toks=("${toks[@]/%/ }")    # add a trailing space to each element
IFS=$saveIFS
问题回答

暂无回答




相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Bash usage of vi or emacs

From a programming standpoint, when you set the bash shell to use vi or emacs via set -o vi or set -o emacs What is actually going on here? I ve been reading a book where it claims the bash shell ...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Perform OR on two hash outputs of sha1sum

I want perform sha1sum file1 and sha1sum file2 and perform bitwise OR operation with them using bash. Output should be printable i.e 53a23bc2e24d039 ... (160 bit) How can I do this? I know echo $(( ...

Set screen-title from shellscript

Is it possible to set the Screen Title using a shell script? I thought about something like sending the key commands ctrl+A shift-A Name enter I searched for about an hour on how to emulate ...

热门标签