发布于 

终端模拟器入门指南

Linux 上叫 Terminal,macOS 上叫 iTerm,Android 上有 Termux

操作系统分为两个部分,一部分叫内核,另一部分叫用户交互界面
在UNIX/LINUX系统中,用户登录系统后会有一个Shell进程,它是一个用 C 语言编写的程序,既是一种命令语言,又是一种程序设计语言,它是管理系统的命令解析器,终端是敲命令的工具,是连接内核与交互界面的桥梁

美化终端

以 macOS 为例,安装 iTerm2

更换 主题

Preferences -> Profiles -> ColorPresets -> Import

导入 iTerm2-Color-Schemes/schemes/xxx.itermcolors

打开 Status bar

Preferences -> Profiles -> Session -> Status bar -> Configure Status Bar

终端一般默认的shell都是 bash,建议安装更换 zshfish

查看已安装的shell:cat /etc/shells

更换shell:chsh -s [shell路径]

zsh

MacOS 默认的shell为 bash,但自带了 zsh

  1. 安装 oh-my-zsh (切换到zsh后进行安装)

    1
    2
    3
    4
    5
    6
    7
    8
    Shell successfully changed to '/bin/zsh'.
    __ __
    ____ / /_ ____ ___ __ __ ____ _____/ /_
    / __ \/ __ \ / __ `__ \/ / / / /_ / / ___/ __ \
    / /_/ / / / / / / / / / / /_/ / / /_(__ ) / / /
    \____/_/ /_/ /_/ /_/ /_/\__, / /___/____/_/ /_/
    /____/ ....is now installed!
    Please look over the ~/.zshrc file to select plugins, themes, and options.
  2. 更换主题

    下载 powerlevel10k(需要安装 Hack Nerd Font 字体和配置)

    编辑 ~/.zshrc ,修改 ZSH_THEME="powerlevel10k/powerlevel10k"

  3. 安装插件

    zsh-syntax-highlighting 语法高亮

    git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

    zsh-autosuggestions 自动补全

    git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions

    下载后编辑 ~/.zshrc ,添加:

    1
    2
    3
    4
    5
    plugins=(
    git
    zsh-syntax-highlighting
    zsh-autosuggestions
    )

    执行 source ~/.zshrc 后重启 zsh

    zsh-incremental 提示补全

    1. brew install wget
    2. wget -O incr-0.2.zsh https://mimosa-pudica.net/src/incr-0.2.zsh
    3. cp incr-0.2.zsh ~/.oh-my-zsh/plugins/
    4. 添加 source ~/.oh-my-zsh/plugins/incr*.zsh~/.zshrc

fish

fish 开箱即用,不需要怎么配置

http://fishshell.com

brew install fish

Ctrl+F或者 右方向键, 即可自动补全

设置fish为默认shell:

sudo vim /etc/shells,添加 /usr/local/bin/fish

1
2
3
4
5
6
7
8
9
10
11
# List of acceptable shells for chpass(1).
# Ftpd will not allow users to connect who are not using
# one of these shells.

/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh
/usr/local/bin/fish

chsh -s /usr/local/bin/fish 更换shell

fish插件管理:oh-my-fish

fish_config 网页可视化配置

fish 的PATH路径为:~/.config/fish/config.fish

格式:set PATH /usr/local/mysql/bin $PATH

常用快捷键

快捷键 描述
Command+T 新建标签页
Command+W 关闭标签页
Command+N 新建终端窗口
Command+Q 关闭终端窗口
Command+N 切换到第N个标签页(N=1,2,…)
Command+- 缩写窗口(包括窗口内的字体)
Command+0 普通大小(阿拉伯数字0)
Ctrl+D 在空白处执行将结束当前会话
Ctrl+P 显示上一条历史命令
Ctrl+N 显示下一条历史命令
Ctrl+R 反向搜索历史命令
Ctrl+J 回车(同enter键功能)
Ctrl+M 回车(同enter键功能)
Ctrl+O 执行并保留命令
Ctrl+A 光标移动到行的开头
Ctrl+E 光标移动到行的结尾
Ctrl+B 光标向后移动一个位置(backward)
Ctrl+F 光标向前移动一个位置(forward),补全提示
Ctrl+Left-Arrow 光标移动到上一个单词的词首
Ctrl+Right-Arrow 光标移动到下一个单词的词尾
Ctrl+T 将光标字符和前一个字符进行位置交换
Ctrl+U 剪切从行的开头到光标前一个所有字符
Ctrl+K 剪切从光标位置到行末的所有字符
Ctrl+Y 粘贴剪切的内容(撤销删除)
Ctrl+H 删除光标前一个字符
Ctrl+* 删除光标前一个字符
Ctrl+D 删除光标后一个字符
Ctrl+C 终止命令
Ctrl+W 删除光标前一个单词
Ctrl+L 清除当前屏幕内容
Ctrl+S 暂停屏幕输出
Ctrl+Q 继续屏幕输出

小提示
Linux 下同理,把 Command 换成 Ctrl+ShiftCtrl
在终端窗口命令提示符下,按一次 Tab 键补全;未匹配到命令时,连续按两次 Tab 键 或 按 Ctrl+i ,将显示所有的命令及工具名称