【问题标题】:How to expand environment variables using keyboard shortcut in Fish?如何在 Fish 中使用键盘快捷键扩展环境变量?
【发布时间】:2018-05-10 18:32:54
【问题描述】:

在 Bash 中,快捷方式 Esc Ctrl-e 可用于在 shell 中扩展环境变量:

$ echo $PATH
/home/joe

$ $PATH<Press Esc Ctrl-e>
$ /home/joe

有没有捷径可以在 Fish 中实现类似的东西?

【问题讨论】:

标签: fish tab-completion


【解决方案1】:

你可以这样做

function bind_expand_all
    # what are the tokens of the current command line
    set tokens (commandline --tokenize)
    # erase the current command line (replace with empty string)
    commandline -r ""
    for token in $tokens
        # append the expanded value of each token followed by a space
        commandline -a (eval echo $token)" "
    end
    # move the cursor to the end of the new command line
    commandline -C (string length (commandline))
end

然后

bind \e\ce bind_expand_all

如果这是您当前的命令行(光标在下划线处):

$ echo $HOME (date -u)_

当你按下 AltCtrle 时,你会得到

$ echo /home/jackman Thu May 10 19:27:18 UTC 2018 _

要永久存储该绑定,请将其添加到您的 fish_user_key_bindings 函数中(如果不存在则创建它):

默认情况下,会话之间不保存键绑定。 config.fish 中的 bind 语句不会产生任何影响,因为它是在设置默认键绑定之前获取的。 要保存自定义键绑定,请将 bind 语句放入名为 @987654329 的函数中@,它将被自动加载。


更好一点:

function bind_expand_all
    set -l expanded
    for token in (commandline --tokenize)
        set expanded $expanded (eval echo $token)
    end
    set -l new (string join " " $expanded)
    commandline -r $new
    commandline -C (string length $new)
end

【讨论】:

  • 没什么花哨的:创建扩展标记的列表,用空格连接的列表替换当前命令行。
猜你喜欢
  • 2011-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多