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

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

Open
scottchiefbaker opened this issue Aug 21, 2023 · 7 comments
Open

Comments

@scottchiefbaker
Copy link
Contributor

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.

@tavianator
Copy link
Collaborator

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

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

@scottchiefbaker
Copy link
Contributor Author

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.

@tmccombs
Copy link
Collaborator

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

@scottchiefbaker
Copy link
Contributor Author

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
Copy link
Contributor Author

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

@scottchiefbaker
Copy link
Contributor Author

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

@tmccombs
Copy link
Collaborator

tmccombs commented Oct 18, 2023

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 ...)

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

Successfully merging a pull request may close this issue.

3 participants