[TOC]

1. 介绍

macos的很多用户都是做it相关的人,类unix系统带来了很多方面,尤其是经常和Linux打交道的人。 但是作为经常使用linux 命令行的人发现macos中的命令行工具很多都是bsd工具,跟linux下得使用方式开始差距挺大的

Max OS X使用的是 BSD版本的命令行工具,这些命令跟 Linux版本是有些不同的,然而它们都遵循 POSIX 标准。

资料: http://blog.csdn.net/orangleliu/article/details/47357339

2. 终端设置

2.1 修改mac终端命令行前缀的显示

/etc/bashrc 中的PS1变量就是用来设置前缀显示的。

  1. 终端打开/etc/bashrc文件:

    sudo vim /etc/bashrc
    
  2. 修改PS1变量,例如修改为如下形式(把原来的注释掉:

    # PS1='\h:\W \u\$ '
    PS1='\u\$ '
    
  3. wq!保存并退出,将终端退出重新打开生效。

  4. 变量代表的含义

    \h表示本地主机名

    \W表示符号~

    \u表示用户名

    \$表示符号$

2.2 修复终端hostname被篡改为bogon

终端显示bogon原因:

  1. terminal显示hostname之前会先根据本机IP做一次rDNS反向查询,就是通过ip地址查询hostname,如果没有查询到,那么使用本机设置的hostname。本机IP通常是局域网IP地址(保留IP地址),一般是查不到的,所以terminal一般显示的本机设置的hostname,比如laowang's-macbook

  2. 上面提到,局域网IP地址一般是查不到hostname,是因为ISP提供商或者用户防火窗的屏蔽保留IP地址,因为保留IP地址在公网中没啥用,即便是没有被屏蔽掉,rDNS服务器一般也会关闭响应保留IP地址的查询请求。凡事都有例外,rDNS服务器对这种保留IP地址对查询一律返回bogon。在ipv4对地址划分中,除了公网分配在用对IP地址外,其余保留IP地址统一叫做bogon space

解决方法:

  1. 将DNS设置为Google的DNS服务器地址 8.8.8.8

  2. 终端输入如下命令:

    sudo hostname MBP
    sudo scutil --set LocalHostName $(hostname)
    sudo scutil --set HostName $(hostname)
    

    注:上面MBP就是你想要修改的新名字。

2.3 终端shell与zsh之间的切换

# 切换默认Shell为Zsh, 成功后需要重启终端
~ chsh -s /bin/zsh
# 切换Zsh为Shell, 成功后需要重启终端
~ chsh -s /bin/bash  
# 查看zsh配置的环境变量:
~ cat ~/.zshrc

2.4 设置终端使用命令打开vscode软件打开当前目录

1、找到打开软件的路径:

打开finder -> Applications(应用程序) -> 找到 vscode,右键,选择Show Package Contents(显示包内容) ….,一级一级的找下去,最终的文件类似于如下:

/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code

2、打开终端,进入用户根目录:

cd
# 或者
cd ~
# 找到.zshrc文件
la -al

3、打开.zshrc文件,在文件的末尾添加如下命令:

# 终端使用 code . 命令使用vscode打开当前目录
alias code='/Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin/code'

4、从新应用配置或重启终端后,终端打开一个项目文件,使用code .即可使用vscode打开项目。

source .zshrc

3. 快捷键

control + L        # 终端清屏,相当于 clear命令
open 文件名        # 使用默认程序打开文件 如 open . 使用finder打开该目录

3.1 终端光标做操快捷键

Mac Terminal终端和Linux上终端光标的快捷键操作是一样的,都是来自Emacs这个神级的编辑器,跟VIM不一样。

Ctrl + a        光标移动到行首(Ahead of line),相当于通常的Home键
Ctrl + e        光标移动到行尾(End of line)
Alt+← 或 ESC+B:左移一个单词;
Alt+→ 或 ESC+F:右移一个单词;
Ctrl + d        删除一个字符,相当于通常的Delete键(命令行若无所有字符,则相当于exit;处理多行标准输入时也表示eof)
Ctrl + h        退格删除一个字符,相当于通常的Backspace键
Ctrl + u        删除光标之前到行首的字符
Ctrl + k        删除光标之前到行尾的字符
Ctrl + c        取消当前行输入的命令,相当于Ctrl + Break
Ctrl + f        光标向前(Forward)移动一个字符位置
Ctrl + b        光标往回(Backward)移动一个字符位置
Ctrl + l        清屏,相当于执行clear命令
Ctrl + p        调出命令历史中的前一条(Previous)命令,相当于通常的上箭头
Ctrl + n        调出命令历史中的下一条(Next)命令,相当于通常的上箭头
Ctrl + r        显示:号提示,根据用户输入查找相关历史命令(reverse-i-search)

次常用快捷键:
Alt + f         光标向前(Forward)移动到下一个单词
Alt + b         光标往回(Backward)移动到前一个单词
Ctrl + w        删除从光标位置前到当前所处单词(Word)的开头
Alt + d         删除从光标位置到当前所处单词的末尾
Ctrl + y        粘贴最后一次被删除的单词

3.2 处理窗口和标签页快捷键

操作 快捷键
新建窗口 Command-N
使用相同命令新建窗口 Control-Command-N
新建标签页 Command-T
使用相同命令新建标签页 Control-Command-T
显示或隐藏标签页栏 Shift-Command-T
显示所有标签页或退出标签页概览 Shift-Command-反斜杠 ()
新建命令 Shift-Command-N
新建远程连接 Shift-Command-K
显示或隐藏检查器 Command-I
编辑标题 Shift-Command-I
编辑背景颜色 Option-Command-I
放大字体 Command-加号键 (+)
缩小字体 Command-减号键 (–)
下一个窗口 Command-重音符键 (`)
上一个窗口 Command-Shift-波浪符号 (~)
下一个标签页 Control-Tab
上一个标签页 Control-Shift-Tab
将窗口拆分为两个面板 Command-D
关闭拆分面板 Shift-Command-D
关闭标签页 Command-W
关闭窗口 Shift-Command-W
关闭其他标签页 Option-Command-W
全部关闭 Option-Shift-Command-W
滚动到顶部 Command-Home
滚动到底部 Command-End
上一页 Command-Page Up
下一页 Command-Page Down
上一行 Option-Command-Page Up
下一行 Option-Command-Page Down

4. 终端常用操作

4.1 查看进程

# 搜索特定进程,
~ ps aux|grep 进程名字
# 动态显示进程
~ top

4.2 查看端口号

# 搜索端口号为8080, 可以看见进程名字与ID
lsof -i:8080   
# 查看IPv4端口:(最好加 sudo)
~ lsof -Pnl +M -i4   

# 查看IPv6协议下的端口
lsof -Pnl +M -i6

~ sudo netstat antup

参考链接:

http://blog.csdn.net/u011619283/article/details/53116291
http://blog.csdn.net/winkenjiang/article/details/7588658


4.3 终端使用代理

4.3.1 终端临时使用代理,只对这个终端有效,关闭后失效:

export http_proxy=http://proxyAddress:port  

export http_proxy="http://127.0.0.1:1080"
export https_proxy="http://127.0.0.1:1080"

这个办法的好处是简单直接,并且影响面很小(只对当前终端有效)。

4.3.2 终端永久使用代理: 把代理服务器地址写入shell配置文件.bashrc或者.zshrc

直接在.bashrc或者.zshrc添加下面内容:

# vi ~/.ashrc    

export http_proxy="http://localhost:port"
export https_proxy="http://localhost:port"   

# 以使用shadowsocks代理为例,ss的代理端口为1080,那么应该设置为:   
export http_proxy="http://127.0.0.1:1080"
export https_proxy="http://127.0.0.1:1080"

localhost就是一个域名,域名默认指向 127.0.0.1,两者是一样的。
然后ESC后:wq保存文件,接着在终端中执行source ~/.bashrc
或者退出当前终端再起一个终端。 这个办法的好处是把代理服务器永久保存了,下次就可以直接用了。

4.4 终端进入名称带空格的目录

可使用\ ' ' " " 将空格转义就可以了。

1. cd Appications/Android\ Studio.app/sdk 
2. cd Appications/Android' 'Studio.app/sdk
3. cd Appications/Android" "Studio.app/sdk

4.5 终端刷新DNS缓存

sudo killall -HUP mDNSResponder

5. 终端一些命令安装

5.1 wget安装

wget是unix上一个发送网络请求的命令工具,不过mac本身并没有,mac自带的是curl,都是发送网络请求,但是两者之间肯定存在一些差异。一般来说,wget主要专注于下载文件,curl长项在于web交互、调试网页等。

需要使用homebrew进行安装,如果没安装请先安装这个

终端安装wget命令:

brew install wget --with-libressl

==> Installing dependencies for wget: libunistring, libidn2 and openssl
==> Installing wget dependency: libunistring
==> Downloading https://homebrew.bintray.com/bottles/libunistring-0.9.10.high_si
######################################################################## 100.0%
==> Pouring libunistring-0.9.10.high_sierra.bottle.tar.gz
🍺  /usr/local/Cellar/libunistring/0.9.10: 54 files, 4.4MB
==> Installing wget dependency: libidn2
==> Downloading https://homebrew.bintray.com/bottles/libidn2-2.0.5.high_sierra.b
######################################################################## 100.0%
==> Pouring libidn2-2.0.5.high_sierra.bottle.tar.gz
🍺  /usr/local/Cellar/libidn2/2.0.5: 68 files, 668.6KB
==> Installing wget dependency: openssl
==> Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2q.high_sierra.
######################################################################## 100.0%
==> Pouring openssl-1.0.2q.high_sierra.bottle.tar.gz
==> Caveats
A CA file has been bootstrapped using certificates from the SystemRoots
keychain. To add additional certificates (e.g. the certificates added in
the System keychain), place .pem files in
  /usr/local/etc/openssl/certs

and run
  /usr/local/opt/openssl/bin/c_rehash

openssl is keg-only, which means it was not symlinked into /usr/local,
because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries.

If you need to have openssl first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.zshrc

For compilers to find openssl you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl/include"

==> Summary
🍺  /usr/local/Cellar/openssl/1.0.2q: 1,794 files, 12.1MB
Warning: wget: this formula has no --with-libressl option so it will be ignored!
==> Installing wget
==> Downloading https://homebrew.bintray.com/bottles/wget-1.20.high_sierra.bottl
######################################################################## 100.0%
==> Pouring wget-1.20.high_sierra.bottle.tar.gz
🍺  /usr/local/Cellar/wget/1.20: 50 files, 3.9MB
==> Caveats
==> openssl
A CA file has been bootstrapped using certificates from the SystemRoots
keychain. To add additional certificates (e.g. the certificates added in
the System keychain), place .pem files in
  /usr/local/etc/openssl/certs

and run
  /usr/local/opt/openssl/bin/c_rehash

openssl is keg-only, which means it was not symlinked into /usr/local,
because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries.

If you need to have openssl first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.zshrc

For compilers to find openssl you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl/include"

➜  ~ 

~ wget --version
GNU Wget 1.20 built on darwin17.7.0.

参考资料:https://www.jianshu.com/p/f6b290710262

5.2 tree命令安装

tree命令用来在终端下显示目录树结构,在Mac下默认是没有安装该命令的。

需要使用终端使用brew命令来安装tree:

brew install tree

==> Downloading http://mama.indstate.edu/users/ice/tree/src/tree-1.7.0.tgz
Already downloaded: /Library/Caches/Homebrew/tree-1.7.0.tgz
==> make prefix=/usr/local/Cellar/tree/1.7.0 MANDIR=/usr/local/Cellar/tree/1.7.0/share/man/man1 CC=c
🍺  /usr/local/Cellar/tree/1.7.0: 7 files, 140K, built in 2 seconds

安装后可使用tree --help来查看使用帮助。

在使用过程中,如果目录中有中文文件,会显示乱码,可使用tree -N命令来解决。

参考资料

“终端”键盘快捷键

powered by Gitbook文件最后修改时间: 2021-05-19 17:03:21

results matching ""

    No results matching ""