22

I want to make a .desktop file like described here.

[Desktop Entry]
Name=Sublime Text 2
GenericName=Sublime Text 2
Comment=Edit text files
Exec=/home/user/opt/sublime/Sublime Text 2/sublime_text %U

However, running that from Nautilus's context menu using Open with this gives me

Could not find '/home/user/opt/sublime/Sublime'

So I tried

Exec="/home/user/opt/sublime/Sublime Text 2/sublime_text" %U

and got

Text ended before matching quote was found for ". (The text was '"/home/user/opt/sublime/Sublime')

What is the correct way to escape spaces in the Exec line of .desktop files?

nh2
  • 1,831
  • 2
  • 18
  • 19

11 Answers11

9

According to the desktop entry specification ASCII space MUST be represented by the \s escape sequence (for string and localestring values).

The Exec= and TryExec= fields are the only fields where the path needs to be escaped. Do NOT escape paths inside Path= or Icon= fields (since those fields accept normal spaces without any issues).

Furthermore, the path to the Exec/TryExec executable MUST ALSO be wrapped in double quotes (""), since the escape-sequences only work inside double quotes.

Any application arguments after the binary's path MUST be separated by normal spaces.

Here is a working example:

Exec="/home/ubuntu/.local/share/My\sSpaces\sApplication/app" arg1 arg2 arg3
Mitch McMabers
  • 190
  • 1
  • 6
moon.musick
  • 1,958
8

Precede each space by a backslash:

[Desktop Entry]
Name=Sublime Text 2
GenericName=Sublime Text 2
Comment=Edit text files
Exec=/home/user/opt/sublime/Sublime\ Text\ 2/sublime_text %U
January
  • 37,208
4

Have you tried using ' ' quotes instead of " " quotes? I have a customised .desktop file with spaces in the Exec line, and mine works with ' ' quotes.

Henry Gibson
  • 839
  • 9
  • 16
3

I was having exactly the same problem! After trying various escaping/quoting patterns, I decided the simplest solution was to have a symbolic link to sublime_text on my $PATH (or you could use a command-line alias).

[Desktop Entry]
Version=1.0
Type=Application
Terminal=false
Icon[C]=/usr/share/Sublime Text 2/Icon/256x256/sublime_text.png
Name[C]=Sublime Text 2
Exec=Sublime-Text-2 %U
Comment[C]=Text Editor
Name=Sublime Text 2
Comment=Text Editor
Icon=/usr/share/Sublime Text 2/Icon/256x256/sublime_text.png
X-Desktop-File-Install-Version=0.21
c24w
  • 169
3

I've got the same problem when following installation instructions from (http://monkeyhacks.com/post/how-to-install-sublime-text-2-on-ubuntu-14-04) site.. So my solution was:

  1. Remove/delete the soft link called "sublime" from /usr/bin

  2. Remove all whitespaces of "Sublime Text 2" folder by renaming it to "SublimeText2"

  3. Re Create a soft link in /usr/bin/:

    sudo ln -s /opt/SublimeText2/sublime_text /usr/bin/sublime
    
  4. Make the .desktop file including:

    Exec=/opt/SublimeText2/sublime_text %U
    Icon=/opt/SublimeText2/Icon/48x48/sublime_text.png
    
1

I followed the answer by moon.musick which pointed to the freedesktop specification to use \s in place of space, but I had some issues with it, since the "Path" and "Exec" entries seem to be interpreted differently.

This is a working example using both "Exec" and "Path":

Exec="/home/janek/.steam/steam/steamapps/common/Torchlight\sII/Torchlight2.bin.x86_64"
Path=/home/janek/.steam/steam/steamapps/common/Torchlight\sII

Note that the "Exec" line is quoted, otherwise it will interpret the part after the space as argument. However, the "Path" is not quoted, because it seems that it is quoted by the program reading the file. I tried it from the start menu and using gtk-launch in the command-line, both ended up working in this format but did not work when the Path was quoted too:

❯ gtk-launch "Torchlight II"
gtk-launch: error launching application: Failed to change to directory “"/home/janek/.steam/steam/steampps/common/Torchlight II"” (No such file or directory)
xeruf
  • 441
  • 1
  • 5
  • 11
0

.desktop files do not handle quotes, spaces, and escaped characters the same way that shells do. Probably, the best option would be to call a shell in your exec line to get the expected behavior, like so:

[Desktop Entry]
Name=Sublime Text 2
GenericName=Sublime Text 2
Comment=Edit text files
Exec=sh -c "/home/user/opt/sublime/Sublime Text 2/sublime_text" %U

For more information, please see man sh. This also avoids having to create and maintain symbolic links, as you will with the other answers.

KDØBPV
  • 24
0

What worked for me is replacing spaces with a single underscore (that's 1 underscore for multiple consecutive spaces) in the [Desktop Entry]'s Icon value.

No need to remove the spaces from the file directory. Also do not enclose the Icon value in quotes.

Icon value in Desktop Entry

Icon Location

0

I don't know why exactly why? (maybe a change in the definition of freedesktop apps), but this is working for me:

#!/usr/bin/env xdg-open
[Desktop Entry]
Version=1.0
Terminal=false
Type=Application
Name=YouTube Music
Exec=/snap/bin/chromium --profile-directory=Default --app-id=cinhimbnkkaeohfgghhklpknlkffjgod
Icon=/home/myuser/snap/chromium/common/chromium/Default/Web Applications/Manifest Resources/cinhimbnkkaeohfgghhklpknlkffjgod/Icons/256.png
StartupWMClass=crx_cinhimbnkkaeohfgghhklpknlkffjgod

Spaces are being recognized in Ubuntu 23.04.

0

In xfce4, I noticed if I tick "Run in Terminal" in the desktop launcher, despite double quotes or single quotes around the path name in Exec, it'll be stripped by desktop daemon (?) and won't be run by terminal.

But if I put double quotes then single quotes outside the path name (or vice-versa), it's worked correctly! Or other way is untick "Open in Terminal" in menu launcher or set Terminal=false direct in that desktop file.

It's confusing when the way handles Exec line differ to other related path fields.

Maybe it depends on your default shell or desktop environment, but just take a few tries.

Nhan.Q
  • 11
0

I ran into the same problem using GNOME nautilus 3.4.2 in Ubuntu 12.04. I found this alternative workaround in a bug report for the same issue in Moblin UI Framework which I prefer to the one mentioned where you need to add every single folder to the $PATH.

The workaround is simply:
"Create a symbolic link in a path that does not have spaces, and point the EXEC field to that link."

Björn
  • 101