如何在 bash 命令中自动完成或使用缩写

How to autocomplete or use abbreviations in bash command

每天我都必须通过 SCP 从我的 bash 终端在 Linux Debian 8 上将文件传输到许多 (6) 个 AWS EC2 实例。我必须手写:

i) files to transfer;  

ii) destination folder;  

iii) url to my .pem certificate;   

iv)  openning urls on the servers: ec2-user@XX.XX.XXX....

作为一个懒惰的程序员,我想使用某种缩写来减少输入,记住 iii) 和 iv) 通常是相同的。

首先我写了一个 bash 脚本,它要求我在我的服务器上传输文件和文件夹,但是由于每次传输时都会提示回答,所以需要更多时间。相反,我可以只使用箭头重复前面介绍的 bash 命令。

这是我用来 运行:

的当前命令的示例
scp -i /pem/aws.pem file_to_upload.txt ec2-user@XX-XX-XX-XX.amazonwas.com:/var/www/html/folder/

以下字符串需要重复输入:

scp -i /pem/aws.pem
ec2-user@.....  // this is a large string of 64 characters.

我喜欢某种占位符,我只需在其中键入要上传的文件的路径和远程文件夹。

如何自动完成或使用缩写,例如 VIM 在 bash 命令中插入相同的重复文本?

听起来您可以在 bashrc 或 bash_profile.

中使用一些 bash 别名

Take a look at my .bashrc file on githut

我经常需要知道我的IP地址是什么。 bash 别名的一个例子是我的 showip 别名。

alias showip='ifconfig | grep "inet" | grep -v 127.0.0.1'

我还创建了别名以通过 ssh 进入我的 Linux 框。您可以创建一个 bash 别名来解决您的问题。

正在从主机递归复制目录:

scp -r user@host:/directory/SourceFolder TargetFolder

注意:如果主机使用端口 22 以外的端口,您可以使用 -P 选项指定它:

scp -P 2222 user@host:/directory/SourceFile TargetFile

当您使用 ssh 程序连接到 bash 终端时,请查看您的 ssh 支持的键盘映射。
其他选项,基于

echo "This is a line I do not want to type twice"

看看如何使用 bash 历史记录

!!
^twice^two times^

您可以将缩写放在 shell 个变量中

no2="This is a line I do not want to type twice"
echo "${no2}"

你可以使用别名

my2='echo "This is a line I do not want to type twice"'
my2

shell变量和别名可以放在${HOME}/.bashrc.

已经发布的所有解决方案都有效,但它们真的很难看,而不是应该的样子。请记住,我们还有 ~/.ssh/config?

scp -i /pem/aws.pem file_to_upload.txt ec2-user@XX-XX-XX-XX.amazonwas.com:/var/www/html/folder/

将归结为

scp file_to_upload.txt am:/var/www/html/folder/

如果您设置 ~/.ssh/config:

Host am:
  Hostname XX-XX-XX-XX.amazonwas.com
  User ec2-user
  IdentityFile /pem/aws.pem

如果您不使用密码短语(或设置 ControlMasterControlPersist 选项,您的本地文件也会获得 auto-completed 和远程目录 - 这甚至会很快!) .