Search code examples
sublimetext3sublimetext

How to set default file extension of saving file for specific syntax in Sublime Text 3


The C++ standard doesn't rule the file extension, so the most of people use .cpp, but I prefer using .cc, how to tell Sublime Text save to .cc when using C++ syntax as default?


Solution

  • The way to change the default file extension shown by the save-as dialog when saving a file set to a specific syntax is to override that syntax's default .sublime-syntax file. In your case that would be the C++.sublime-syntax file.

    Of course, as MattDMo points out, the easiest thing to do is to manually type the preferred file extension whenever you save a file. But for those that want to make the change, here's how to do it.

    • Open the Command Palette and select View Package File.
    • Type C++ and then select C++/C++.sublime-syntax. ST will open the file.
    • Near the top you should see file_extensions: and a list like this:
    file_extensions:
      - cpp
      - cc
      - cp
      - cxx
      - c++
      ...snip...
    
    • ST uses the 1st item in the file_extensions list as the default file extension for the save-as dialog.
    • To make .cc the default extension instead of .cpp, edit the list order so that - cc is the 1st item and - cpp is 2nd, i.e. swap the top 2 lines around.
    • Now save the file in your ST config Packages directory with this path: ../Packages/C++/C++.sublime-syntax. If you like you can create a C++ directory in Packages and then just use Ctrl+S to save because the path will have been automatically set by ST but the file won't save unless the ../Packages/C++/ directory already exists.
    • Note: The full path on a Linux machine would be like this: /home/user/.config/sublime-text-3/Packages/C++/C++.sublime-syntax
    • The Package Resource Viewer plugin's Open Resource Command Palette command could be used to extract .sublime-syntax files instead of ST's native View Package File command. That plugin will automatically create the appropriate Packages directory when necessary.

    Your altered Packages/C++/C++.sublime-syntax file will now override the default version that is shipped with ST. You can reverse this easily simply by deleting the file. It is safe to delete the directory as well if you are not overriding any other files in the same directory.

    The only problem with this is when you install a new version of ST. If the newly installed version has an updated C++.sublime-syntax file, the local one you've created will continue to override the new one. To get around this delete your altered Packages/C++/C++.sublime-syntax file when you install a new version of ST and repeat the steps above to restore your preferred default file extension once the installation has been done.

    Unfortunately there is no way around this, you can not partially override the C++.sublime-syntax file with just the file_extensions: section. Of course new versions of ST don't come along very frequently and the C++.sublime-syntax file gets updated even less often. So this is not a major issue.