Adding a shortcut to a language....



  • Hey all!

    Is there an easy way to add a ‘shortcut’ mapping to change to a particular language? I use Notepad++ to past in SQL or PowerShell code periodically and I’ve love to setup CTRL-ALT-S or CTRL-ALT-P to toggle to those languages directly for a quick visual inspection. Is this doable already or do I need to upvote a feature enhancement somewhere?

    Thanks!
    Charles



  • Interesting question.

    The languages don’t show up in Settings > Shortcut Mapper, so the easiest possibility isn’t there.

    The language change doesn’t seem to get recorded in a macro.

    I think the issue is that they don’t have a static menu command ID (ie, aren’t listed in menuCmdID.h), so there isn’t a direct way to call them.

    However, some of the scripting plugins, like PythonScript (install instructions here), do have a way to search the menus for an existing command, even if they don’t have a static command ID.

    PythonScript’s notepad.runMenuCommand("Language", "YAML") will change the language to YAML; any languages at the same level will have the same syntax. If you have Preferences > Languages > ☑ Make language menu compact enabled, then the languages under a letter submenu will require a trick: for example Languages > N > Normal Text would be accessed via notepad.runMenuCommand("N", "Normal Text").

    So, if you install PythonScript, then Plugins > Python Script > New Script, and create these two scripts:
    SetLanguageSQL.py

    from Npp import *
    notepad.runMenuCommand("S", "SQL")
    

    SetLanguagePowerShell.py

    from Npp import *
    notepad.runMenuCommand("P", "PowerShell")
    

    Then Plugins > Python Script > Configuration,

    • click on SetLanguageSQL.py and ADD to the Menu Items list
    • click on SetLanguagePowerShell.py and ADD to the Menu Items list

    Once there, restart Notepad++.

    Now Settings > Shortcut Mapper > Plugin Commands will list those two scripts, and you can associate them with keyboard shortcut of your choice.


Log in to reply