If I drop a file from Finder (or another file source) into the Terminal, the system helpfully escapes characters like spaces: Dropping a file named /Lorem Ipsum.txt yields /Lorem\ Ipsum.txt.

I would like to change this behavior to use quotation marks instead, so that /Lorem Ipsum.txt would yield '/Lorem Ipsum.txt' instead (note the single quotation marks and the missing backslash).

Is there a way to do this using the standard Terminal.app? If not, is it maybe possible with iTerm or something else?


The reason why I want to do this is that I am using xonsh that parses the backslash as a literal character, echo /Lorem\ Ipsum.txt prints /Lorem\ Ipsum.txt.

Most of the time I will just manually delete the backslashes, and sometimes I use the following workaround:

  1. Enter @(r"
  2. Drop the file – this will fill a string literal with the path using backslash-escapes
  3. Enter ".replace("\\",""))
  • A workaround: create a shell script named quote with the content echo "'$@'" to do the conversion. – lhf Sep 11 '20 at 10:31
  • Is there an underlying reason for preferring quotation marks rather than escape characters or is it purely aesthetic? – jaume Sep 11 '20 at 11:54
  • @hlf: Due to my shell the script would receive multiple parameters /Lorem` and Ipsum.txt`, but you are right, I could create a script that does the conversion. – nd. Sep 11 '20 at 14:12
  • 2
    @jaume: The underlying reason is that I am using xonsh that does have different escaping rules (see my edit of the question) – nd. Sep 11 '20 at 14:13
  • @JamesBrickley Would you please write this as an answer to the question so that I can flag it as the solution? – nd. Sep 15 '20 at 6:50

I can confirm that iTerm2 does exactly what you want in regards to enclosing long file names and directories with single quotes instead of escaping the spaces when dragging and dropping. It does this by default.

There are many additional reasons and benefits to switch to iTerm2 from the default Terminal on macOS. Far too many to list here.

Update for iTerm 3.4

iTerm 3.4 changed the escaping behavior to use backslash escaping as well. However, there is an advanced experimental feature Escape file names with single quotes instead of backslashes. that reverts the escaping back. The comment even specifies my very use case: This is intended for users of xonsh, which does not accept backslash escaping.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .