如何在 Fish 中使用快捷键扩展环境变量?

How to expand environment variables using keyboard shortcut in Fish?

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

$ echo $PATH
/home/joe

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

在 Fish 中是否有实现类似功能的捷径?

你可以这样做

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 函数(如果不存在则创建它):

Key bindings are not saved between sessions by default. Bare bind statements in config.fish won't have any effect because it is sourced before the default keybindings are setup. To save custom keybindings, put the bind statements into a function called fish_user_key_bindings, which will be autoloaded.


好看一点:

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