Skip to content

Instantly share code, notes, and snippets.

@c02y
Last active February 27, 2019 10:13
Show Gist options
  • Save c02y/288dfa5770bacd188194608174376198 to your computer and use it in GitHub Desktop.
Save c02y/288dfa5770bacd188194608174376198 to your computer and use it in GitHub Desktop.
z.lua and fzf for fish
# find
alias find 'find -L' # make find follow symlink dir/file by default
function f -d 'find the files by name, if no argv is passed, use the current dir'
find $argv[1] -name "*$argv[2]*"
end
function fl -d 'find a file using fzf and view it using less'
if fzfp # fzf exists, $status = 0
less (find $argv[1] -name "*$argv[2]*" | fzf)
else
find $argv[1] -name "*$argv[2]*"
end
end
function fv -d 'find a file using fzf and edit it using vim'
if fzfp # fzf exists, $status = 0
vim (find $argv[1] -name "*$argv[2]*" | fzf)
else
find $argv[1] -name "*$argv[2]*"
end
end
function fe -d 'find a file using fzf and edit it using emacs'
if fzfp # fzf exists, $status = 0
emacs (find $argv[1] -name "*$argv[2]*" | fzf)
else
find $argv[1] -name "*$argv[2]*"
end
end
function fex -d 'find a file using fzf and edit it using emx'
if fzfp # fzf exists, $status = 0
emx (find $argv[1] -name "*$argv[2]*" | fzf)
else
find $argv[1] -name "*$argv[2]*"
end
end
function ff -d 'find a folder and cd into it using fzf'
if fzfp # fzf exists, $status = 0
cd (find $argv[1] -type d -name "*$argv[2]*" | fzf)
else
find $argv[1] -name "*$argv[2]*"
end
end
function ffc -d 'find a fold and copy its path'
if fzfp # fzf exists, $status = 0
if not test $DISPLAY
echo (find $argv[1] -type d -name "*$argv[2]*" | fzf)
else
find $argv[1] -type d -name "*$argv[2]*" | fzf | xc
end
else
find $argv[1] -name "*$argv[2]*"
end
end
function fzfp -d 'check if fzf is existed, with any argument, fzf binary file will be upgraded'
if command -sq fzf; and set -q $argv[1] # check if fzf is in $PATH, and no any argv is given, two conditions
return 0
else
# check internet connection
if not wget -q --spider http://www.baidu.com # failed, $status != 0
echo "fzf doesn't exist and error occurs when downloading it!"
return 1
end
set tag_name (curl -s "https://api.github.com/repos/junegunn/fzf-bin/releases/latest" | grep "tag_name" | cut -d : -f 2 | awk -F[\"\"] '{print $2}')
set file_name (echo fzf-$tag_name-linux_amd64.tgz)
set file_link (echo https://github.com/junegunn/fzf-bin/releases/download/$tag_name/$file_name)
wget $file_link -O /tmp/$file_name
if test -f /tmp/$file_name
tar -xvzf /tmp/$file_name -C ~/.local/bin
return 0
else
echo "fzf doesn't exist and error occurs when downloading it!"
return 1
end
end
end
source (lua ~/.config/fish/functions/z.lua --init fish once echo | psub)
#set -gx _ZL_FZF_FLAG '-e'
# z.lua using built-in cd which won't affect the cd stack of fish shell, change it to fish's cd so you can use cd -
set -gx _ZL_CD cd
set -gx _ZL_INT_SORT 1
set -gx _ZL_FZF_HEIGHT 0 # 0 means fullscreen
set -gx FZF_DEFAULT_OPTS '-1 -0' # auto select the only match, auto exit if no match
abbr zb 'z -b' # Bonus: zb .. equals to cd .., zb ... equals to cd ../.. and
# zb .... equals to cd ../../.., and so on. Finally, zb ..20 equals to cd (..)x20.
function zz -d 'z\'s interactive selection mode'
if fzfp # fzf exists, $status = 0
set z_cmd z -I
else
set z_cmd z -i
end
if test (count $argv) = 0
eval $z_cmd .
else
eval $z_cmd $argv
end
end
function zh -d 'z\'s cd into the cd history'
if fzfp # fzf exists, $status = 0
set z_cmd z -I -t
else
set z_cmd z -i -t
end
if test (count $argv) -eq 0
eval $z_cmd .
else
eval $z_cmd $argv
end
end
function zu -d 'update z.lua'
set z_path ~/.config/fish/functions/z.lua
if test -e $z_path # test if file existed
rm -rfv $z_path
end
curl https://raw.githubusercontent.com/skywind3000/z.lua/master/z.lua -o $z_path
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment