2

I want to make a script with FZF (Fuzzy Finder) and I want to show only those file which can be opened with VIM. So, I don't want to list mp4, jpg, pdf etc files in FZF.
So, is there any way to decide whether a file can be opened in vim or not?

  • 2
    Technically, vim can open any file... it just may not be useful. That said, have a look at the wildignore setting. If you can place reasonable restrictions on the kinds of files to exclude, something might be possible. – D. Ben Knoble Jan 20 at 23:27
  • might need to preprocess files using e.g. file -i – Christian Brabandt Jan 21 at 6:25
0

Customize fzf search command

You can pass a dict with custom source to fzf functions:

nnoremap <c-p> :call fzf#vim#files('', {'source': g:fzf_project_source})<cr>

let g:fzf_project_source = 'find . \( -name ".hg" -o -name ".git" -o -name "build"
            \ -o -name ".vscode" -o -name ".clangd" \) -prune -o -type f -print'

Customize fzf action

It's actually not bad to list media files, you can open it with specific program like this:

let g:fzf_action = {
      \ 'ctrl-o': '!gio open',
      \ }

gio open works for me under ubuntu18.04, you might need to change it.

There is one edge case you need to know:

  • If current buffer is an empty new buffer, fzf always open choosen file in vim with e command.

Not sure if it's a bug.

| improve this answer | |
  • Your find command is giving mp4, pdf etc files too in the output. – Ujjwal Gupta Jan 21 at 13:27
  • @UjjwalGupta That's just an example to show you how to modify search command, you need to construct the search command by yourself. – dedowsdi Jan 21 at 13:56
  • But find command will show the results according to a file name, not by their property. There may be cases when a file has no extension like .txt, .cpp, .py etc but it may be a text file and vice-versa. – Ujjwal Gupta Jan 21 at 14:01
  • find can do way more than that, check man or info of find. – dedowsdi Jan 21 at 14:24
  • An alternative source might be ack. It's a grep alternative that processes target dirs recursively and by default only searches file types that are readable (source code, plain text, etc.). You can have it bypass the search part and just list the files it would process with ack -f. Just a thought. One way or another you're going to have to define what should be opened since vim opens everything. – B Layer Jan 21 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.