21

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?

10 Answers 10

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
7
  • 5
    That does not work: Text ended just after a '\' character. (The text was '/home/user/opt/sublime/Sublime\')
    – nh2
    Sep 18, 2012 at 21:11
  • Funny, it works for me.
    – January
    Sep 18, 2012 at 21:17
  • Do we use the same versions? GNOME nautilus 3.4.2 on 12.04.
    – nh2
    Sep 19, 2012 at 12:45
  • 1
    Works for me on Ubuntu 16.04, as well as double quotes by the way. However, DO NOT escape spaces nor quote the Path and Icon values, it may cause the application not to run (on my computer, the dash icon blinks but does nothing, not even open the terminal even if Terminal=true). Same issue if you don't escape Exec, by the way, so you can imagine it took me some time to find what to do with each line of the .desktop!
    – hsandt
    Sep 25, 2016 at 17:17
  • 1
    Worth adding a note that the Icon= property actually requires passing spaces as is, without any quotation or backslashes.
    – Hi-Angel
    Sep 18, 2022 at 1:35
8

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

1
  • It is correct answer! Thanks man )))
    – Maxim
    Aug 18, 2023 at 4:06
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.

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
2
  • 4
    This is a sensible workaround, but we still don't know how we are meant to use spaces in .desktop files.
    – nh2
    Jan 23, 2013 at 17:34
  • 2
    Agreed, although to me it just seems buggy in the way Exec is parsed, rather than us implementing incorrectly. It interprets a space as the end of the value, which is odd, since you can use unescaped spaces / unquoted strings for paths in the other properties!
    – c24w
    Jan 24, 2013 at 11:39
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
    
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."

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.

3
  • This solution doesn't pass arguments, e. g. file paths, to the ultimately invoked command. -1 Mar 3, 2018 at 22:50
  • Huh. Apparently it's also not reliable. I tested this solution before posting it, and it worked. But then I modified my test to check if you were right, and now it's NOT working, even after restoring it to how it was working. Weird.
    – KDØBPV
    Mar 3, 2018 at 23:20
  • In my case it works: Exec=sh -c ' "/home/user/opt/sublime/Sublime Text 2/sublime_text" "%U" '
    – user278801
    Jul 22, 2020 at 16:52
0

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)
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.

1
  • Please note that it is only for Icon and not for Exec like OP
    – elig
    Mar 22 at 0:16

You must log in to answer this question.

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