zoxide/templates/powershell.txt

140 lines
3.1 KiB
Plaintext
Raw Normal View History

2020-10-18 09:22:13 +00:00
{%- let SECTION = "# =============================================================================\n#" -%}
{%- let NOT_CONFIGURED = "# -- not configured --" -%}
{{ SECTION }}
# Utility functions for zoxide.
#
# pwd based on the value of _ZO_RESOLVE_SYMLINKS.
function __zoxide_pwd {
$(Get-Location).Path
}
# cd + custom logic based on the value of _ZO_ECHO.
function __zoxide_cd($dir) {
Set-Location $dir -ea Stop
{%- if echo %}
__zoxide_pwd
{%- endif %}
}
{{ SECTION }}
# Hook configuration for zoxide.
#
# Hook to add new entries to the database.
function __zoxide_hook {
zoxide add $(__zoxide_pwd)
}
# Initialize hook.
{%- match hook %}
{%- when Hook::None %}
{{ NOT_CONFIGURED }}
{%- when Hook::Prompt %}
$PreZoxidePrompt = $function:prompt
function prompt {
$null = __zoxide_hook
& $PreZoxidePrompt
}
{%- when Hook::Pwd %}
if ($PSVersionTable.PSVersion.Major -ge 6) {
$ExecutionContext.InvokeCommand.LocationChangedAction = {
$null = __zoxide_hook
}
} else {
Write-Error "`
zoxide: PWD hooks are not supported below PowerShell 6.
Use '--hook prompt' when initializing zoxide."
}
{%- endmatch %}
{{ SECTION }}
# When using zoxide with --no-aliases, alias these internal functions as
# desired.
#
# Jump to a directory using only keywords.
function __zoxide_z {
if ($args.Length -eq 0) {
__zoxide_cd ~
}
elseif ($args.Length -eq 1 -and $args[0] -eq '-') {
__zoxide_cd -
}
elseif ($args.Length -eq 1 -and ( Test-Path $args[0] -PathType Container) ) {
__zoxide_cd $args[0]
}
2020-10-18 09:22:13 +00:00
else {
$__zoxide_result = zoxide query -- @args
if ($LASTEXITCODE -eq 0) {
__zoxide_cd $__zoxide_result
}
}
}
# Jump to a directory using interactive search.
function __zoxide_zi {
2020-10-18 09:22:13 +00:00
$__zoxide_result = zoxide query -i -- @args
if ($LASTEXITCODE -eq 0) {
__zoxide_cd $__zoxide_result
}
}
# Add a new entry to the database.
function __zoxide_za {
zoxide add @args
}
# Query an entry from the database using only keywords.
function __zoxide_zq {
zoxide query @args
}
# Query an entry from the database using interactive selection.
function __zoxide_zqi {
zoxide query -i @args
}
# Remove an entry from the database using the exact path.
function __zoxide_zr {
zoxide remove @args
}
# Remove an entry from the database using interactive selection.
function __zoxide_zri {
zoxide remove -i @args
2020-10-18 09:22:13 +00:00
}
{{ SECTION }}
# Convenient aliases for zoxide. Disable these using --no-aliases.
#
{%- match cmd %}
{%- when Some with (cmd) %}
Set-Alias {{cmd}} __zoxide_z
Set-Alias {{cmd}}i __zoxide_zi
Set-Alias {{cmd}}a __zoxide_za
Set-Alias {{cmd}}q __zoxide_zq
Set-Alias {{cmd}}qi __zoxide_zqi
Set-Alias {{cmd}}r __zoxide_zr
Set-Alias {{cmd}}ri __zoxide_zri
{%- when None %}
{{ NOT_CONFIGURED }}
{%- endmatch %}
{{ SECTION }}
# To initialize zoxide with PowerShell, add the following line to your
# PowerShell configuration file (the location is stored in $profile):
#
# Invoke-Expression (& { $hook = if ($PSVersionTable.PSVersion.Major -ge 6) { 'pwd' } else { 'prompt' } (zoxide init powershell --hook $hook) -join "`n" })