Search code examples
emacsorg-modeorg-babel

How can I escape filepaths with spaces as org-babel file argument?


I've found org-babel's ability to interact with db's an invaluable tool.

However, notwithstanding the recommendation to not have file names/paths with spaces, sometimes there's no alternative.

On specifying a file argument for org-babel SQLite mode, it works fine with any filename with no spaces (even expanding ~'s and following links), however, I can't seem to find the proper incantation to properly escape paths with spaces.

This works fine:

#+BEGIN_SRC sqlite :db ~/my_sqlite.db :header :line

Whatever combination of quoting (single, double, triple) and escaping (single backslash, double backslash) I can come up with error out somehow. For

#+BEGIN_SRC sqlite :db "/users/me/somedir/a dir with a space/my_sqlite.db" :header :line

Results in:

Error: in prepare, near "dir": syntax error
  dir
  ^--- error here

While

#+BEGIN_SRC sqlite :db "/Users/me/somedir/a\ dir with a space/mysqlite.db" :header :line

Gets me:

Error: in prepare, incomplete input

G__gle-fu has failed me and even searching through github public code repositories for instances of BEGIN_SRC "sqlite :db" didn't turn up any examples.

Perhaps there's another variable to set a default path that the db argument would be relative to that supports spaces?


Solution

  • Escape the spaces in the filename with two backslashes, .../a\\ dir\\ with etc. The first backslash is interpreted by emacs, escaping the next character after it- but you want to pass a single backslash (for each space in the path) on to the subprocess, so two backslashes.