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 vianotepad.runMenuCommand("N", "Normal Text").So, if you install PythonScript, then Plugins > Python Script > New Script, and create these two scripts:
SetLanguageSQL.pyfrom Npp import * notepad.runMenuCommand("S", "SQL")SetLanguagePowerShell.pyfrom Npp import * notepad.runMenuCommand("P", "PowerShell")Then Plugins > Python Script > Configuration,
- click on
SetLanguageSQL.pyand ADD to the Menu Items list - click on
SetLanguagePowerShell.pyand 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.
- click on
-
@PeterJones Thanks!! This really quite the perfect write-up. Top results off Google and 20 min later, I have icons for “Set Language to SQL” and “Set Language to XML.”
The PytonScript was a little wonky with allowing me to set the icon. It seemed committed to .bmp or .ico files rather than .png. Ok – found those alternatives. But each time I tried to load them, it just didn’t work. But hey – that’s more the plug-in…
Your solution worked like a charm! Thank you!
-
@PeterJones Thanks!!
-
D dr ramaanand referenced this topic on
-
This post is deleted! -
I had started to reply to a recent post here, but that post was deleted while I was working on my answer.
But there was part of my response that I think might be useful to future readers of this topic, as it’s not as commonly understood:
When using
notepad.runMenuCommand(...)in the PythonScript plugin, it is using the actual text from the menu names. Thus, it is affected by Settings > Preferences > General > Localization – thus, using the XML example above, in the default English localization, it would benotepad.runMenuCommand("Language", "XML"). But if you were in Pig Latin (for example), it would have to benotepad.runMenuCommand("Anguagelay", "XML"); or if you were in Greek, it would have to benotepad.runMenuCommand("Γλώσσα", "XML")(Aside: there’s actually a quirk, though: if you have switched from, for example, Pig Latin to Greek, then until you restart Notepad++, you can use either the Pig Latin or the Greek menu-name-strings in your
runMenuCommandcalls. Once you restart Notepad++, you can only use the active Localization for those strings.)(I don’t know if Localization was actually the problem in the now-deleted post, but it might have been, and it could easily be a problem for other users as well; This actually came up recently, and since it has bearing on this discussion, I thought I would share that main idea here, too: if you are using Localization other than English, you will have to adapt any
runMenuCommandcalls in your PythonScript scripts to use the actual name of the menu in your GUI, not the English names – whether or not it would have solved the deleted problem, it’s important enough to share.) -
@PeterJones ,
I just use the Context menu to create an entry that I can right click on the page and switch to the language I want using the contextMenu.xml file with the following entries.<Item id="0"/> <Item MenuEntryName="Language" subMenuId="language-userDefinedLanguage" name="User Defined Language" MenuItemName="dBASEPlus_Dark"/> <Item MenuEntryName="Language" subMenuId="language-userDefinedLanguage" name="User Defined Language" MenuItemName="dBASEPlus_Light"/>This seems to me to be the quickest easiest way to do this kind of thing and it’s always available with a right mouse click and selection.