Lorin Hochstein
Lorin Hochstein

Reputation: 59202

Sublime Text 2: Recognize Makefile.local as a Makefile

How do I configure Sublime Text 2 so that it recognizes a file named Makefile.local as a Makefile, so I don't have to manually do "Set Syntax: Makefile" when I open this file?

Upvotes: 4

Views: 1314

Answers (2)

elomage
elomage

Reputation: 4474

You can turn on syntax highlighting based on the contents of the file.

For example, I have numerous makefiles named Makefile.msp430 and similar, having the first line as follows:

#-*-Makefile-*- vim:syntax=make

This is typical practice for other editors such as vim.

However, for this to work you need to modify the Makefile.tmLanguage file.

  1. Find the file (for Sublime Text 3 in Ubuntu) at:

    /opt/sublime_text/Packages/Makefile.sublime-package
    

Note, that is really a zip file. Copy it, rename with .zip at the end, and extract the Makefile.tmLanguage file from it.

  1. Edit the new Makefile.tmLanguage by adding the "firstLineMatch" key and string after the "fileTypes" section. In the example below, the last two lines are new (should be added by you). The <string> section holds the regular expression, that will enable syntax highlighting for the files that match the first line. This expression recognizes two patterns: "#-*-Makefile-*-" and "vim:syntax=make", but you can improve it.

    <key>fileTypes</key>
    <array>
        <string>GNUmakefile</string>
        <string>makefile</string>
        <string>Makefile</string>
        <string>OCamlMakefile</string>
        <string>make</string>
    </array>
    
    <key>firstLineMatch</key>
    <string>^#\s*-\*-Makefile-\*-|^#.*\s*vim:syntax=make</string>
    
  2. Place the new Makefile.tmLanguage in the User settings directory:

    ~/.config/sublime-text-3/Packages/User/Makefile.tmLanguage
    

From now on, the files matching the first line rules should turn on the syntax highlighting.

Upvotes: 2

messivanio
messivanio

Reputation: 2311

Add the tag <string>Makefile.local</string> to the <array> section in Makefile.tmLanguage file.

<dict>
    <key>fileTypes</key>
    <array>
        <string>Makefile.local</string>
        <string>GNUmakefile</string>
        <string>makefile</string>
        <string>Makefile</string>
        <string>OCamlMakefile</string>
        <string>make</string>
    </array>
    <key>name</key>
...

To find the file, click on Preferences | Browse Packages... menu.

The file can be used to change more Syntax Definition options.

Upvotes: 5

Related Questions