Window 设置终端

安装Terminal-Icons

github地址:https://github.com/devblackops/Terminal-Icons

安装命令

PS> Install-Module -Name Terminal-Icons -Repository PSGallery

加入powershell自定义配置里面,实现启动terminal自动加载

vim $profile

写入下面的内容

Import-Module -Name Terminal-Icons

安装nerd font系列字体之一

下载地址:https://www.nerdfonts.com/font-downloads
我这里安装了:LiterationMono Nerd Font
选择该字体,下载解压,全选右击安装即可。

PSReadLine

如果你之前没有创建过 Powershell Profile,使用下列命令创建:

if (!(Test-Path -Path $PROFILE )) { New-Item -Type File -Path $PROFILE -Force }
notepad $PROFILE

命令 $PROFILE 会显示配置文件的路径。

安装

Install-Module -Name PSReadLine -AllowPrerelease -Scope CurrentUser -Force -SkipPublisherCheck

使用配置文件激活

# PSReadLine
Import-Module PSReadLine

自定义配置

键入命令:

Get-PSReadLineOption  # 显示所有可以配置的选项
Get-PSReadLineKeyHandler  # 显示所有可以配置的快捷键

之后在配置文件中通过 Set-PSReadLineOption 和 Set-PSReadlineKeyHandler 可以进行配置。

安装PSFzf

安装

Install-Module PSFzf  

安装Oh My Posh,配置cmd样式

oh-my-poshcmd中使用,在/vendor/clink文件夹下新增oh-my-posh.lua文件即可

load(io.popen('oh-my-posh --config="D:/Scoop/persist/oh-my-posh3/themes/pure.omp.json" --init --shell cmd'):read("*a"))()

或者

load(io.popen('oh-my-posh --config="D:/Scoop/persist/oh-my-posh3/themes/powerlevel10k_rainbow.omp.json" --init --shell cmd'):read("*a"))()

这是两个比较好看的皮肤

安装StarShip,配置cmd样式

You need to use Clink (opens new window)(v1.2.30+) with Cmd. Add the following to a file starship.lua and place this file in Clink scripts directory:

-- starship.lua

load(io.popen('starship init cmd'):read("*a"))()
    

这里对cmder中的clink版本要升级到1.2.30以上,并且要将starship中的版本升级到1.20版本以上

因为cmder中的clink版本必须1.2.30以上,为了以后每次升级不手动迁移文件,设置硬链接

cmder\current\vendor via 🌙
❯ mklink /J D:\Scoop\apps\cmder\current\vendor\clink D:\Scoop\apps\clink\current
为 D:\Scoop\apps\cmder\current\vendor\clink <<===>> D:\Scoop\apps\clink\current 创建的联接

注意,clink中必须有starship.lua文件,没有要迁移过去。

设置逗号,补全建议命令

clink 官网

"\,":                win-cursor-forward             # , (replaces `forward-word`)

powershell

Set-PSReadLineKeyHandler -Key "," -ScriptBlock {
       param($key, $arg)

       $line = $null
       $cursor = $null
       [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)

       if ($cursor -lt $line.Length) {
           [Microsoft.PowerShell.PSConsoleReadLine]::EndOfLine($key, $arg)
       } else {
           [Microsoft.PowerShell.PSConsoleReadLine]::AcceptSuggestion($key, $arg)
       }
}

clink中默认不开启自动提示

clink set autosuggest.enable true
clink set history.max_lines 99999999

查看clink信息

❯ clink info
version  : 1.3.8.c918a0
session  : 11712
binaries : D:\Scoop\apps\clink\1.3.8
state    : D:\Scoop\apps\cmder\current\config
log      : D:\Scoop\apps\cmder\current\config\clink.log
settings : D:\Scoop\apps\cmder\current\config\clink_settings
history  : D:\Scoop\apps\cmder\current\config\clink_history
scripts  : D:\Scoop\apps\cmder\current\vendor ; D:\Scoop\apps\clink\1.3.8 ; D:\Scoop\apps\cmder\current\config
inputrc  : %clink_inputrc%
             (unset)
         : state directory
             D:\Scoop\apps\cmder\current\config\.inputrc
             D:\Scoop\apps\cmder\current\config\_inputrc
         : %userprofile%
             C:\Users\qhong\.inputrc   (LOAD)
             C:\Users\qhong\_inputrc
         : %localappdata%
             C:\Users\qhong\AppData\Local\.inputrc
             C:\Users\qhong\AppData\Local\_inputrc
         : %appdata%
             C:\Users\qhong\AppData\Roaming\.inputrc
             C:\Users\qhong\AppData\Roaming\_inputrc
         : %home%
             C:\Users\qhong\.inputrc   (exists)
             C:\Users\qhong\_inputrc

clink配置信息:

D:\Git
❯ clink set
autosuggest.async                 True
autosuggest.enable                True
autosuggest.original_case         True
autosuggest.strategy              match_prev_cmd history completion
clink.autostart
clink.colorize_input              True
clink.default_bindings            bash
clink.gui_popups                  False
clink.logo                        full
clink.paste_crlf                  space
clink.path
clink.promptfilter                True
cmd.admin_title_prefix
cmd.auto_answer                   off
cmd.ctrld_exits                   True
cmd.get_errorlevel                True
color.arg                         bold
color.arginfo                     yellow
color.argmatcher
color.cmd                         bold
color.cmdredir                    bold
color.cmdsep                      bold
color.comment_row                 bright white on cyan
color.description                 bright cyan
color.doskey                      bold cyan
color.executable
color.filtered                    bold
color.flag                        default
color.git.star                    bright green
color.hidden
color.horizscroll
color.input
color.interact                    bold
color.message                     default
color.modmark
color.popup
color.popup_desc
color.prompt
color.readonly
color.selected_completion
color.selection
color.suggestion                  bright black
color.unexpected                  default
color.unrecognized
debug.log_terminal                False
doskey.enhanced                   True
exec.aliases                      True
exec.commands                     True
exec.cwd                          True
exec.dirs                         True
exec.enable                       True
exec.files                        False
exec.path                         True
exec.space_prefix                 True
files.hidden                      True
files.system                      False
history.dont_add_to_history_cmds  exit history
history.dupe_mode                 erase_prev
history.expand_mode               not_dquoted
history.ignore_space              False
history.max_lines                 10000
history.save                      True
history.shared                    True
history.sticky_search             False
lua.break_on_error                False
lua.break_on_traceback            False
lua.debug                         False
lua.path
lua.reload_scripts                False
lua.strict                        True
lua.traceback_on_error            False
match.expand_envvars              False
match.fit_columns                 True
match.ignore_accent               True
match.ignore_case                 relaxed
match.max_fitted_matches          0
match.max_rows                    0
match.preview_rows                5
match.sort_dirs                   with
match.substring                   False
match.translate_slashes           system
match.wild                        True
prompt.async                      True
prompt.transient                  off
readline.hide_stderr              False
terminal.adjust_cursor_style      True
terminal.differentiate_keys       False
terminal.east_asian_ambiguous     auto
terminal.emulation                auto
terminal.mouse_input              auto
terminal.mouse_modifier
terminal.raw_esc                  False
terminal.use_altgr_substitute     True

用clink给Windows的cmd.exe插上readline的翅膀

初始化配置

安装完clink之后,默认会修改注册表,然后在启动cmd.exe自动启动clink,会显示以下信息:

link v0.4.9 [git:2fd2c2] Copyright (c) 2012-2016 Martin Ridgers
http://mridgers.github.io/clink

如果没有的话,则需要手动inject一下,假设clink.exe在你的PATH环境变量中找到,执行clink autorun install

然后执行clink autorun show,会有类似一下的信息输出:

native : "C:\Users\name\scoop\apps\clink\current\clink.bat" inject --autorun

执行

D:\Scoop\apps\windows-terminal\current>clink autorun show
Current AutoRun values

  Current user:

    native : <unset>
     wow64 : <unset>

  All users:

    native : <unset>
     wow64 : <unset>
D:\Development\baseline-leads>clink autorun install
Current AutoRun values

  Current user:

    native : "D:\Scoop\apps\clink\current\clink.bat" inject --autorun
     wow64 : "D:\Scoop\apps\clink\current\clink.bat" inject --autorun

  All users:

    native : <unset>
     wow64 : <unset>

Clink successfully installed to run when cmd.exe starts (for current user).

配置

clink set autosuggest.enable true
clink set history.max_lines 99999999

clink设置别名

Windows 和 Clink 的别名

方式一

clink autorun set \"C:\Users\qhong\aliases.cmd\" "&&" \"D:\Scoop\apps\clink\current\clink.bat\"

然后再install

clink autorun install

自动注册此脚本

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Microsoft\Command Processor]
"AutoRun"="%USERPROFILE%\\aliases.cmd"

清除设置

clink autorun set
clink autorun uninstall

记住,结束要重新执行clink autorun install

clink中安装z,跳转目录

方法一:

  1. 复制 z.luaz.cmdclink 的目录,或者使用硬链接方式

    mklink D:\Scoop\apps\clink\current\z.cmd D:\Scoop\apps\z.lua\current\z.cmd
    mklink D:\Scoop\apps\clink\current\z.lua D:\Scoop\apps\z.lua\current\z.lua
    mklink D:\Scoop\apps\clink\current\starship.lua D:\MyYun\配置\starship.lua
    
  2. Clink 的目录加到 %PATH%

方法二:不行

简单的,给z.lua所在目录新增到PATH环境变量中

方法三:不行

安装dodorz下面的z.lua,这个里面shims里面有软连接,main下面的没有

发现方法二,三都不行,必须放在clink文件夹下面,不让的化,z命令没问题,但是不会收集跳转过的文件夹

clink中安装zoxide

下载zoxide

下载zoxide.lua

将下载的zoxide.lua配置到clink的配置路径中即可。

终端执行命令

clink set zoxide.cmd z

z.lua使用

z.lua 提供 dirstack 让你更便捷的访问最近刚刚去过的目录,而不需要输入任何关键字。这个方法叫做 dirstack,它记录着最近你刚刚去过的 10 条最新路径,然后是用 z -z --z -{num} 来操作:

# 显示当前的 dir stack
$ z --    
 0  /home/skywind/work/match/memory-match
 1  /home/skywind/.local/etc
 2  /home/skywind/software/vifm-0.9.1
 3  /home/skywind/work
 4  /home/skywind/work/match

# cd 到栈里 2 号路径
$ z -2
  => cd /home/skywind/software/vifm-0.9.1

# 弹出栈顶 (cd 到上一次的老路径),和 "z -0" 相同
$ z -
  => cd -

这个 dirstack 是根据 z.lua 的路径历史数据计算的出来的,和具体的 shell 或者操作系统无关。你退出再登陆不会丢失这些路径历史,不同的 shell 之间也可以共享同一份记录。

此外,还能通过前面提到的 -I-t 参数组和,使用 fzf 选择最近去过的地方:

alias zh='z -I -t .'

首先定义个新的别名 zh(回到历史路径的意思),我们用 -t 参数告诉 z.lua 按时间戳为权重排序,同时 -I 启用 fzf 进行搜索,最后句号代表任意路径。

当我们在命令行里输入 zh 时,就可用 fzf 进行历史路径操作了

第一列为上次访问距今多少秒,第二列是目录名。你可以试着敲几个字母,用 fzf 的字符串模糊匹配进行定位,或者用光标键的上和下(CTRL+J/K 也可以)来上下移动,最后按回车 cd 过去,或者 ESC 放弃。

help方法

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 开头的那一级

fzf设置

设置相关环境变量

setx LANG "en_US.UTF-8"
setx LC_ALL "en_US.UTF-8"
setx LC_CTYPE "en_US.UTF-8"

setx FZF_DEFAULT_COMMAND "fd --hidden --exclude .git"
setx FZF_DEFAULT_OPTS "--height=60% --layout=reverse --info=inline --border --margin=1 --padding=1"
setx FZF_CTRL_T_COMMAND "fd --hidden --exclude .git"
setx FZF_CTRL_T_OPTS "--preview 'bat --style=numbers --color=always --line-range :500 {}'"

相关命令:

查找历史命令并拷贝到粘贴板

history | fzf | sed -E "s/[[:space:]]*[[:digit:]]+[[:space:]]*//g" | clip 

注意这里用到sed命令,要用sed单独安装的,不能使用unxutils uutils-coreutils 这两个,这两个unxutil里面很多都老版本,不要用,uutils-coreutils是为了兼容各个操作系统,很新,但是跟通用的有时参数也有区别。

mac:

history | fzf | sed -E "s/[[:space:]]*[[:digit:]]+[[:space:]]*//g" | pbcopy

参考:

oh-my-posh 官网

starship 官网

终端-快速目录跳转-z_lua

posted @ 2022-10-25 20:57  hongdada  阅读(193)  评论(0编辑  收藏  举报