gpt4 book ai didi

Bash Completion how to ask it not to append a SPACE after current word is completed(如何让它在当前单词完成后不追加空格)

转载 作者:bug小助手 更新时间:2023-10-27 20:03:08 39 4
gpt4 key购买 nike



In bash by default, pressing Tab will show all files and directories in the current directory. For example:

在bash中,默认情况下,按Tab键将显示当前目录中的所有文件和目录。例如:


cat a<tab><tab> would display something like aFile.txt apples.png aDirectory/

Cat a 将显示类似aFile.txt apples.png aDirectory/的内容


If you then completed to aDirectory, it will show the contents of aDirectory, for example:

如果您随后完成了a目录,它将显示a目录的内容,例如:


cat aDirectory/<tab><tab> might display file.txt file.mp4

cat aDirectory/ 可能显示file.txt file.mp4


It does this while keeping the cursor on the current argument. So pressing <tab><tab> on cat aD would end with the cursor like this cat aDirectory/| (where the pipe is the cursor)

它在将光标保持在当前参数上的同时执行此操作。因此,在cat ad上按 将以这样的光标结束,就像这个cat a目录/|(其中竖线是光标)


I've been trying to make my own bash completion scripts, and one is for a log program. This program is broken down into months in individual files. So 2308, 2309 etc. What I want is that when I press <tab><tab>, until 4 characters are filled, it will display only the year and months, so 2308 2309, but when that is filled (log 2308|), pressing <tab><tab> would then show the individual days in the file (so 230801 230803 230807 for example).

我一直在尝试制作自己的bash完成脚本,其中一个是用于日志程序的。该计划在各个文件中被分解为几个月。我想要的是,当我按 ,直到填满4个字符时,它将只显示年和月,因此23082309,但当它被填充时(LOG 2308|),按 将显示文件中的各个日期(例如,230801 230803 230807)。


I managed to get this working, however the issue is if tab autocompletes (for example if the only options were 2308, 2309 and 2310, then hitting <tab> on 231 would autocomplete to 2310), the cursor jumps to the next argument (log 2310 | instead of log 2310|) unlike the default behaviour with directories.

我设法让它正常工作,但问题是,如果制表符自动完成(例如,如果仅有的选项是2308、2309和2310,那么点击231上的 会自动完成到2310),游标跳到下一个参数(日志2310|而不是日志2310|),这与目录的默认行为不同。


Then of course pressing tab does nothing, as it is no longer on ${COMP_WORDS[1]}. How can you get the cursor to not jump to the next argument, until some condition is met (e.g. ${COMP_WORDS[1]} is a file, or ${COMP_WORDS[1]} is 6 characters long)?

当然,按Tab键不会起任何作用,因为它不再位于${comp_words[1]}上。如何才能使光标不跳转到下一个参数,直到满足某些条件(例如,${comp_words[1]}是一个文件,或者${comp_words[1]}是6个字符)?


This is the full function:

以下是完整的功能:


_logCompletion() {
if [ "${#COMP_WORDS[@]}" != "2" ]; then
if [ "${COMP_WORDS[1]}" == "-d" ] && [ "${#COMP_WORDS[@]}" == "3" ]; then # If -d was chosen, then display the avalible years/months
if [ ${#COMP_WORDS[2]} -ge 4 ]; then # If month already filled, start showing avalible days
curLogMonth=${COMP_WORDS[2]:0:4} # Get month in yymm format
[ -f ~/Programs/output/log/${curLogMonth}.json ] || return 0 # Check that month exists, if not, don't continue
curMonthSuggestions=$(cat ~/Programs/output/log/$curLogMonth.json | jq -r 'keys[]' | sed "s/\(.*\)/$curLogMonth\1/g") # Get the days avalible in given month
COMPREPLY=( $(compgen -W "$curMonthSuggestions" -- "${COMP_WORDS[2]}") )
return 0
else # Otherwise just show avalible months
COMPREPLY=( $(compgen -W "$(ls ~/Programs/output/log/ | grep -oE [0-9]*)" -- "${COMP_WORDS[2]}") )
return 0
fi
elif [ "${COMP_WORDS[1]}" == "-ds" ] && [ "${#COMP_WORDS[@]}" == "3" ]; then # If -ds was chosen, show avalible months
COMPREPLY=( $(compgen -W "$(ls ~/Programs/output/log/ | grep -oE [0-9]*)" -- "${COMP_WORDS[2]}") )
fi
return 0
fi
COMPREPLY=( $(compgen -W "-h -p -d -ds -s -f -fa" -- "${COMP_WORDS[1]}") )
}

更多回答

This behaviour is controlled with complete -o nospace, telling readline to not append a blank after completion.

这种行为是用complete -o nospace控制的,告诉readline在完成后不要追加空白。

优秀答案推荐

Option #1


complete -o nospace -F _logCompletion log

This way it would even not automatically append a SPACE when your date is already 6-digits long.

这样一来,当你的日期已经有6位数时,它甚至不会自动追加一个空格。


Option #2


Update the else ... block like this:

更新Else...按如下方式阻止:


else # Otherwise just show avalible months
COMPREPLY=( $(compgen -W ... ) )
if [[ ${#COMPREPLY[@]} == 1 ]]; then
COMPREPLY[1]="${COMPREPLY[0]}/"
# ^ (other char is also fine)
fi
return 0
fi

This way yourself can control when you want it to auto append a SPACE.

这样,您就可以控制它何时自动追加一个空格。


更多回答

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com