简体   繁体   English

Bash TAB完成双引号字符串

[英]Bash TAB-completion inside double-quoted string

Problem 问题

I'm writing a Twitter client for the command line (in C). 我正在为命令行编写一个Twitter客户端(在C中)。 I'm currently working on doing TAB-completion for Twitter screen names, like so: 我正在为Twitter屏幕名称做TAB完成,如下所示:

tweet "@s<TAB> 推特“@s <TAB>
@sourcebits @spolsky @sourcebits @spolsky

However, I can't get it to work mid-string, eg: 但是,我不能让它在mid-string中工作,例如:

tweet "Foo bar @s<TAB> 推特“Foo bar @s <TAB>

since Bash treats the string as one word. 因为Bash将字符串视为一个单词。 I couldn't find anything in the Bash man page suggesting a simple fix, so I decided to hack around it. 我在Bash手册页中找不到任何建议简单修复的东西,所以我决定破解它。 Below is my half-done solution to the problem. 以下是我对该问题的半成品解决方案。 I simply split the incoming string by spaces, take the last word (simplification, for now) and send it to compgen (the variable $last_word below). 我简单地用空格分割传入的字符串,取最后一个字(现在简化)并将其发送到compgen (下面的变量$last_word )。 However, I need to append $prefix to the beginning of the string that the TAB-completion produces, as it replaces the whole input string (remember: it's treated as one word). 但是,我需要将$prefix附加到TAB完成产生的字符串的开头,因为它替换了整个输入字符串(记住:它被视为一个单词)。 That's where I'm stuck. 那就是我被困住的地方。

Question

How can this be done? 如何才能做到这一点?

Code etc. 代码等

__cltwitter_complete () {
  local cache="$HOME/.cltwitter_users.cache"
  local string=${COMP_WORDS[COMP_CWORD]}
  local last_word=${string##* }
  local prefix=${string% *}

  COMPREPLY=()

  #if [ ! -f ${cache} ] || [ "`find ${cache} -mmin +60`" != "" ]; then
  #  cltwitter-update-cache
  #fi

  if [[ "$last_word" == \"@* ]]; then  # if word is beginning of a quotation
    last_word=${last_word:2}
  elif [[ "$last_word" == @* ]]; then  # if user put '@' in front
    last_word=${last_word:1}
  fi

  COMPREPLY=( $( compgen -W "`cat $cache`" -P @ -- $last_word ) )
}

complete -F __cltwitter_complete tweet

Relevant section from the Bash man page: Bash手册页的相关部分:

COMP_WORDS COMP_WORDS

An array variable (see Arrays below) consisting of the individual words in the current command line. 一个数组变量(参见下面的数组),由当前命令行中的各个单词组成。 The words are split on shell metacharacters as the shell parser would separate them . 这些单词在shell元字符上被拆分, 因为shell解析器会将它们分开 This variable is available only in shell functions invoked by the programmable completion facilities. 此变量仅在可编程完成工具调用的shell函数中可用。

查看可编程完成: http//www.faqs.org/docs/bashman/bashref_103.html

Do you really need the quotes? 你真的需要报价吗?

For example, your command could be: 例如,您的命令可能是:

tweet Foo bar @s<TAB>

Then @s would simply be the word you are trying to complete, and you can search for that. 然后@s只是你想要完成的单词,你可以搜索它。 Inside the script, you can still get at the entire string with $*. 在脚本内部,您仍然可以使用$ *获取整个字符串。

You'll have to be more careful invoking now, though, as you can't use special characters such as ! 但是,你现在必须更加小心地调用,因为你不能使用特殊的字符,比如! and ( ) anymore without having to escape them with a backslash. 和()不再需要用反斜杠来逃避它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 bash 中双引号字符串中的单引号不受支持 - Single quotes inside a double-quoted string in bash not honored 忽略带有bash tab-completion的路径条目 - Ignore a path entry with bash tab-completion 更改 bash 中内置读取的制表符补全 - Changing tab-completion for read builtin in bash Bash终端中的多个制表符完成例程 - Multiple tab-completion routines in Bash terminal 当我回显bash中较大的双引号字符串中的&#39;[table.20121128]&#39;时,其计算结果为&#39;1&#39; - '[table.20121128]' in a larger, double-quoted string in bash is evaluating to '1' when I echo it 如何用sed替换双引号字符串中的所有变量之前的所有美元符号? - How to replace all dollar signs before all variables inside a double-quoted string with sed? Bash导入的变量在双引号时会破坏配置脚本 - Bash imported variables break configure script when double-quoted 如何在bash中启用命令行开关的tab-completion? - How to enable tab-completion of command line switches in bash? 可以在 zsh 中使用 Bash 选项卡完成脚本吗? - Can a Bash tab-completion script be used in zsh? 我通过完整命令使用制表符完成编写了一个 bash 文件,但在 zsh 中它不起作用 - I write a bash file using tab-completion by complete command, but in zsh it didn't work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM