How to escape spaces in .desktop files Exec line

8,484

Solution 1

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

Solution 2

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

Solution 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
    

Solution 4

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

Solution 5

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.

Share:
8,484

Related videos on Youtube

nh2
Author by

nh2

I run my own software consultancy company. Usually I build high-quality systems in Haskell (which I have used at Google, Tsuru Capital and FP Complete), but have in-depth experience with lots of other technologies as well. I like to make, and use, stuff that works well. If you need professional help, contact me at [email protected].

Updated on September 18, 2022

Comments

  • nh2
    nh2 over 1 year

    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
    nh2 over 11 years
    That does not work: Text ended just after a '\' character. (The text was '/home/user/opt/sublime/Sublime\')
  • January
    January over 11 years
    Funny, it works for me.
  • nh2
    nh2 over 11 years
    Do we use the same versions? GNOME nautilus 3.4.2 on 12.04.
  • nh2
    nh2 over 11 years
    This is a sensible workaround, but we still don't know how we are meant to use spaces in .desktop files.
  • c24w
    c24w over 11 years
    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!
  • hsandt
    hsandt over 7 years
    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!
  • David Foerster
    David Foerster about 6 years
    This solution doesn't pass arguments, e. g. file paths, to the ultimately invoked command. -1
  • KDØBPV
    KDØBPV about 6 years
    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.
  • Adrian Lopez
    Adrian Lopez almost 6 years
    Weird... This works if I run the launcher manually, but it doesn't on autostart. This feature is super bugged.
  • Aaron Franke
    Aaron Franke over 5 years
    Doesn't work on XFCE.
  • user278801
    user278801 almost 4 years
    In my case it works: Exec=sh -c ' "/home/user/opt/sublime/Sublime Text 2/sublime_text" "%U" '