如何从FZF打开特定应用程序中的文件

时间:2017-01-31 17:19:27

标签: macos shell terminal fzf

我想使用Multimaps#invertFrom搜索文件,然后在我选择的编辑器中打开它们,例如崇高,原子。我不知道如何为此配置我的shell,我已经尝试了以下但我无法让它工作。

你能帮忙吗?

谢谢!

fe() {
local files
  IFS=$'\n' files=($(fzf-tmux --query="$1" --multi --select-1 --exit-0))
  [[ -n "$files" ]] && ${EDITOR:-atom} "${files[@]}"
}

2 个答案:

答案 0 :(得分:2)

根据您的评论,可能唯一的问题来自这一部分:

${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之类的内容。

答案 1 :(得分:0)

我编写了一个保存在.bashrc中的函数,您可以使用该函数通过fzf选择任何文件,并将它们传递给所需的任何程序(因此,不仅崇高,而且您添加到该函数列表中的任何GUI程序)它也可以与cd,cat,tail,head等命令行工具一起使用。另外,您还可以循环浏览历史记录,并在fzf完成操作后查找扩展后的命令。

在您的情况下,您只需输入终端:

f sublime

并且fzf将启动,在您选择文件后,升华将打开它们。

我将函数放在下面,并从中得到启发here

# 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 (hit enter, choose path)                                                 
# f cat (hit enter, choose files)                                               
# f vim (hit enter, choose files)                                               
# f vlc (hit enter, choose files)                                               
                                                                                
f() {                                                                           
    # Store the arguments from fzf                                              
    IFS=$'\n' arguments=($(fzf --query="$2" --multi))                           
                                                                                
    # If no arguments passed (e.g. if Esc pressed), return to terminal          
    if [ -z "${arguments}" ]; then                                              
        return 1                                                                
    fi                                                                          
                                                                                
    # We want the command to show up in our bash history, so write the shell's  
    # active history to ~/.bash_history. Then we'll also add the command from   
    # fzf, then we'll load it all back into the shell's active history          
    history -w                                                                  
                                                                                
    # RUN THE COMMANDS ######################################################## 
    # The cd command has no effect when run as background, and doesn't show up  
    # as a job the can be brought to the foreground. So we make sure not to add 
    # a '&' (more programs can be added separated by a '|')                       
    if ! [[ $1 =~ ^(cd)$ ]]; then                                               
        $1 "${arguments[@]}" &                                                  
    else                                                                        
        $1 "${arguments[@]}"                                                    
    fi                                                                          

    # If the program is not on the list of GUIs (e.g. vim, cat, etc.) bring it  
    # to foreground so we can see the output. Also put cd on this list          
    # otherwise there will be errors)                                           
    if ! [[ $1 =~ ^(cd|sublime|zathura|vlc|eog|kolourpaint)$ ]]; then                   
        fg %%                                                                   
    fi                                                                          
                                                                                
    # ADD A REPEATABLE COMMAND TO THE BASH HISTORY ############################ 
    # Store the arguments in a temporary file for sanitising before being       
    # entered into bash history                                                 
    : > /tmp/fzf_tmp                                                            
    for file in ${arguments[@]}; do                                             
        echo $file >> /tmp/fzf_tmp                                              
    done                                                                        
                                                                                
    # Put all input arguments on one line and sanitise the command such that    
    # spaces and parentheses are properly escaped. More sanitisation            
    # substitutions can be added if needed                                      
    sed -i 's/\n//g; s/ /\\ /g; s/(/\\(/; s/)/\\)/' /tmp/fzf_tmp                
                                                                                
    # If the program is on the GUI list add a '&' to the command history        
    if [[ $1 =~ ^(sublime|zathura|vlc|eog|kolourpaint)$ ]]; then                        
        sed -i '${s/$/ \&/}' /tmp/fzf_tmp                                       
    fi                                                                          
                                                                                
    # Grab the sanitised arguments                                              
    arguments=$(cat /tmp/fzf_tmp)                                               
                                                                                
    # Add the command with the sanitised arguments to our .bash_history         
    echo ${1} ${arguments} >> ~/.bash_history                                   
                                                                                
    # Reload the ~/.bash_history into the shell's active history                
    history -r                                                                  
                                                                                
    # Clean up temporary variables                                              
    rm /tmp/fzf_tmp                                                             
}