我想用FZF搜索文件,然后在我选择的编辑器中打开它们,例如太棒了,原子。我不知道如何为此配置我的 shell,我已经尝试了以下方法,但无法让它工作。
你能帮我吗?
Thanks!
fe() {
local files
IFS=$'\n' files=($(fzf-tmux --query="$1" --multi --select-1 --exit-0))
[[ -n "$files" ]] && ${EDITOR:-atom} "${files[@]}"
}
I 写一个函数我保存在 .bashrc 中,您可以使用它通过 fzf 选择任何文件,并将它们传递给您想要的任何程序(因此不仅是 sublime,还包括您添加到函数列表中的任何 GUI 程序),它也可以与命令行一起使用像这样的工具cd
, cat
, tail
, head
等等。您还可以循环回历史记录并找到在 fzf 完成其操作后扩展的命令。如果你配置 fzf 默认情况下查看文件系统上的许多常见位置 (or see here)这个功能真的很闪亮。我每天都会用很多次,主要是换目录(f cd
)或打开文件。
在您的情况下,您只需在终端中输入:
f sublime
fzf 将启动,在您选择文件后,sublime 将打开它们。
我把这个功能放在下面,我从中得到了灵感here
#!/bin/bash
# Run command/application and choose paths/files with fzf.
# Always return control of the terminal to user (e.g. when opening GUIs).
# The full command that was used will appear in your history just like any
# other (N.B. to achieve this I write the shell's active history to
# ~/.bash_history)
#
# Usage:
# f cd [OPTION]... (hit enter, choose path)
# f cat [OPTION]... (hit enter, choose files)
# f vim [OPTION]... (hit enter, choose files)
# f vlc [OPTION]... (hit enter, choose files)
f() {
# if no arguments passed, just lauch fzf
if [ $# -eq 0 ]
then
fzf | sort
return 0
fi
# store the program
program="$1"
# remove first argument off the list
shift
# store any option flags
options="$@"
# store the arguments from fzf
arguments=$(fzf --multi)
# if no arguments passed (e.g. if Esc pressed), return to terminal
if [ -z "${arguments}" ]; then
return 1
fi
# sanitise the command:
# put an extra single quote next to any pre-existing single quotes
# put single quotes around each argument
# put them all on one line.
for arg in "${arguments[@]}"; do
arguments=$(echo "$arg" | sed "s/'/''/g;
s/.*/'&'/g;
s/\n//g"
)
done
# if the program is on the GUI list, add a '&'
if [[ "$program" =~ ^(nautilus|zathura|evince|vlc|eog|kolourpaint)$ ]]; then
arguments="$arguments &"
fi
# write the shell's active history to ~/.bash_history.
history -w
# add the command with the sanitised arguments to .bash_history
echo $program $options $arguments >> ~/.bash_history
# reload the ~/.bash_history into the shell's active history
history -r
# execute the last command in history
fc -s -1
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)