前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >尝试 Windows Terminal + z.lua

尝试 Windows Terminal + z.lua

作者头像
szhshp
发布2022-09-21 10:36:35
1.3K0
发布2022-09-21 10:36:35
举报

代替 Bash + autojump 完成全部的设置大概需要 0.5-1h

主体安装

Powershell

系统自带基本不用操作

Windows Terminal

Windows-Terminal - Microsoft Store

Git for Windows

Git for Windows

Powershell

这一段是设置 Powershell, 因为最终我们是通过 Windows Terminal 启动 Powershell

插件安装

使用 PowerShell, 执行下面的命令分别安装 Posh-Git 和 Oh-My-Posh:

代码语言:javascript
复制
# PSReadline - 实现类似 ZSH 那样的命令补全
Install-Module -Name PSReadLine  -Scope CurrentUser
# posh-git - Git 插件
Install-Module posh-git -Scope CurrentUser
# oh-my-posh - 主要用于 Powershell 美化
Install-Module oh-my-posh -Scope CurrentUser
  1. 命令执行后会有一段时间的卡顿, 安装速度会受到网络环境的影响.
  2. 如果你以前安装过旧版本, 那么可以在每一段命令后面加上 -Force 参数以强制安装新版本

应用插件

打开 powershell 输入:

代码语言:javascript
复制
notepad $PROFILE

这一段脚本在每次新的 powershell 开启的时候都会自动执行

添加这一段东西:

代码语言:javascript
复制
# ========================= Import Modules =======================
Import-Module posh-git
Import-Module oh-my-posh
Import-Module PSReadLine



# ========================= Theme =======================
# 这里选择配色主题, 也可以另外开启一个 Terminal 输入`Get-PoshThemes` 来直接查看所有主题
Set-Theme marcduiker


# ========================= PSReadLine =======================
# 使用历史命令记录来应用自动补全
Set-PSReadLineOption -PredictionSource History

# 每次回溯输入历史,光标定位于输入内容末尾
Set-PSReadLineOption -HistorySearchCursorMovesToEnd

# 设置 Tab 为补全的快捷键
Set-PSReadLineKeyHandler -Key "Tab" -Function MenuComplete

# 设置 Ctrl + Z 为撤销
Set-PSReadLineKeyHandler -Key "Ctrl+z" -Function Undo

# 设置向上键为后向搜索历史记录
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward

# 设置向下键为前向搜索历史纪录
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward

Windows Terminal

下载字体

下载你希望使用的字体, 这里有一个好网站: Nerd Fonts, 我比较喜欢这款: Cascadia Code

设置 font-face 和主题

旧版 Powershell 从 Windows Terminal 下拉菜单中选择 设置 来打开 settings.json 文件中的配置文件设置.

代码语言:javascript
复制
{
    // Make changes here to the powershell.exe profile.
    "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}", 
    "name": "Windows PowerShell", 
    "commandline": "powershell.exe", 
+   "fontFace": "Cascadia Code PL", 
    "hidden": false, 

    // 这里给 windows terminal 设置主题
+   "colorScheme": "One Half Dark"

}, 

这个地方的主题还可以进行自定义或者导入别人已经成功写好的主题

新版 Powershell 点开设置的时候可以通过 GUI 页面进行设置字体和颜色

z.lua

一个代替 autojump 的快速跳转工具

lua 安装

http://luabinaries.sourceforge.net/download.html

下载 64 位 executable 然后解压, 将目录加到 %PATH%

z.lua 下载

https://github.com/skywind3000/z.lua/releases

下载之后解压到自己想要放的文件夹, 记录下对应的路径.

z.lua 在 Powershell 设置

Powershell 输入:

代码语言:javascript
复制
notepad $PROFILE

最后一行输入:

代码语言:javascript
复制
iex ($(lua D:/你的路径/z.lua/z.lua --init powershell) -join "`n") 
使用方法

https://github.com/skywind3000/z.lua/blob/master/README.cn.md

代码语言:javascript
复制
z foo       # 跳转到包含 foo 并且权重(Frecent)最高的路径
z foo bar   # 跳转到同时包含 foo 和 bar 并且权重最高的路径
z -r foo    # 跳转到包含 foo 并且访问次数最高的路径
z -t foo    # 跳转到包含 foo 并且最近访问过的路径
z -l foo    # 不跳转,只是列出所有匹配 foo 的路径
z -c foo    # 跳转到包含 foo 并且是当前路径的子路径的权重最高的路径
z -e foo    # 不跳转,只是打印出匹配 foo 并且权重最高的路径
z -i foo    # 进入交互式选择模式,让你自己挑选去哪里(多个结果的话)
z -I foo    # 进入交互式选择模式,但是使用 fzf 来选择
z -b foo    # 跳转到父目录中名称以 foo 开头的那一级

快速打开 Windows Terminal

实际上在安装的时候 Win 就已经给 Windows Terminal 设置了别名, Win+R 然后输入 wt 就可以打开

Alias

代码语言:javascript
复制
notepad $profile

打开配置文件, 然后在上面添加上 alias:

代码语言:javascript
复制
# 这里用 `push` 代替 `git push` 
function push { git push }

# 可以一次执行多少命令
function gitupdate {
    git add --all;
    git commit -m "Update";
    git push;
}

# 还可以使用参数等扩展用法
# 使用方法: cmt 123 'Commemts'
function cmt($ticketNum, $comments) { 

  $string1="Ticket-"
  $string2=$ticketNum + ""
  $string3=$string1 + $string2 + ":"+ $comments

  # echo $string3
  git commit -m $string3 

  # git commit -m "Ticket-XXX:Comment"
}

# 以及使用 Switch 来操作多段命令
function project($x) {
  switch($x){
    "goto" {
      project
      explorer .
    }
    "code" {
      project
      code .
    }
    "dev"{
      project
      yarn dev
    }
    Default {
      z project
    }
  }
}

z.lua 和 Clink 的替代用法

不需要管理员权限

  1. 安装 Lua on Windows, lua.exe 目录加到 %PATH%
    1. 到 CMD 里面试一下 lua 能不能运行
  2. 安装 Clink
    • Clink 和 cmd 大同小异, 主要加了一些 Bash 命令
  3. 复制 z.luaz.cmd 到 clink 的目录
  4. Clink 的目录加到 %PATH%
  5. Try z in clink window

Troubleshooting

中文乱码

代码语言:javascript
复制
notepad.exe $PROFILE

在脚本末尾加入如下代码即可:

代码语言:javascript
复制
$env:LESSCHARSET='utf-8'

也可以修改环境变量: 直接添加环境变量 LESSCHARSET,变量值 utf-8,重启电脑让变量生效 或者可以只对当前窗口生效, 直接输入:set LESSCHARSET=utf-8

无法加载文件 xxxx.ps1, 因为在此系统上禁止运行脚本. 有关详细信息, 请参阅 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies

管理员权限运行 Powershell:

代码语言:javascript
复制
get-ExecutionPolicy -list

会得到:

代码语言:javascript
复制
        Scope ExecutionPolicy
        ----- ---------------
MachinePolicy       Undefined
   UserPolicy       Undefined
      Process       Undefined
  CurrentUser    Unrestricted
 LocalMachine    Unrestricted

若显示 Restricted , 表示状态是禁止的, 进行如下修改即可:

代码语言:javascript
复制
set-executionpolicy remotesigned
我修改了 executionpolicy 依然无效?
解决方法 1: 设置成 Unrestricted
代码语言:javascript
复制
Set-ExecutionPolicy Unrestricted -Scope CurrentUser
Set-ExecutionPolicy Unrestricted 
# CurrentUser 对应的权限将会覆盖 LocalMachine 的权限, 因此要一起设置
解决方法 2: Unlock file

这个方法仅仅解锁了一个文件安全性高一些

代码语言:javascript
复制
Set-ExecutionPolicy remotesigned -Scope CurrentUser
Set-ExecutionPolicy remotesigned

# 然后跑到你对应的 PS1 文件所在的文件夹
Unblock-File .\Microsoft.PowerShell_profile.ps1
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-01-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 主体安装
    • Powershell
      • Windows Terminal
        • Git for Windows
        • Powershell
          • 插件安装
            • 应用插件
            • Windows Terminal
              • 下载字体
                • 设置 font-face 和主题
                • z.lua
                  • lua 安装
                    • z.lua 下载
                      • z.lua 在 Powershell 设置
                        • 使用方法
                      • 快速打开 Windows Terminal
                      • Alias
                      • z.lua 和 Clink 的替代用法
                      • Troubleshooting
                        • 中文乱码
                          • 无法加载文件 xxxx.ps1, 因为在此系统上禁止运行脚本. 有关详细信息, 请参阅 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies
                            • 我修改了 executionpolicy 依然无效?
                        领券
                        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档