powershell+starship 个人配置

前言

年纪大了,开始喜欢简洁的东西了,Oh-My-Posh 还是有点过于繁琐和缓慢了,遂转战 starship。
starship 简洁有快速。
只是分享记录一下个人配置,便于以后复现。
所有配置均在PowerShell7中配置,不是Windows PowerShell

用到的模块

  • PSReadLine
  • CompletionPredictor
  • PSCompletions
  • argc(可选)

StarShip 安装+配置

标准的安装过程官网都有,不再过多赘述
安装完最好重启一下系统,使系统变量能够应用

下一步就是按照官网上的安装指南,为对应的 shell 启用 starship,推荐把 starship 的激活语句放在配置的最下面
如果一切顺利,新开一个 shell 就会发现有些许改变。剩下的就是为 starship 贴点花,稍微改变一下外观,可以自己写配置文件,或者是在预设里选一个应用,个人是选择了Nerd Font Symbols Preset,跟着指南操作就行。不过在这条命令中 Windows 下好像不支持用~表示用户目录,所以要把用户目录写全
例如:starship preset nerd-font-symbols -o ~/.config/starship.toml需要改为starship preset nerd-font-symbols -o C:\Users\xxx\.config\starship.toml
如果不想输入地址,可以cd ~ && starship preset nerd-font-symbols -o $pwd/.config/starship.toml

Terminal-Icons 安装+配置

美化一下终端里的一些图标
Install-Module -Name Terminal-Icons
再在配置文件中 Import

PSCompletions 安装+配置

具体的步骤在github readme中都有详细写明
个人不喜欢 scoop,所以我使用的是Install-Module PSCompletions
配置很简单,只需要在配置文件写入 Import 就行,可以使用psc add ...来扩展补全范围。
不过我之前在使用的时候出过一点小问题,安装是没有问题的,但是一旦 Import 后就会报错,是和文件下载有关的,这个时候只需要把魔法开成全局就行了。

其他各模块安装

Powershell 中执行Install-Module ***即可,如果是更新就加上-Force开关,具体安装过程参见各项目官方文档

个人配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using namespace System.Management.Automation
using namespace System.Management.Automation.Language

# Remove build in Alias
@(
'rm', 'rmdir', 'mv', 'cp', 'cat', 'curl', 'wget',
'ls', 'diff', 'sort', 'tee', 'echo', 'sleep'
) | ForEach-Object {
if (Get-Alias $_ -ErrorAction SilentlyContinue) {
Remove-Alias $_ -Force -ErrorAction SilentlyContinue
}
}

# Alias
Set-Alias nv nvim -Force -Option AllScope
function Back-Previous { cd .. }
Set-Alias bd Back-Previous

# starship
Invoke-Expression (&starship init powershell)

# zoxide
Invoke-Expression (& { (zoxide init powershell | Out-String) })

# auto suggestions
Import-Module PSReadLine
Set-PSReadLineOption -PredictionViewStyle ListView -BellStyle None -EditMode Windows
Set-PSReadLineKeyHandler -Key 'Ctrl+z' -Function Undo
Import-Module CompletionPredictor

# Custom Module
Import-Module Terminal-Icons
Import-Module PSCompletions

function File {
param(
[Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)]
[string]$file
)
process {
Get-Item -Path $file | Format-List *
}
}

# argc
$argc_scripts = @('git','ffmpeg')
$PSCompletions.argc_completions($argc_scripts)

# fd
Register-ArgumentCompleter -Native -CommandName 'fd' -ScriptBlock {
param($wordToComplete, $commandAst, $cursorPosition)

$commandElements = $commandAst.CommandElements
$command = @(
'fd'
for ($i = 1; $i -lt $commandElements.Count; $i++) {
$element = $commandElements[$i]
if ($element -isnot [StringConstantExpressionAst] -or
$element.StringConstantType -ne [StringConstantType]::BareWord -or
$element.Value.StartsWith('-') -or
$element.Value -eq $wordToComplete) {
break
}
$element.Value
}) -join ';'

$completions = @(switch ($command) {
'fd' {
[CompletionResult]::new('--and', '--and', [CompletionResultType]::ParameterName, 'Additional search patterns that need to be matched')
[CompletionResult]::new('-d', '-d', [CompletionResultType]::ParameterName, 'Set maximum search depth (default: none)')
[CompletionResult]::new('--max-depth', '--max-depth', [CompletionResultType]::ParameterName, 'Set maximum search depth (default: none)')
[CompletionResult]::new('--min-depth', '--min-depth', [CompletionResultType]::ParameterName, 'Only show search results starting at the given depth.')
[CompletionResult]::new('--exact-depth', '--exact-depth', [CompletionResultType]::ParameterName, 'Only show search results at the exact given depth')
[CompletionResult]::new('-E', '-E ', [CompletionResultType]::ParameterName, 'Exclude entries that match the given glob pattern')
[CompletionResult]::new('--exclude', '--exclude', [CompletionResultType]::ParameterName, 'Exclude entries that match the given glob pattern')
[CompletionResult]::new('-t', '-t', [CompletionResultType]::ParameterName, 'Filter by type: file (f), directory (d/dir), symlink (l), executable (x), empty (e), socket (s), pipe (p), char-device (c), block-device (b)')
[CompletionResult]::new('--type', '--type', [CompletionResultType]::ParameterName, 'Filter by type: file (f), directory (d/dir), symlink (l), executable (x), empty (e), socket (s), pipe (p), char-device (c), block-device (b)')
[CompletionResult]::new('-e', '-e', [CompletionResultType]::ParameterName, 'Filter by file extension')
[CompletionResult]::new('--extension', '--extension', [CompletionResultType]::ParameterName, 'Filter by file extension')
[CompletionResult]::new('-S', '-S ', [CompletionResultType]::ParameterName, 'Limit results based on the size of files')
[CompletionResult]::new('--size', '--size', [CompletionResultType]::ParameterName, 'Limit results based on the size of files')
[CompletionResult]::new('--changed-within', '--changed-within', [CompletionResultType]::ParameterName, 'Filter by file modification time (newer than)')
[CompletionResult]::new('--changed-before', '--changed-before', [CompletionResultType]::ParameterName, 'Filter by file modification time (older than)')
[CompletionResult]::new('--format', '--format', [CompletionResultType]::ParameterName, 'Print results according to template')
[CompletionResult]::new('-x', '-x', [CompletionResultType]::ParameterName, 'Execute a command for each search result')
[CompletionResult]::new('--exec', '--exec', [CompletionResultType]::ParameterName, 'Execute a command for each search result')
[CompletionResult]::new('-X', '-X ', [CompletionResultType]::ParameterName, 'Execute a command with all search results at once')
[CompletionResult]::new('--exec-batch', '--exec-batch', [CompletionResultType]::ParameterName, 'Execute a command with all search results at once')
[CompletionResult]::new('--batch-size', '--batch-size', [CompletionResultType]::ParameterName, 'Max number of arguments to run as a batch size with -X')
[CompletionResult]::new('--ignore-file', '--ignore-file', [CompletionResultType]::ParameterName, 'Add a custom ignore-file in ''.gitignore'' format')
[CompletionResult]::new('-c', '-c', [CompletionResultType]::ParameterName, 'When to use colors')
[CompletionResult]::new('--color', '--color', [CompletionResultType]::ParameterName, 'When to use colors')
[CompletionResult]::new('--hyperlink', '--hyperlink', [CompletionResultType]::ParameterName, 'Add hyperlinks to output paths')
[CompletionResult]::new('--ignore-contain', '--ignore-contain', [CompletionResultType]::ParameterName, 'Ignore directories containing the named entry')
[CompletionResult]::new('-j', '-j', [CompletionResultType]::ParameterName, 'Set number of threads to use for searching & executing (default: number of available CPU cores)')
[CompletionResult]::new('--threads', '--threads', [CompletionResultType]::ParameterName, 'Set number of threads to use for searching & executing (default: number of available CPU cores)')
[CompletionResult]::new('--max-buffer-time', '--max-buffer-time', [CompletionResultType]::ParameterName, 'Milliseconds to buffer before streaming search results to console')
[CompletionResult]::new('--max-results', '--max-results', [CompletionResultType]::ParameterName, 'Limit the number of search results')
[CompletionResult]::new('-C', '-C ', [CompletionResultType]::ParameterName, 'Change current working directory')
[CompletionResult]::new('--base-directory', '--base-directory', [CompletionResultType]::ParameterName, 'Change current working directory')
[CompletionResult]::new('--path-separator', '--path-separator', [CompletionResultType]::ParameterName, 'Set path separator when printing file paths')
[CompletionResult]::new('--search-path', '--search-path', [CompletionResultType]::ParameterName, 'Provides paths to search as an alternative to the positional <path> argument')
[CompletionResult]::new('--strip-cwd-prefix', '--strip-cwd-prefix', [CompletionResultType]::ParameterName, 'By default, relative paths are prefixed with ''./'' when -x/--exec, -X/--exec-batch, or -0/--print0 are given, to reduce the risk of a path starting with ''-'' being treated as a command line option. Use this flag to change this behavior. If this flag is used without a value, it is equivalent to passing "always"')
[CompletionResult]::new('--gen-completions', '--gen-completions', [CompletionResultType]::ParameterName, 'gen-completions')
[CompletionResult]::new('-H', '-H ', [CompletionResultType]::ParameterName, 'Search hidden files and directories')
[CompletionResult]::new('--hidden', '--hidden', [CompletionResultType]::ParameterName, 'Search hidden files and directories')
[CompletionResult]::new('--no-hidden', '--no-hidden', [CompletionResultType]::ParameterName, 'Overrides --hidden')
[CompletionResult]::new('-I', '-I ', [CompletionResultType]::ParameterName, 'Do not respect .(git|fd)ignore files')
[CompletionResult]::new('--no-ignore', '--no-ignore', [CompletionResultType]::ParameterName, 'Do not respect .(git|fd)ignore files')
[CompletionResult]::new('--ignore', '--ignore', [CompletionResultType]::ParameterName, 'Overrides --no-ignore')
[CompletionResult]::new('--no-ignore-vcs', '--no-ignore-vcs', [CompletionResultType]::ParameterName, 'Do not respect .gitignore files')
[CompletionResult]::new('--ignore-vcs', '--ignore-vcs', [CompletionResultType]::ParameterName, 'Overrides --no-ignore-vcs')
[CompletionResult]::new('--no-require-git', '--no-require-git', [CompletionResultType]::ParameterName, 'Do not require a git repository to respect gitignores. By default, fd will only respect global gitignore rules, .gitignore rules, and local exclude rules if fd detects that you are searching inside a git repository. This flag allows you to relax this restriction such that fd will respect all git related ignore rules regardless of whether you''re searching in a git repository or not')
[CompletionResult]::new('--require-git', '--require-git', [CompletionResultType]::ParameterName, 'Overrides --no-require-git')
[CompletionResult]::new('--no-ignore-parent', '--no-ignore-parent', [CompletionResultType]::ParameterName, 'Do not respect .(git|fd)ignore files in parent directories')
[CompletionResult]::new('--no-global-ignore-file', '--no-global-ignore-file', [CompletionResultType]::ParameterName, 'Do not respect the global ignore file')
[CompletionResult]::new('-u', '-u', [CompletionResultType]::ParameterName, 'Unrestricted search, alias for ''--no-ignore --hidden''')
[CompletionResult]::new('--unrestricted', '--unrestricted', [CompletionResultType]::ParameterName, 'Unrestricted search, alias for ''--no-ignore --hidden''')
[CompletionResult]::new('-s', '-s', [CompletionResultType]::ParameterName, 'Case-sensitive search (default: smart case)')
[CompletionResult]::new('--case-sensitive', '--case-sensitive', [CompletionResultType]::ParameterName, 'Case-sensitive search (default: smart case)')
[CompletionResult]::new('-i', '-i', [CompletionResultType]::ParameterName, 'Case-insensitive search (default: smart case)')
[CompletionResult]::new('--ignore-case', '--ignore-case', [CompletionResultType]::ParameterName, 'Case-insensitive search (default: smart case)')
[CompletionResult]::new('-g', '-g', [CompletionResultType]::ParameterName, 'Glob-based search (default: regular expression)')
[CompletionResult]::new('--glob', '--glob', [CompletionResultType]::ParameterName, 'Glob-based search (default: regular expression)')
[CompletionResult]::new('--regex', '--regex', [CompletionResultType]::ParameterName, 'Regular-expression based search (default)')
[CompletionResult]::new('-F', '-F ', [CompletionResultType]::ParameterName, 'Treat pattern as literal string stead of regex')
[CompletionResult]::new('--fixed-strings', '--fixed-strings', [CompletionResultType]::ParameterName, 'Treat pattern as literal string stead of regex')
[CompletionResult]::new('-a', '-a', [CompletionResultType]::ParameterName, 'Show absolute instead of relative paths')
[CompletionResult]::new('--absolute-path', '--absolute-path', [CompletionResultType]::ParameterName, 'Show absolute instead of relative paths')
[CompletionResult]::new('--relative-path', '--relative-path', [CompletionResultType]::ParameterName, 'Overrides --absolute-path')
[CompletionResult]::new('-l', '-l', [CompletionResultType]::ParameterName, 'Use a long listing format with file metadata')
[CompletionResult]::new('--list-details', '--list-details', [CompletionResultType]::ParameterName, 'Use a long listing format with file metadata')
[CompletionResult]::new('-L', '-L ', [CompletionResultType]::ParameterName, 'Follow symbolic links')
[CompletionResult]::new('--follow', '--follow', [CompletionResultType]::ParameterName, 'Follow symbolic links')
[CompletionResult]::new('--no-follow', '--no-follow', [CompletionResultType]::ParameterName, 'Overrides --follow')
[CompletionResult]::new('-p', '-p', [CompletionResultType]::ParameterName, 'Search full abs. path (default: filename only)')
[CompletionResult]::new('--full-path', '--full-path', [CompletionResultType]::ParameterName, 'Search full abs. path (default: filename only)')
[CompletionResult]::new('-0', '-0', [CompletionResultType]::ParameterName, 'Separate search results by the null character')
[CompletionResult]::new('--print0', '--print0', [CompletionResultType]::ParameterName, 'Separate search results by the null character')
[CompletionResult]::new('--prune', '--prune', [CompletionResultType]::ParameterName, 'Do not traverse into directories that match the search criteria. If you want to exclude specific directories, use the ''--exclude=…'' option')
[CompletionResult]::new('-1', '-1', [CompletionResultType]::ParameterName, 'Limit search to a single result')
[CompletionResult]::new('-q', '-q', [CompletionResultType]::ParameterName, 'Print nothing, exit code 0 if match found, 1 otherwise')
[CompletionResult]::new('--quiet', '--quiet', [CompletionResultType]::ParameterName, 'Print nothing, exit code 0 if match found, 1 otherwise')
[CompletionResult]::new('--show-errors', '--show-errors', [CompletionResultType]::ParameterName, 'Show filesystem errors')
[CompletionResult]::new('--one-file-system', '--one-file-system', [CompletionResultType]::ParameterName, 'By default, fd will traverse the file system tree as far as other options dictate. With this flag, fd ensures that it does not descend into a different file system than the one it started in. Comparable to the -mount or -xdev filters of find(1)')
[CompletionResult]::new('-h', '-h', [CompletionResultType]::ParameterName, 'Print help (see more with ''--help'')')
[CompletionResult]::new('--help', '--help', [CompletionResultType]::ParameterName, 'Print help (see more with ''--help'')')
[CompletionResult]::new('-V', '-V ', [CompletionResultType]::ParameterName, 'Print version')
[CompletionResult]::new('--version', '--version', [CompletionResultType]::ParameterName, 'Print version')
break
}
})

$completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
Sort-Object -Property ListItemText
}