zoxide/templates/fish.txt

142 lines
4.0 KiB
Plaintext
Raw Normal View History

2021-04-09 00:05:42 +05:30
{%- let section = "# =============================================================================\n#" -%}
{%- let not_configured = "# -- not configured --" -%}
2020-10-18 14:52:13 +05:30
2021-04-09 00:05:42 +05:30
{{ section }}
2020-10-18 14:52:13 +05:30
# Utility functions for zoxide.
#
# pwd based on the value of _ZO_RESOLVE_SYMLINKS.
function __zoxide_pwd
2022-04-26 20:10:27 +05:30
{%- if cfg!(windows) %}
command cygpath -w (builtin pwd -P)
{%- else if resolve_symlinks %}
2021-02-17 19:34:47 +05:30
builtin pwd -P
2020-10-18 14:52:13 +05:30
{%- else %}
2021-02-17 19:34:47 +05:30
builtin pwd -L
2020-10-18 14:52:13 +05:30
{%- endif %}
end
# A copy of fish's internal cd function. This makes it possible to use
# `alias cd=z` without causing an infinite loop.
if ! builtin functions --query __zoxide_cd_internal
if builtin functions --query cd
builtin functions --copy cd __zoxide_cd_internal
else
2021-09-19 01:26:43 +08:00
alias __zoxide_cd_internal='builtin cd'
end
end
2020-10-18 14:52:13 +05:30
# cd + custom logic based on the value of _ZO_ECHO.
function __zoxide_cd
2024-02-24 04:00:38 +05:30
if set -q __zoxide_loop
builtin echo "zoxide: infinite loop detected"
builtin echo "Avoid aliasing `cd` to `z` directly, use `zoxide init --cmd=cd fish` instead"
return 1
end
2022-04-26 20:10:27 +05:30
{%- if cfg!(windows) %}
2024-02-24 04:00:38 +05:30
__zoxide_loop=1 __zoxide_cd_internal (cygpath -u $argv)
2022-04-26 20:10:27 +05:30
{%- else %}
2024-02-24 04:00:38 +05:30
__zoxide_loop=1 __zoxide_cd_internal $argv
2022-04-26 20:10:27 +05:30
{%- endif %}
2021-04-09 00:05:42 +05:30
{%- if echo %}
2020-10-18 14:52:13 +05:30
and __zoxide_pwd
2021-04-09 00:05:42 +05:30
{%- endif %}
2020-10-18 14:52:13 +05:30
end
2021-04-09 00:05:42 +05:30
{{ section }}
2020-10-18 14:52:13 +05:30
# Hook configuration for zoxide.
#
2021-09-07 02:41:17 +05:30
{% if hook == InitHook::None -%}
{{ not_configured }}
{%- else -%}
2020-10-18 14:52:13 +05:30
# Initialize hook to add new entries to the database.
2021-09-07 02:41:17 +05:30
{%- if hook == InitHook::Prompt %}
function __zoxide_hook --on-event fish_prompt
{%- else if hook == InitHook::Pwd %}
function __zoxide_hook --on-variable PWD
{%- endif %}
test -z "$fish_private_mode"
and command zoxide add -- (__zoxide_pwd)
2020-10-18 14:52:13 +05:30
end
2021-09-07 02:41:17 +05:30
{%- endif %}
2021-04-09 00:05:42 +05:30
{{ section }}
2022-04-22 13:11:11 +05:30
# When using zoxide with --no-cmd, alias these internal functions as desired.
2020-10-18 14:52:13 +05:30
#
2023-05-06 20:48:19 +05:30
if test -z $__zoxide_z_prefix
set __zoxide_z_prefix 'z!'
end
set __zoxide_z_prefix_regex ^(string escape --style=regex $__zoxide_z_prefix)
2020-10-18 14:52:13 +05:30
# Jump to a directory using only keywords.
function __zoxide_z
set -l argc (count $argv)
2020-10-18 14:52:13 +05:30
if test $argc -eq 0
__zoxide_cd $HOME
2021-04-13 01:57:36 +05:30
else if test "$argv" = -
2020-10-18 14:52:13 +05:30
__zoxide_cd -
2021-09-19 01:26:43 +08:00
else if test $argc -eq 1 -a -d $argv[1]
__zoxide_cd $argv[1]
else if set -l result (string replace --regex -- $__zoxide_z_prefix_regex '' $argv[-1]); and test -n $result
__zoxide_cd $result
2020-10-18 14:52:13 +05:30
else
set -l result (command zoxide query --exclude (__zoxide_pwd) -- $argv)
and __zoxide_cd $result
2020-10-18 14:52:13 +05:30
end
end
2023-01-08 07:51:03 +05:30
# Completions.
2021-09-19 01:26:43 +08:00
function __zoxide_z_complete
set -l tokens (commandline --current-process --tokenize)
set -l curr_tokens (commandline --cut-at-cursor --current-process --tokenize)
2021-09-19 01:26:43 +08:00
2021-10-31 20:16:40 +05:30
if test (count $tokens) -le 2 -a (count $curr_tokens) -eq 1
# If there are < 2 arguments, use `cd` completions.
2023-05-06 20:48:19 +05:30
complete --do-complete "'' "(commandline --cut-at-cursor --current-token) | string match --regex '.*/$'
else if test (count $tokens) -eq (count $curr_tokens); and ! string match --quiet --regex $__zoxide_z_prefix_regex. $tokens[-1]
# If the last argument is empty and the one before doesn't start with
# $__zoxide_z_prefix, use interactive selection.
2021-10-31 20:16:40 +05:30
set -l query $tokens[2..-1]
2023-05-06 20:48:19 +05:30
set -l result (zoxide query --exclude (__zoxide_pwd) --interactive -- $query)
and echo $__zoxide_z_prefix$result
commandline --function repaint
2021-09-19 01:26:43 +08:00
end
end
2023-01-08 07:51:03 +05:30
complete --command __zoxide_z --no-files --arguments '(__zoxide_z_complete)'
2021-09-19 01:26:43 +08:00
2020-10-18 14:52:13 +05:30
# Jump to a directory using interactive search.
function __zoxide_zi
2023-05-06 20:48:19 +05:30
set -l result (command zoxide query --interactive -- $argv)
and __zoxide_cd $result
2020-10-18 14:52:13 +05:30
end
2021-04-09 00:05:42 +05:30
{{ section }}
2022-04-11 03:41:51 +05:30
# Commands for zoxide. Disable these using --no-cmd.
2020-10-18 14:52:13 +05:30
#
{%- match cmd %}
{%- when Some with (cmd) %}
2022-06-26 00:37:44 +05:30
abbr --erase {{cmd}} &>/dev/null
2023-01-08 07:51:03 +05:30
alias {{cmd}}=__zoxide_z
2020-10-18 14:52:13 +05:30
2022-06-26 00:37:44 +05:30
abbr --erase {{cmd}}i &>/dev/null
2023-01-08 07:51:03 +05:30
alias {{cmd}}i=__zoxide_zi
2020-10-18 14:52:13 +05:30
{%- when None %}
2021-04-09 00:05:42 +05:30
{{ not_configured }}
2020-10-18 14:52:13 +05:30
{%- endmatch %}
2021-04-09 00:05:42 +05:30
{{ section }}
2021-04-27 17:37:38 +05:30
# To initialize zoxide, add this to your configuration (usually
# ~/.config/fish/config.fish):
2020-10-18 14:52:13 +05:30
#
2022-07-01 15:03:30 +05:30
# zoxide init fish | source