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

How to open a file in a specific application from FZF

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

你能帮忙吗?

谢谢!

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

根据您的意见,唯一的问题可能出在这部分:

${EDITOR:-atom}

如果具有 non-null 值,则扩展到变量 EDITOR 的内容,如果它为空或未设置,则扩展到 atom。您可能将该变量初始化为 atom 以外的其他内容。尝试简单地使用 atom,像这样:

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

当然,您也可以保持功能原样,但请确保您的环境包含类似 EDITOR=atom.

的内容

wrote a function that I keep in my .bashrc which you can use to select any files through fzf and have them passed to whatever program you want (so not only sublime, but any GUI program you add to the function's list) and it also works with command line tools like cd, cat, tail, head and so on. Also you can cycle back through your history and find the command as it was expanded after fzf did its thing. If you (or see here)这个功能真是让人眼前一亮。我每天都用它很多次,主要是换目录(f cd)或者打开文件。

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

f sublime

并且 fzf 将在您 select 之后启动,您的文件 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=""

    # 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
}