0

I'm confronted with an escape challenge:

The process:

Step 1 - the file retrieval: Within bash, find command will be invoked.

Find commands output is 1 single file with a path like this:

./vacation/the united states/"best times of our lives"/persona notes.org

Note:

• Within the path

  • 2 times double quote signs " appear, enclosing the phrase best times of our lives

  • Most sub directories contain 1 or more whitespace characters

• Within the file name

  • also, 1 or more whitespace characters appear

Step 2 - Emacs launch in bash by visiting the found 1 file by refering to the corresponding find command result:

Doing this by only passing the tricky path would work like this $ emacs -nw './vacation/the united states/"best times of our lives"/persona notes.org'

emacs launches in bash, what is displayed on the screen switches from bash content to an Emacs buffer containing the file content of said file.

The final question now is: How to acchieve exactly this with giving the emacs command within bash the find command output mentioned at the beginning?

In general substitution is the way to go:

$ emacs -nw $(find ./vacation/ -mmin -700 -type f -iname *.org)

but as the substitute literally is

./vacation/the united states/"best times of our lives"/persona notes.org

the resulting line

$ emacs -nw ./vacation/the united states/"best times of our lives"/persona notes.org

is in need of escaping the whitespace characters and the both double quotes.

But how?

$ emacs -nw '$(find ./vacation/ -mmin -700 -type f -iname *.org)'

  • Doesn't do the trick: Emacs starts in bash, but sets up a buffer with the name -mmin -700 -type f -iname *.org) Such a file doesn't exist, so the buffer is empty

• Replacing the single quotes with double quotes also doesn't work

  • $ emacs -nw "$(find ./vacation/ -mmin -700 -type f -iname *.org)"
  • trying it outworks I'm baffled! It DOES work
  • Well, you know what? That's exactly why I've taken one step back from hacking on it and took another approach - turning to you with this issue, expaining it to you in search of solving advice
  • so, take all of this as both, specifically as presentation of a certain problem and its solution, and in general as inspiration how to solve a problem ahead of you, which put you in a deadlock - taking a step back, trying different approaches, among others seeking advice of others, explaining the problem to others, questioning oneself, doing the stuff excluded by you in the first place is very helpful, is a way for you to overcome your current limitations.
  • You may have asked yourself: 'Why not simply opening that found file with extension of the respective find invocation by a fitting -exec option?' The answer: Then the process would be find invocation, not the emacs invocation, and as a result you have worse access to emacs. For example aliases set up by you to resume the current emacs session suspended before wouldn't work, because the job listing just contains the find job - containing emacs job - but the alias relies on an emacs job to be stand-alone.
  • 3
    Bash doesn't process any expansions or substitutions in single-quotes. Single quotes are for literal text: echo '$(ls)' will echo $(ls), while echo "$(ls)" executes a subshell process and echos a directory listing. I think this isn't an emacs question? – phils Oct 12 '23 at 13:32
  • 1
    As @phils points out, this is a bash question, best asked on the Unix & Linux SE. – NickD Oct 12 '23 at 15:23
  • 1
    It sounds like you answered your own question - "It DOES work". Not clear what else you are asking for. – Tyler Oct 12 '23 at 16:29
  • My guess is that the OP is looking for an explanation. – NickD Oct 12 '23 at 17:30
  • 1
    As the bash manpage says in the Command Substitution section: "If the substitution appears within double quotes, word splitting and pathname expansion are not performed on the results." – NickD Oct 12 '23 at 18:30
  • 1
    in addition to the linked posts, see also https://mywiki.wooledge.org/WordSplitting – ilkkachu Oct 12 '23 at 19:26

1 Answers1

0

Use find and xargs:

find ... -print0 | \
    xargs -0 --no-run-if-empty -n 1 \
        emacs -nw

If you add (server-start) to your Emacs startup file, ~/.emacs, you can start emacs once, in a different window, and use emacsclient to open the files you want, with much less overhead.

Read man find xargs emacsclient emacs.

waltinator
  • 4,865