How to change the escape style when dropping paths into Terminal

terminal

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?

Edit

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("\\",""))

Best Answer

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.