1

I'd like to be able to open the Windows hosts file in Notepad++ and have it automatically associated with a programming language, such as Perl. Unfortunately, since the hosts file doesn't have an extension, I'm not sure how to do this.

1

If a file doesnt have an extension, it is not able to be set to open with a specific application to my knowledge, however once you have opened the file in notepad++ before, you can open it back up and it will show the document in the "Open recent files" under the file tab. This is typically the easiest way for me to open files without extensions like that.

  • 1
    The question is about how to associate a language to host files IN notepad++, not how to associate them WITH notepad++ – Máté Juhász Jan 31 '18 at 5:16
  • Notepad++ has this handy feature where you can choose the language that the file is in, but once you decide that manually on a file with no extension, then it cannot be saved.. unless you open the file from your recents. In that case, it automatically reassigns the last language you used on the file. So I know that my answer doesn't seem all that spectacular, but it works. – user865814 Jan 31 '18 at 19:33
  • 1
    Actually, this does not work, unfortunately. – vertigoelectric Mar 23 '18 at 19:59
1

I ran into this very issue some years ago, and ended up getting something workable:

  • First I grabbed a Hosts user language (this is different than the one I used, but should work). Add the script there to your userDefineLang.xml file in either your "%appdata%/Roaming/Notepad++" or in "Program Files (x86)/Notepad++". That will let set the language for syntax highlighting hosts files manually.

  • Next get and configure the PythonScript extension for Notepad++ (also available via the Plugin Manager). After it's installed, go to it under "Plugins", and select "Configuration...". At the bottom in the "Initialization:" dropdown, select "ATSTARTUP" so that it will automatically launch its startup script when you open Notepad++.

  • Then I got the Modeline Parser extension. Download it, and follow the instructions in the README. Esentially, copy the contents into your plugins folder (wherever PythonScript is). Then it has you modify "startup.py" in PythonScript's "scripts" directory, and add the following lines to the bottom of it:

    import ModelineParser_dll
    import ModelineFoldmethod        # If you want to use the example option to fold based on indent
    import ModelineLanguageDetectors # If you want to use the example language detectors
    ModelineParser_dll.modeline_start()
    

    This includes and launches the Modeline Parser.

  • Lastly, you need to tell the Modeline Parser what a hosts file looks like and what to do with it. To do that go into PythonScripts' "lib" directory and edit "ModelineLanguageDetectors.py". After the default 'makefile' entry, add something very similar for your hosts file:

    ModelineParser_dll.modeline_add_preparse(lambda settings, name, path, text:
        fnmatch.fnmatch(name, 'hosts*') and settings.__setitem__('language','hosts')
    )
    

    Then at the end of that file, associate your hosts file with the hosts user language we added in the first step. Add an entry for the language to the dictionary in the modeline_add_languages method call's parameters:

    'hosts': 'Hosts',
    

After that, close and restart Notepad++ if it's open. Re-open it, and try opening your hosts file. It should automatically have the syntax highlighting applied. I found something along the lines of these instructions years ago, but can no longer find it via Google. Credit mostly goes to whoever wrote that at the time.

-1
  1. Open the file with Notepad++ by dragging and dropping into the editor
  2. Select the language as Python (Language > P > Python). Formatting happens automatically.
  • 3
    Did you read the question? – Toto Jul 31 '19 at 8:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.