When I need to progmatically open in emacs a file that has a space in its name, how can I do that? I've tried these commands from a Ruby script, inside backquotes or popen(…):

emacs "foo bar"


emacs foo\ bar

opens two files each named foo and bar, but I want to open a file named foo bar.

  • For me, typing emacs "foo bar" RETURN, typing some garbage followed by C-x C-s creates a file called foo bar. emacs foo\ bar RETURN also works. My shell is bash. If the shell or emacs is behaving differently for you, then there's something in your environment that is causing it.
    – Kyle Jones
    Mar 11, 2012 at 0:21
  • What do you mean by "programatically open" the file? You're calling emacs in a script? If so, can you show the relevant bits of the script?
    – cjc
    Mar 11, 2012 at 0:38
  • @cjc It is just what I showed above surrounded with backticks, or those string put inside a Ruby script popen(...).
    – sawa
    Mar 11, 2012 at 0:39
  • 1
    In that case, this is more of a Ruby question, and how Ruby deals with escape characters, than strictly a Unix/Linux question. Post it to Stackoverflow?
    – cjc
    Mar 11, 2012 at 3:01
  • 1
    If this is a Ruby question, you need to show the Ruby code you're using to invoke emacs. Mar 11, 2012 at 3:41

1 Answer 1


You're having problems with quoting because you're calling popen with a commandline, which is passed to the shell. Ruby's string parser is either eating the double quotes or eating the backslash. You can either call popen with an array of strings, which will bypass the shell, or you can write emacs foo\\ bar which will escape the backslash that you want Ruby to leave for the shell to see.

You must log in to answer this question.

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