Applying Defined Language to all open files

  • When I have multiple files open in notepad++ and I want to apply a defined language to all of them, is there a way to do that? Other than one by one going to the Language menu and clicking the defined language?

    I don’t really want this defined language to be the default. When I use this defined language, I often have 5-10 files open and want to apply it to all of them at once.

  • Not that I know of. Unfortunately, if you don’t want the UDL to be the default language for the file type, then you will have to do it manually for each of the files.

    You could use a scripting plugin, like PythonScript or LuaScript, to automate the procedure that you would take manually; procedure/algorithm described below, but not coded

    • For each open file
      • activate the file’s tab
      • select the Language menu’s item for the MySuperCool UDL, or whatever it’s named

    I know you can do file activation and activate menu items in PythonScript; it may have an easier way to change the language… I’d have to search the PythonScript helpfile to be sure.

    (if you don’t have a solution by the time I have more than a couple minutes spare time, I might look into implementing it, because it sounds an interesting but not too difficult implementation; but I’ve already take more of a break than I should…)

  • Is this the case of not wanting to give files real names with extensions and then having Notepad++ apply the language via the extension automagically?

  • I don’t think I fall into that bucket. I don’t get to control the file names, I’m just working with what I’m given, they’re log files. And I’m not asking notepad++ to do anything on it’s own, I can be the one to do it… just wondering if there was a way to me to do it faster than (a) click tab for an open file, (b) Language -> Click desired language, © repeat for each other desired open file. Wondering if maybe there’s something with the ctrl key I can use to select all open files, before I go to Language -> Click desired language.

  • @Katie-Rowe

    When you say “apply a defined language”, what is the language you’re talking about in your case? Is it C, C++, HTML, XML, etc (one of the prepackaged ones) or something you’ve defined yourself (UDL = User Defined Language)?

  • Assuming it’s a User Defined Language, I found the time to hack together a PythonScript to do it. This example assumes the UDL is named “Markdown” (because that’s a UDL I have), but you just have to change the udl_name in the code.

    #   For each open file
    #       * activate the file's tab
    #       * select the Language menu's item for the MySuperCool UDL, or whatever it's named
    udl_name = 'Markdown'   # change as needed
    files_tups_list = notepad.getFiles()
    curr_file = notepad.getCurrentFilename()
    for tup in files_tups_list:
        file = tup[0]
        keep = notepad.getCurrentLang()
        #console.write("lang:" + str(notepad.getCurrentLang()) + "\t" + str(int(notepad.getCurrentLang())) + "\n")
        #   notepad.setLangType(LANGTYPE.USER)          # couldn't find a way to make this select a particular UDL
        #notepad.menuCommand(MENUCOMMAND.LANG_USER)     # ditto
        notepad.runMenuCommand('Language', udl_name)    # here's one that will
        #console.write("lang:" + str(notepad.getCurrentLang()) + "\t" + str(int(notepad.getCurrentLang())) + "\n")
        #console.write("lang:" + str(notepad.getCurrentLang()) + "\t" + str(int(notepad.getCurrentLang())) + "\n")

    I’ve left in my debug code (commented) so you can enable extra prints to help you see what’s going on.

    If you don’t want to set every file to the UDL, but just some of the open files, you can wrap it in an “if” block.

    Note, the thread has similar content, but changes to a UDL it when you switch to a matching file (so it’s got an example of what the “if” block might look like)

Log in to reply