Windows 10 命令行还能这样玩

858 阅读6分钟

Windows 10 命令行还能这样玩

前言

习惯了 Mac 的终端开发,改换 Windows 的时候,不在命令行敲一敲,总觉得少了点什么。默认的 CMD 和 PowerShell 太丑,Git Bash 不好用,Xshell、MobaXterm、Cmder 用不习惯。最后弄来弄去,发现 Windows Terminal Preview + PowerShell 7 + WSL 这个组合贼好用。Terminal 可以开多个标签页,还能拆分窗格,WSL 装个 CentOS 7,再配上 oh-my-zsh 和 Homebrew,这不就和 Mac 上的 iTerm2 一样的手感了?

开搞

1、安装

开启 WSL

WSL 是 Windows Subsystem for Linux 的缩写,即 “适用于 Linux 的 Windows 子系统”。

启动或关闭 Windows 功能

开启方式:在 Windows 10 左下角徽标右键,选择应用和功能,点击右上角程序和功能,点击左边的启动或关闭 Windows 功能,右侧滚动条拉到最下面,勾选上适用于 Linux 的 Windows 子系统,点击确定,完成后重启即可。

下载

前往 Github 下载对应安装包,这里以当前版本为例:

安装

双击 Microsoft.WindowsTerminalPreview_1.2.2022.0_8wekyb3d8bbwe.msixbundle 安装 Windows Terminal Preview,如果不能打开,请开启 Microsoft Store。

双击 PowerShell-7.0.3-win-x64.msi 安装 PowerShell 7,用作默认的 sh 工具。

解压 CentOS7.zip,将文件夹 CentOS7 剪切到 C 盘(因为我的是 SSD 盘,放在其他位置也行),双击 CentOS7.exe 注册安装。

2、配置

Windows Terminal Preview 装好后,可以在开始菜单找到,也可以 Win+R 运行 wt 打开。

同样 PowerShell 也可以在开始菜单找到,或者运行 pwsh 打开。

CentOS7 不会出现在开始菜单,但是可以运行 bash 打开。

配置 Windows Terminal

配置 Windows Terminal

在打开的 Windows Terminal 点击标签页右侧的箭头,再点击设置即可进行自定义配置,详细教程请查看 Windows 终端,这里贴一份我的粗糙配置:

{
    "$schema": "https://aka.ms/terminal-profiles-schema",
    "defaultProfile": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
    "copyOnSelect": true,
    "copyFormatting": true,
    "profiles":
    {
        "defaults": {},
        "list":
        [
            {
                "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
                "name": "PowerShell",
                "commandline": "powershell.exe",
                "hidden": false
            },
            {
                "guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
                "hidden": false,
                "name": "PowerShell7",
                "source": "Windows.Terminal.PowershellCore"
            },
            {
                "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
                "name": "命令提示符",
                "commandline": "cmd.exe",
                "hidden": false
            },
            {
                "guid": "{a8202b0e-781a-5dab-98e2-e9d469a63619}",
                "hidden": false,
                "name": "CentOS7",
                "source": "Windows.Terminal.Wsl",
                "fontFace": "Fira Code Retina",
                "fontSize": 10,
                "startingDirectory": "//wsl$/CentOS7/home/admin"
            },
            {
                "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
                "hidden": true,
                "name": "Azure Cloud Shell",
                "source": "Windows.Terminal.Azure"
            }
        ]
    },
    "schemes": [],
    "theme": "dark",
    "keybindings":
    [
        { "command": {"action": "copy", "singleLine": false }, "keys": "ctrl+c" },
        { "command": "paste", "keys": "ctrl+v" },
        { "command": "find", "keys": "ctrl+shift+f" },
        { "command": { "action": "splitPane", "split": "auto", "splitMode": "duplicate" }, "keys": "alt+shift+d" }
    ],
    "initialCols": 98,
    "initialRows": 26,
    "initialPosition": "500,300",
    "scrollbarState": "hidden"
}

配置 PowerShell

PowerShell 可以安装个 oh-my-posh,但是我在 WSL 安装了 oh-my-zsh,PowerShell 不是主工具,这里就不再单独配置,Windows Terminal 里的风格就很棒了!

配置 CentOS 7

配置 CentOS 7

服务器千千万,CentOS 占一半,之所以选择 CentOS 是因为我比较熟悉这个(害)。

在 Windows Terminal 键入 bash 进入 CentOS 7,这时候默认是无密码的 root 用户。键入 passwd 为 root 增加一个密码。

因为需要安装 Linuxbrew,而 Linuxbrew 不能使用 root 运行,所以需要新建一个可以使用 sudo 的用户,这里我们取名为 admin。

# 添加用户
adduser admin

# 设置密码
passwd admin

# 添加到 wheel 用户组
usermod -aG wheel admin

添加后先不急着切换到 admin 用户,先进行一些源的配置工作。

cd /etc/yum.repos.d/

# 更新 Yum 源为国内的 aliyun
mv CentOS-Base.repo CentOS-Base.repo.backup
curl -o CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' CentOS-Base.repo

# 生成缓存
yum makecache

# 安装开发工具和 wget
yum install -y wget

# 增加 Epel
wget http://mirrors.aliyun.com/repo/epel-7.repo

# 增加 IUS
wget https://mirrors.aliyun.com/ius/ius-7.repo
wget https://mirrors.aliyun.com/ius/ius-archive-7.repo
wget https://mirrors.aliyun.com/ius/ius-testing-7.repo

# 重新生成缓存
yum makecache

# 安装 Git 2.x
yum install -y git224

# 安装 Zsh
yum install -y zsh

这时候前置工作差不多了,切换到 admin 用户,然后进行 oh-my-zshHomebrew 的安装。

oh-my-zsh

# 切换到 admin
su admin

# 安装 oh-my-zsh
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

# 安装 Homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

安装完成后按照自己需求配置下 Zsh,配置文件为 .zshrc,位置是 /home/admin/.zshrc

下面是我的粗糙配置:

# 设置环境变量
export PATH="$HOME/.linuxbrew/Homebrew/sbin:$HOME/.linuxbrew/Homebrew/bin:$PATH:$(yarn global bin):$HOME/.config/yarn/global/node_modules/.bin"
export ZSH="$HOME/.oh-my-zsh"

# 忽略 brew 语言环境设置错误问题
export HOMEBREW_NO_ENV_FILTERING=1

# 设置代理
export http_proxy=http://192.168.1.111:10809
export https_proxy=http://192.168.1.111:10809

# 设置主题
ZSH_THEME="gitster"

# 设置插件
plugins=(brew colored-man-pages colorize command-not-found common-aliases dirhistory dotenv dnf emoji emoji-clock encode64 extract git gitfast git-extras git-flow git-flow-avh git-hubflow git-prompt github gitignore history-substring-search jsontools man node npm npx pip python sudo systemd urltools vi-mode vim-interaction yarn yum zsh-autosuggestions zsh-completions zsh_reload zsh-navigation-tools)

# zsh-completions
autoload -U compinit && compinit

# 载入 oh-my-zsh
source $ZSH/oh-my-zsh.sh

# 设置指令别名
alias vi="vim"
alias code="code-insiders"
alias a="code-insiders"

上面可见我将 Homebrew 加入了环境变量,这样就可以方便使用 brew 的命令了,同时我还装了 Yarn,安装方式请查看 安装 Yarn

Homebrew 安装后运行 brew doctor 进行自检,看看哪里需要进行修改。

oh-my-zsh 的主题和插件有很多,主题请查看 Themes,默认自带的插件请查看 plugins,更多插件可以在 zsh-users 找到,使用插件需要加在配置文件的 plugins=() 里面,激活配置重启一下终端或者键入 source .zshrc 即可。

为了提高 nodejs 依赖包的安装速度,可以设置安装源为淘宝源,npm 和 yarn 配置文件分别是 .npmrc.yarnrc,存放在当前用户根目录,这里是 /home/admin/

.npmrc

registry=https://registry.npm.taobao.org/
disturl=https://npm.taobao.org/dist
chromedriver_cdnurl=https://cdn.npm.taobao.org/dist/chromedriver
phantomjs_cdnurl=https://cdn.npm.taobao.org/dist/phantomjs
sass_binary_site=https://cdn.npm.taobao.org/dist/node-sass
electron_mirror=https://npm.taobao.org/mirrors/electron/
fse_binary_host_mirror=https://npm.taobao.org/mirrors/fsevents
scripts-prepend-node-path=true

.yarnrc

registry "https://registry.npm.taobao.org"
chromedriver_cdnurl "https://cdn.npm.taobao.org/dist/chromedriver"
disturl "https://npm.taobao.org/dist"
electron_mirror "https://npm.taobao.org/mirrors/electron/"
fse_binary_host_mirror "https://npm.taobao.org/mirrors/fsevents"
phantomjs_cdnurl "https://cdn.npm.taobao.org/dist/phantomjs"
sass_binary_site "https://cdn.npm.taobao.org/dist/node-sass"
scripts-prepend-node-path true

3、使用

Windows Terminal 可以拆分窗格,具体命令行参数 可以看这里,头图中的命令是这样的:

wt.exe -d "E:\CODE\Project1" yarn.cmd dev `; sp -H -d "E:\CODE\Project2" yarn.cmd dev `; sp -V -d "E:\CODE\Project3" wsl ./http.sh `; sp -H -d "E:\CODE\Project4" wsl ./http.sh

默认值

个性化配置弄完后就可以使用了,搭配上面 Windows Terminal 配置的 CentOS 7 默认启动目录 startingDirectory,需要更改默认启动用户为 admin。

.\CentOS7.exe help

进入到安装 CentOS 7 的目录,这里以 C:\CentOS7 为例,打开 Windows Terminal,键入以下命令:

# 设置默认用户
.\CentOS7.exe config --default-user admin

# 设置默认终端窗口
.\CentOS7.exe config --default-term wt

这样新开的 CentOS 7 终端进入的用户就是 admin 了,还可以设置 Windows Terminal 默认打开的终端 defaultProfile,例如上面我配置的是 PowerShell 7。

打开方式

如果安装了 Git for windows,默认的 sh 文件打开方式是 git-bash,可能会出现一些执行权限的问题,只要将打开方式改为 PowerShell 7,就可以解决了。

方法是在 sh 文件右键设置打开方式 pwsh,如果没有 pwsh 选项,依次点击 更多应用↓ -> 在这台电脑上查找其他应用,点击地址栏,粘贴 PowerShell 7 的安装目录,例如 C:\Program Files\PowerShell\7,选中 pwsh.exe,打开并应用即可。

命令执行

因为是 WSL,CentOS 7 终端是可以调用 Windows 环境变量里的命令的,如果加上后缀的命令存在,如 nginx.exeyarn.cmd,那么就可以运行 Windows 系统里对应的命令。不加上后缀,按照先后顺序查找执行,不存在就报错误提示。

例如查看 node 版本,node -v 运行的是 CentOS 7 的版本,node.exe -v 运行的则是 Windows 的版本。

可以通过键入 where <命令>which <命令> 查看命令位置和执行的是哪一个,例如查看 yarn:

查看 yarn

WSL 默认会挂载 Windows 的盘符,为了方便进入 Windows 各个盘符,可以设置一个目录软连接,这样就可以不用敲 /mnt/<盘符> 了,直接 /<盘符> 即可:

sudo ln -s /mnt/* /

右键操作

右键操作

有时候不想执行 打开终端 -> 打开目录 -> 复制目录路径 -> cd 进入目录 -> 运行命令 这种操作,那么就需要使用右键菜单一键打开了,下面是注册表代码,保存为 shell.reg,双击注册到右键,然后在打开的目录里右键,点击 CentOS Here 或者 Powershell Here 就能启动终端并定位到当前目录了。

terminal.ico 图标在这里下载,放在当前 Windows 用户目录(%userprofile%)里面。

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\Background\shell]

[HKEY_CLASSES_ROOT\Directory\Background\shell\centos]
"Icon"="%userprofile%\\terminal.ico"
@="CentOS Here"

[HKEY_CLASSES_ROOT\Directory\Background\shell\centos\command]
@="wt -p CentOS7 -d %v"

[HKEY_CLASSES_ROOT\Directory\Background\shell\Powershell_2]
@="Powershell Here"
"Icon"="pwsh.exe"

[HKEY_CLASSES_ROOT\Directory\Background\shell\Powershell_2\command]
@="wt -p PowerShell7 -d %v"

折腾工作大概就是这样了,还有更多的操作,正在摸索。

4、参考阅读