zoxide/templates/fish.txt

127 lines
3.2 KiB
Plaintext

{%- let section = "# =============================================================================\n#" -%}
{%- let not_configured = "# -- not configured --" -%}
{{ section }}
# Utility functions for zoxide.
#
# provide a custom __fish_cd function that is either a copy of the default fish
# cd wrapper function, or an alias for the `builtin cd` to prevent recursively
# calling cd when using `alias cd=z`.
#
# only create the function `__fish_cd` if it doesn't yet exist
if ! functions -q __fish_cd
if functions -q cd
# use the fish wrapper function for __fish_cd if it exists
functions -c cd __fish_cd
else
# use `builtin cd` as fallback if the cd wrapper function does not exist
alias __fish_cd="builtin cd"
end
end
# pwd based on the value of _ZO_RESOLVE_SYMLINKS.
function __zoxide_pwd
{%- if resolve_symlinks %}
builtin pwd -P
{%- else %}
builtin pwd -L
{%- endif %}
end
# cd + custom logic based on the value of _ZO_ECHO.
function __zoxide_cd
{#- We can't use `builtin cd` over here, because fish wraps its builtin cd with
a function that adds extra features (such as `cd -`). Using the builtin
would make those features stop working. Instead use a custom function that is
either a copy of the cd function or an alias for `builtin cd` as fallback #}
__fish_cd $argv
{%- if echo %}
and __zoxide_pwd
{%- endif %}
and builtin commandline -f repaint
end
{{ section }}
# Hook configuration for zoxide.
#
# Initialize hook to add new entries to the database.
if test "$__zoxide_hooked" != 1
set __zoxide_hooked 1
{%- match hook %}
{%- when InitHook::None %}
function __zoxide_hook
{%- when InitHook::Prompt %}
function __zoxide_hook --on-event fish_prompt
{%- when InitHook::Pwd %}
function __zoxide_hook --on-variable PWD
{%- endmatch %}
command zoxide add -- (__zoxide_pwd)
end
end
{{ section }}
# When using zoxide with --no-aliases, alias these internal functions as
# desired.
#
# Jump to a directory using only keywords.
function __zoxide_z
set argc (count $argv)
if test $argc -eq 0
__zoxide_cd $HOME
else if test "$argv" = -
__zoxide_cd -
else if begin
test $argc -eq 1; and test -d $argv[1]
end
__zoxide_cd $argv[1]
else
set -l __zoxide_result (command zoxide query --exclude (__zoxide_pwd) -- $argv)
and __zoxide_cd $__zoxide_result
end
end
# Jump to a directory using interactive search.
function __zoxide_zi
set -l __zoxide_result (command zoxide query -i -- $argv)
and __zoxide_cd $__zoxide_result
end
{{ section }}
# Convenient aliases for zoxide. Disable these using --no-aliases.
#
{%- match cmd %}
{%- when Some with (cmd) %}
# Remove definitions.
function __zoxide_unset
set --erase $argv >/dev/null 2>&1
abbr --erase $argv >/dev/null 2>&1
builtin functions --erase $argv >/dev/null 2>&1
end
__zoxide_unset {{cmd}}
function {{cmd}}
__zoxide_z $argv
end
__zoxide_unset {{cmd}}i
function {{cmd}}i
__zoxide_zi $argv
end
{%- when None %}
{{ not_configured }}
{%- endmatch %}
{{ section }}
# To initialize zoxide, add this to your configuration (usually
# ~/.config/fish/config.fish):
#
# zoxide init fish | source