简体   繁体   English

bash变量扩展,用于完成scp选项卡

[英]bash variable expansion for scp tab completion

I use ssh at work and recently switched to Linux on my local work machine. 我在工作中使用ssh,最近在本地工作计算机上切换到Linux。 As a result, I now have to fully-qualify the hostname (ie instead of dev1 I need to use dev1.megacorpnetworkdomain.com). 结果,我现在必须完全限定主机名(即,我需要使用dev1.megacorpnetworkdomain.com代替dev1)。 This gets rather annoying for scp. 这对于s​​cp来说相当烦人。 I already have auto-completion set up for remote paths. 我已经为远程路径设置了自动补全功能。 What I tried to do is create a variable alias for the path: 我试图做的是为路径创建变量别名:

WORK='user@dev1.megacorpnetworkdomain.com:/home/user'

The problem is now scp fails to do remote completion when I do the following: 现在的问题是,当我执行以下操作时,scp无法执行远程完成:

scp $WORK/<tab>

Whereas the following performs remote completion correctly: 而以下内容可以正确执行远程完成:

scp user@dev1.megacorpnetworkdomain.com:/home/user/<tab>

I tried to naively work around this by using complete : 我试图通过使用complete天真地解决此问题:

_scp_expand_vars () {
    COMPREPLY=()
    local cur=${COMP_WORDS[COMP_CWORD]}
    eval cur=$cur
    COMPREPLY=( $cur )
    return 0
}
complete -F _scp_expand_vars scp

This now correctly expands variables for scp, but breaks the original functionality of remote directory expansion. 现在,这可以正确地为scp扩展变量,但是会破坏远程目录扩展的原始功能。 How could I achieve both? 我怎样才能做到? I'm assuming the original functionality is handled by _scp_remote_files function based on a bit of online research? 我假设原始功能由_scp_remote_files函数基于一些在线研究处理?

For one, if you regularly access hosts under megacorpnetworkdomain.com, you should put it in /etc/resolv.conf; 首先,如果您定期访问megacorpnetworkdomaindomain.com下的主机,则应将其放在/etc/resolv.conf中。 there should be a line: 应该有一行:

search megacorpnetworkdomain.com

Then you can use all hosts by just referencing the short hostname, ie dev1 in any program. 然后,您可以通过引用短主机名(即在任何程序中使用dev1)来使用所有主机。

For ssh you can follow the advice of Etan Reiser, an example looks like this: 对于ssh,您可以遵循Etan Reiser的建议,示例如下所示:

Host dev1
    HostName dev1.megacorpnetworkdomain.com
    User user

When you use ssh or scp they will offer you the choices defined there with tab completion as usual. 当您使用ssh或scp时,它们将为您提供在那里定义的选项,并且像往常一样使用制表符补全。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM