-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Examples (fish)
Pierre Neidhardt edited this page Dec 23, 2016
·
27 revisions
function fzf-bcd-widget -d 'cd backwards'
pwd | awk -v RS=/ '/\n/ {exit} {p=p $0 "/"; print p}' | tac | eval (__fzfcmd) +m --select-1 --exit-0 $FZF_BCD_OPTS | read -l result
[ "$result" ]; and cd $result
commandline -f repaint
end
function fzf-cdhist-widget -d 'cd to one of the previously visited locations'
# Clear non-existent folders from cdhist.
set -l buf
for i in (seq 1 (count $dirprev))
set -l dir $dirprev[$i]
if test -d $dir
set buf $buf $dir
end
end
set dirprev $buf
string join \n $dirprev | tac | sed 1d | eval (__fzfcmd) +m $FZF_CDHIST_OPTS | read -l result
[ "$result" ]; and cd $result
commandline -f repaint
end
The following is useful to manipulate the commandline from the result of jobs. For instance, calling fzf-select
over pacman -Qlq fzf
will allow you to select files in the fzf
package and print them bck to commandline for further manipulation.
function fzf-select -d 'fzf commandline job and print unescaped selection back to commandline'
set -l cmd (commandline -j)
[ "$cmd" ]; or return
eval $cmd | eval (__fzfcmd) -m --tiebreak=index --select-1 --exit-0 | string join ' ' | read -l result
[ "$result" ]; and commandline -j -- $result
commandline -f repaint
end
The following can replace fish completion menu with fzf. Because of a fish bug this will fail on variables and other elements that need escaping (or not).
function fzf-complete -d 'fzf completion and print selection back to commandline'
set -l complist (complete -C(commandline -c))
set -l result
string join -- \n $complist | sort | eval (__fzfcmd) -m --tiebreak=index --select-1 --exit-0 --header '(commandline)' | cut -f1 | while read -l r; set result $result $r; end
for i in (seq (count $result))
set -l r $result[$i]
## We need to escape the result.
switch (string sub -s 1 -l 1 -- (commandline -t))
case "'"
commandline -t -- (string escape -- (eval "printf '%s' '$r'"))
case '"'
set -l quoted (string escape -- (eval "printf '%s' '$r'"))
set -l len (string length $quoted)
commandline -t -- '"'(string sub -s 2 -l (math $len - 2) (string escape -- (eval "printf '%s' '$r'")))'"'
case '~'
commandline -t -- (string sub -s 2 (string escape -n -- (eval "printf '%s' '$r'")))
case '*'
commandline -t -- (string escape -n -- (eval "printf '%s' '$r'"))
end
[ $i -lt (count $result) ]; and commandline -i ' '
end
commandline -f repaint
end
function fco -d "Fuzzy-find and checkout a branch"
git branch --all | grep -v HEAD | string trim | fzf | xargs git checkout
end
function fcoc -d "Fuzzy-find and checkout a commit"
git log --pretty=oneline --abbrev-commit --reverse | fzf +s | xargs git checkout
end
function fssh -d "Fuzzy-find ssh host and ssh into it"
ag '^host [^*]' ~/.ssh/config | cut -d ' ' -f 2 | fzf | xargs -o ssh
end
function fs -d "Switch tmux session"
tmux list-sessions -F "#{session_name}" | fzf | xargs tmux switch-client -t
end
function fpass -d "Fuzzy-find a Lastpass entry and copy the password"
if not lpass status -q
lpass login $EMAIL
end
if not lpass status -q
exit
end
lpass ls | fzf | string replace -r -a '.+\[id: (\d+)\]' '$1' | xargs lpass show -c --password
end