Open editor only for text files #3698
-
How can I use fzf to open an editor only if the selected file is a text file? If it's not a text file, I want it to go back to fzf for another selection. Additionally, once the file is opened in the editor, I want the script to exit when the editor is closed. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
One idea is to use the [[ -f {} && ! $(file --dereference --mime -- {}) =~ binary ]] If the file meets the requirements, perform the desired action, such as Tip The placeholder, for instance ✅ ❌ fzf --bind "enter:transform:[[ -f {} && ! \$(file --dereference --mime -- {}) =~ binary ]] &&
echo 'become:vim \{}' ||
echo 'change-header:I am not a text file'" Footnotes |
Beta Was this translation helpful? Give feedback.
One idea is to use the
transform
1 action to check if the file meets certain requirements. Thecheck below has been used by the maintainer in one of their scripts2 to verify if it's a text
file.
If the file meets the requirements, perform the desired action, such as
become:vim …
3, or donothing.
Tip
The placeholder, for instance
{}
, needs to be escaped in theecho
statement fortransform
actions. Otherwise it will cause issues with files that contain spaces in their names.
✅
echo 'become:vim \{}'
❌
echo 'become:vim {}'