3

I want to set sublime text as my default app to open files like .cpp and .java, however any of my application lists in "Select Application" do not show sublime text.

I tried the method in this link but it didn't work: Change all associations from gedit to another application

Is there any alternative method?

Here is the output of a few related commands, if this helps:

$ ls -al /usr/share/applications/*sublime*
-rw-r--r-- 1 root root 230 Aug  8 23:50 /usr/share/applications/sublime.desktop
-rw-r--r-- 1 root root 532 Sep 23  2016 /usr/share/applications/sublime_text.desktop

$ ls -al /usr/local/share/applications/*sublime*
ls: cannot access '/usr/local/share/applications/*sublime*': No such file or directory

$ ls -al /opt
total 20
drwxr-xr-x  5 root root 4096 Aug  8 23:58 .
drwxr-xr-x 25 root root 4096 Aug 11 08:07 ..
drwxr-xr-x  3 root root 4096 Apr 21 16:02 google
drwxr-xr-x  5 root root 4096 Jul 14  2012 sublime
drwxr-xr-x  4 root root 4096 Aug  8 23:58 sublime_text

$ cat /usr/local/share/applications/sublime*.desktop
cat: '/usr/local/share/applications/sublime*.desktop': No such file or directory

$ cat /usr/share/applications/sublime_text.desktop
[Desktop Entry]
Version=1.0
Type=Application
Name=Sublime Text
GenericName=Text Editor
Comment=Sophisticated text editor for code, markup and prose
Exec=/opt/sublime_text/sublime_text %F
Terminal=false
MimeType=text/plain;
Icon=sublime-text
Categories=TextEditor;Development;
StartupNotify=true
Actions=Window;Document;

[Desktop Action Window]
Name=New Window
Exec=/opt/sublime_text/sublime_text -n
OnlyShowIn=Unity;

[Desktop Action Document]
Name=New File
Exec=/opt/sublime_text/sublime_text --command new_file
OnlyShowIn=Unity;

In response to the comments:

Output I get after running sudo fsck -f /

This is what appears a few seconds later each time the previous command is executed

Link for the screen that appears automatically after

The icon on the left (the one having a box) is Sublime Text 3, for which I want a default path. The right icon (one with the S) is for Sublime Text 2, which doesn't open due to expired build.

Left one is Sublime Text 3

Kewal Shah
  • 1,044
  • 2
  • 13
  • 35
  • I believe that you set that in the /usr/share/applications/defaults.list file. – Terrance Aug 11 '17 at 02:55
  • @Terrance , can you please give me the complete command? – Kewal Shah Aug 11 '17 at 03:44
  • Oh, I also think that if the sublime text is not showing up, that could be because you might be missing /usr/share/applications/sublime_text.desktop file. – Terrance Aug 11 '17 at 04:27
  • @Terrance Yes it shows no such file in the directory. How do I get it? (Reinstallation doesn't work.) – Kewal Shah Aug 12 '17 at 11:51
  • When you right-click the file to associate it with sublime_text does it happen to appear under Other Applications instead of the Default Applications? sublime_text may not be set as a default for anything. – Terrance Aug 12 '17 at 14:37
  • @Terrance, it doesn't appear in the Other Applications either. – Kewal Shah Aug 12 '17 at 20:54
  • Could you by chance get a screenshot of what you are seeing when you right-click the file(s) you are trying to set ST to? – Terrance Aug 12 '17 at 21:24
  • Also, can you add the output of cat /usr/share/applications/sublime_text.desktop? – Terrance Aug 12 '17 at 21:29
  • @Terrance , it doesn't even appear in the list of a .txt file, so it's no use uploading screenshot of the right-click menu. However the command you just asked above did give an output finally! I have uploaded it in the question, see if you can help. – Kewal Shah Aug 12 '17 at 22:23
  • What I was looking for is what you see in the context menu. I have different file managers here and they show different context menus. thunar shows different than nautilus, etc. Your .desktop file looks good, so that is not an issue. The last part might be to change the actual first defaults file I mentioned. – Terrance Aug 12 '17 at 22:45
  • @Terrance , So can you provide me the command to change the actual first defaults file? – Kewal Shah Aug 13 '17 at 03:09
  • 1
    sudo nano /usr/share/applications/defaults.list but before you do any editing, I recommend that you back up your file first. sudo cp /usr/share/applications/defaults.list /usr/share/applications/defaults.list.backup – Terrance Aug 13 '17 at 03:29
  • It worked man! Thank you so much @Terrance. Those .java and .cpp were by default given the path to sublime-text 2, which does not open on my pc due to it's expired build, and may be that is why even ST 2 didn't appear on my other application list. – Kewal Shah Aug 13 '17 at 04:32
  • I am so glad you got it working! :) Glad I could be of some assistance! :) – Terrance Aug 13 '17 at 04:33
  • @Terrance Good job! It looks like your first comment was the correct answer! And ST2 was expired! I still don't know why ST3 didn't show up in the list of apps as per my method. Thanks! – heynnema Aug 13 '17 at 13:32

2 Answers2

4

Even easier... right-click on any .c++ or .java document, select Properties. Go to the Open With tab, select Sublime in the list of available applications, and click Set as Default.

If, for some reason, Sublime isn't appearing in your Unity Dashboard, or in the list of available applications, then you should reinstall Sublime, as it's missing its .desktop file. Recommend that you use Sublime 3 beta, as Sublime 2 has expired.

Update #1:

To check the file system on your Ubuntu partition...

  • boot to the GRUB menu
  • choose Advanced Options
  • choose Recovery mode
  • choose Root access
  • at the # prompt, type sudo fsck -f /
  • repeat the fsck command if there were errors
  • type reboot

Update #2:

As per @Terrance it was a problem with /usr/share/applications/defaults.list, and that Sublime Text 2 was expired and would not launch. See the accepted answer for details.

heynnema
  • 70,711
  • Comments are not for extended discussion; this conversation has been moved to chat. Please make sure any relevant information is moved into the question/answer. – Seth Aug 13 '17 at 00:45
  • @Seth , I understand that, but I need 20 reputation to move to chat. You have the power to give me that if you want, by upvoting this question! (if you do not know it's answer.) – Kewal Shah Aug 13 '17 at 03:18
  • @KewalShah The automated system that created that chat room overrode the rep requirement for that specific room. – Seth Aug 13 '17 at 03:21
  • @Seth , oh sorry, I understood. I'll keep that in mind in future. – Kewal Shah Aug 13 '17 at 03:26
3

For those who do not wish to go through all the lengthy comments and chats related to this post, I've finally got the answer (credit: @Terrance):

  • Enter cat /usr/share/applications/sublime_text.desktop to check for the .desktop file present for ST 3.

  • Enter sudo nano /usr/share/applications/defaults.list to open the default app list.

  • Edit lines like text/x-java and text/x-c++ from whatever the default app is, to ST 3, for eg. text/x-java=sublime_text.desktop, save and exit.

enter image description here

Hope this helps :)

Kewal Shah
  • 1,044
  • 2
  • 13
  • 35