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?
1 Answer
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 invim
withe
command.
Not sure if it's a bug.
-
Your find command is giving mp4, pdf etc files too in the output. Jan 21, 2020 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.– dedowsdiJan 21, 2020 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. Jan 21, 2020 at 14:01
-
1
-
1An 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 withack -f
. Just a thought. One way or another you're going to have to define what should be opened since vim opens everything.– B LayerJan 21, 2020 at 16:06
file -i