fd icon indicating copy to clipboard operation
fd copied to clipboard

Add an option to quote files in output for easier copy/pasting

Open scottchiefbaker opened this issue 1 year ago • 7 comments

I have some files with spaces/quotes in the names and I would like fd to quote the output files appropriately to make copy/pasting/chaining easier. My use case is to search for a couple of files and then copy/paste a subset of the found files to another command. If the file contains special characters it makes copy/pasting more complicated.

If I use vanilla fd nova I get:

nova - permissions.odt
nova-status.ods

The first will require quoting to work with the spaces, and the second will not.

If use fd nova -l I get:

-rw-rw-r--. 1 bakers bakers  13K Apr  5  2011 './nova - permissions.odt'
-rw-rw-r--. 1 bakers bakers 9.8K Apr  6  2006  ./nova-status.ods

Note that the filename is automatically quoted as needed, which is what is 90% of I want. Is there a way to get this functionality without the ls style details? I only need the filename.

scottchiefbaker avatar Aug 21 '23 17:08 scottchiefbaker

Here's a quick way to do that, using bash's printf %q to quote:

$ fd '\.rs$' -X bash -c 'printf "%q\n" "$@"' bash

tavianator avatar Aug 21 '23 17:08 tavianator

TIL about %q in printf. Neat!

FWIW: Using printf on the output is a good hack, but I still feel like fd should have a --quote option or something similar internally.

scottchiefbaker avatar Aug 21 '23 18:08 scottchiefbaker

One issue with that is that different shells have slightly different quoting rules. Which one do we use?

tmccombs avatar Aug 21 '23 20:08 tmccombs

All of the major shells I've used use the same basic ' and \ quoting syntax. It should be possible to target the major shells pretty simply. If there are non-standard ones beyond that we could document the differences.

ls -lsa

Automagically does quoting for me. I wonder how ls does it.

scottchiefbaker avatar Aug 21 '23 21:08 scottchiefbaker

FWIW it looks like this is a coreutils feature, and not specific to any shell. The documentation explains how/when quoting is enabled.

scottchiefbaker avatar Aug 21 '23 21:08 scottchiefbaker

This has been open for almost two months and not even a single comment from the devs?

scottchiefbaker avatar Oct 15 '23 00:10 scottchiefbaker

not even a single comment from the devs?

There have been two. @tavianator and I are both maintainers.

My current inclination is that this is out of scope for fd. I think the -0, --exec, and --exec-batch options work for the most common cases where escaping might be necessary. And as @tavianator mentioned, there is a workaround using printf.

I might be able to be convinced otherwise, but I would need a compelling use case for why it is necessary.

I'd also like to point out that using bash may not be necessary. The version of the printf executable that is part of GNU coreutils also supports the %q substition, so you can just do

$ fd '\.rs$' -X printf '%q\n'

And if you want to store the results in a bash array you can do this:

FS=$'\0' arr=($(fd -0 ...))
# or
readarray -d $'\0' arr <(fd -0 ...)

tmccombs avatar Oct 18 '23 06:10 tmccombs