美文网首页
Mac终端技巧

Mac终端技巧

作者: GoddyWu | 来源:发表于2019-02-27 20:06 被阅读0次

mac command alias

mac环境变量配置在~/.bash_profile文件里(相当于linux的~/.bashrc),将以下代码添加进去,source即可。

alias test='echo test mac command alias success!'
然后我们执行test,

SSH Config File

ssh config的位置是~/.ssh/config,如果不存在的话可以自己创建一下。

上面👆的博客介绍的很清楚。举个栗子,$ ssh -i ~/config/k8s-nx.pem -p 50001 ubuntu@ec2-52-83-181-77.cn-northwest-1.compute.amazonaws.com.cn可以转化为以下:

Host ruyi
   HostName ec2-52-83-181-77.cn-northwest-1.compute.amazonaws.com.cn
   Port 50001
   User ubuntu
   IdentityFile ~/config/k8s-nx.pem

配置完,其他相关命令也会非常简单:

  • ssh port forward:$ ssh -NL 3307:127.0.0.1:3306 ruyi
    • 3307为转发到本地的端口
    • 127.0.0.1为内网ip地址
    • 3306为内网端口
  • scp: $ scp docker-compose.yaml ruyi:/data

Mac SSH Autocomplete

这个功能就是在我们使用ssh时帮助我们自动补全。
mac环境变量配置在~/.bash_profile文件里,将以下代码添加进去,source即可。

_complete_ssh_hosts ()
{
        COMPREPLY=()
        cur="${COMP_WORDS[COMP_CWORD]}"
        comp_ssh_hosts=`cat ~/.ssh/known_hosts | \
                        cut -f 1 -d ' ' | \
                        sed -e s/,.*//g | \
                        grep -v ^# | \
                        uniq | \
                        grep -v "\[" ;
                cat ~/.ssh/config | \
                        grep "^Host " | \
                        awk '{print $2}'
                `
        COMPREPLY=( $(compgen -W "${comp_ssh_hosts}" -- $cur))
        return 0
}
complete -F _complete_ssh_hosts ssh

这个时候,我们ssh r,按tab键,发现自动补全了、!

使用多个配置文件

创建~/.bash_profile_autocomplete文件,可以将上面👆的内容添加进去。
然后在~/.bash_profile加入一行source ~/.bash_profile_autocomplete,执行$ source ~/.bash_profile就可以了、!


tree

# 安装命令
$ brew install tree

# 忽略某些文件,然后以树形结构打印当前目录及其子目录的文件
$ tree -I '*node*|*p'

相关文章

网友评论

      本文标题:Mac终端技巧

      本文链接:https://www.haomeiwen.com/subject/iamfuqtx.html