Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
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 '12 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 '12 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 '12 at 0:39
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 '12 at 3:01
If this is a Ruby question, you need to show the Ruby code you're using to invoke emacs. –  Keith Thompson Mar 11 '12 at 3:41
show 2 more comments

1 Answer

up vote 5 down vote accepted

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.

share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.