[英]How to open a file in a specific application from FZF
我想使用FZF搜索文件,然后在我選擇的編輯器中打開它們,例如 Sublime、Atom。 我不確定如何為此配置我的 shell,我嘗試了以下方法,但無法使其正常工作。
你能幫我嗎?
謝謝
fe() {
local files
IFS=$'\n' files=($(fzf-tmux --query="$1" --multi --select-1 --exit-0))
[[ -n "$files" ]] && ${EDITOR:-atom} "${files[@]}"
}
根據您的評論,唯一的問題可能來自這部分:
${EDITOR:-atom}
如果具有非空值,則擴展為變量 EDITOR 的內容,如果為空或未設置,則擴展為atom
。 您很可能將該變量初始化為atom
以外的其他內容。 嘗試使用簡單的atom
代替,如下所示:
fe() {
local files
IFS=$'\n' files=($(fzf-tmux --query="$1" --multi --select-1 --exit-0))
[[ -n "$files" ]] && atom "${files[@]}"
}
當然,您也可以保留該功能,但請確保您的環境包含EDITOR=atom
。
我寫了一個函數,我保存在我的 .bashrc 中,你可以用它通過 fzf 選擇任何文件,並將它們傳遞給你想要的任何程序(不僅是崇高的,而且是你添加到函數列表的任何 GUI 程序),它也與 cd、cat、tail、head 等命令行工具配合使用。 您也可以循環瀏覽歷史記錄並找到在 fzf 完成其操作后擴展的命令。 如果您將 fzf 配置為默認查看文件系統上的許多常見位置(或參見此處),則此功能真的很出色。 我每天都使用它很多次,主要是改變目錄(f cd)或打開文件。
在您的情況下,您只需在終端中輸入:
f sublime
並且 fzf 將啟動,在您選擇文件后,sublime 將打開它們。
我把這個函數放在下面,我在這里得到了靈感
#!/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
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 by putting single quotes around each argument, also
# first put an extra single quote next to any pre-existing single quotes in
# the raw 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
}
對於那些尋求在 MacOS 中打開結果的通用方法的人:
定義此別名以通過操作系統默認應用程序打開文件(基於選定的文件類型):
alias f='open "$(fzf)"'
然后鍵入f
命令,找到您的文件ENTER
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.