Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

https://matt-a-bennett.github.io/fzf_launcher/fzf_launcher.html #5

Open
utterances-bot opened this issue Apr 5, 2024 · 4 comments
Open

Comments

@utterances-bot
Copy link

Using FZF to select files for any program or command | Matthew Bennett

I am a data scientist working on time series forecasting (using R and Python 3) at the London Ambulance Service NHS Trust. I earned my PhD in cognitive neuroscience at the University of Glasgow working with fmri data and neural networks. I favour linux machines, and working in the terminal with Vim as my editor of choice.

https://matt-a-bennett.github.io/fzf_launcher/fzf_launcher.html

Copy link

itsgabz commented Apr 5, 2024

Thanks for this great idea, I tried the script, first it would not run and i'd get some weird errors pertaining to carriage returns, figured that i need to change the file mode in Neovim to Unix instead of DOS, and also that i needed to call the function from within the bash script. I called it at the end of the script with f (no arguments), which never ended up catching the arguments on the command line since they were overridden, so i removed the function altogether and just kept the script unwrapped. still some struggles, but great idea, hoping to make it work. PS: i am using bash through cygwin on Windows 10. thanks again.

Copy link
Owner

You should be able to put the function in your ~/.bashrc file (then it would be sourced automatically when you start a new shell) and from there typing f vlc or f vim would start fzf, you select your file, and it opens.

I hope it works for you!

Copy link

Thanks, Being a beginner this gave me some ideas, even though I could not follow the whole code, I tried something of my own.
I am using xdg-mime for setting up the default application but yours is more flexible for sure.

f() {
  local selection=$(fzf)
  if [[ -n "$selection" ]]; then
    xdg-open "$selection" &
    disown &&
    exit
  fi
}

@Akshdeep53
Copy link

above code was not working for programs like vim as they use the terminal. Below code should include that. We can further add similar app types to keep the terminal running. Also I have put this code in .bashrc. I could not understand why you used .bash_history.

  f() {
      local selection=$(fzf)  
      if [[ -n "$selection" ]]; then  
          local mime_type=$(xdg-mime query filetype "$selection")
          local default_app=$(xdg-mime query default "$mime_type")
 
          if [[ "$default_app" == "vim.desktop" ]]; then
              xdg-open "$selection"
          else
              xdg-open "$selection" &
              disown 
              exit
          fi
      fi
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants