Notepad++ : Custom Syntax Highlighting for .txt files

10,488

Solution 1

I ended up writing it myself:

  1. You need the Python plugin

  2. Add the code below to your startup.py file

  3. Switch your Python Initialization setting from "LAZY" to "ATSTARTUP"


#if found determine the menu command and switch language in NPP
def switch_language_view(args):
    notepad.activateBufferID(args["bufferID"])
    lineone = editor.getLine(0)
    if '##' in lineone:
        lineone = lineone[lineone.rfind('##'):].replace('##', '')
        lineone = "MENUCOMMAND." + lineone.upper()
        try:
            notepad.menuCommand( eval(lineone) )
        except:
            pass

#command to link notification
notepad.callback(switch_language_view, [NOTIFICATION.FILEOPENED])

Solution 2

No, it can't. You can choose it manually or use special file type extensions which you then associate with Notepad++ and tell it to highlight the files as the appropriate language.

For example, use .txtsql files for SQL, .txtcpp files for C++ and so on.

Solution 3

I'd suggest giving them the proper file extensions, then import something like this into your registry:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\*\shell\NotepadPlusPlus]

[HKEY_CLASSES_ROOT\*\shell\NotepadPlusPlus\command]
@="C:\\path\\to\\notepad++.exe \"%1\""

Then you can open your files in NP++ with a quick right-click, and NP++ will be able to auto-detect the right language based on the file extension.

Solution 4

Manual selection is a much simpler way. Store all the files in .txt format (irrespective of java or C or C++). Open the file in Notepad++ and select the corresponding language in the Menu. e.g. Language --> Java.

Solution 5

You could try some npp scripting,

python

lua

and/or hacking macros. you could make the script start conditionally, check your special string and select the language for you.

Share:
10,488
jj.
Author by

jj.

Software Developer, Technology fan. https://www.jjerome.com

Updated on June 24, 2022

Comments

  • jj.
    jj. almost 2 years

    I keep code samples that I find useful as text files on my computer. I store them as txt files as opposed to the language in which they are written, so that they will open in Notepad++ instead of the editor (i.e. I don't want my c++ examples to open in an IDE, just Notepad).

    Is there a way I can have Notepad++ apply appropriate syntax highlighting to the text file by reading a special code in the text file itself?

    For example if I had some sql, the first line of the text file could read like this:

    ##Language=SQL 
    
    ... my sql code properly highlighted as sql ...
    

    Thanks in advance. I realize I could just choose the language after opening the file (i.e. Language > SQL), but it would be much more convenient if it could do it automatically.

  • jj.
    jj. over 14 years
    Thanks, I'll accept this for now. I was hoping there might be a Macro or Plugin, but haven't found anything yet.
  • Pacerier
    Pacerier almost 9 years
    @jj., Btw, why don't you simply change Windows to open .sql .cpp etc directly with notepad++?
  • Pacerier
    Pacerier almost 9 years
    Are you sure this is cross-Windows? Is there a better way?
  • jj.
    jj. almost 9 years
    @Pacerier I still want .sql .cpp files to open in their respective IDE. My code sample files are .txt
  • Pacerier
    Pacerier almost 9 years
    @jj. Hmm, what about naming your sample files as name.sql.sample and name.cpp.sample, and then set Notepad++ to recognize .sql.sample and .cpp.sample files. Does that work?
  • Seth
    Seth almost 9 years
    Regarding being cross-Windows, yes. Windows has been storing file associations like this since at least Windows 95.
  • Hosein Aqajani
    Hosein Aqajani about 5 years
    is there any java approach?