Launch in default Web browser
-
If you want to open links with your default Web browser, the default answer is to use clickable links by enabling the option in Settings > Preferences > MISC > Clickable Link Settings. Then reload Notepad++.
Clickable links are activated with a double-click.
There are some hiccups–like markdown links for example: where the trailing parenthesis is included in the link.
I actually prefer to select text and use a keyboard shortcut. It is not immediately obvious how to do that. If you look at a fresh
%appdata%\notepad++\shortcuts.xml
, the clues are in there.Warning What I am about to demonstrate is equivalent to copying some text and pasting it into the Run prompt. That means, if you select some text and press the wrong button, you can kill your computer.
The main learning curve here is:
- Carefully selecting text: no MS-Office-knows-what-I-mean.
- Knowing what the selected text will do if executed as a shell command: such as opening a URL with the default Web browser or formatting your computer.
- You have to fix it if you break it.
- Using multiple keys for a shortcut so the command cannot be activated by accident.
If you don’t want to deal with this then stick to clickable links.
Adding the Command
-
Open
%appdata%\notepad++\shortcuts.xml
-
Add the line
<Command name="Run selected text" Ctrl="no" Alt="no" Shift="no" Key="0">$(CURRENT_WORD)</Command>
under the other <UserDefinedCommands>.
-
Save shortcuts.xml and relaunch Notepad++.
-
Open a new file and type a URL inside.
-
Select the URL.
-
Click Run (menu) > Run selected text.
This will open the URL in the default Web browser. This works because if you send a link to the OS, it will be opened in the default Web browser.
So, if you want to open links with your default Web browser, now you know how to do it.
As I’ve explained, you can use this for other things. For example, selecting
pwsh.exe
and activating the command will open PowerShell Core (if installed).If you want to bind this to a keyboard shortcut:
Run (menu) > Modify Shortcut/Delete Command (bottom) > Run selected text (entry) > Modify (button)
Mind any conflicts that show while setting a keybinding. A given key combination might already be taken. Resolve the conflict by choosing a different shortcut for one of commands or menu items. It is possible to clear a shortcut so only one item uses it.
If you want to open the current file (Web development), use
$(FULL_CURRENT_PATH)
instead of$(CURRENT_WORD)
in the command above. -
Similar to the information presented in this thread:
https://community.notepad-plus-plus.org/topic/19030/open-selected-file-in-default-application
Specifically, starting at this post and proceeding downward:
-
From my point of view, I was wondering how Search on Internet worked versus the Launch in Chrome, Launch in Firefox etc shown in the default shortcuts.xml. I had long copied the examples in shortcuts.xml for browser shortcuts. But, I was then forced to modify shortcuts.xml when I switched my default browser. Those examples always explicitly gave a browser (due to Web development). And I followed that convention. Search on Internet followed the default browser.
I first tried searching in the NPP sources. But I can’t read C++ well enough.
I happened on Launch in Web browser · Issue #472 where Mr. Ho simply says “It’s been done.” I wasn’t sure what that could mean and then found the answer I linked to in my original post.
I typically disable clickable links as a first customization. So, it made sense to me that clickable links was the implied solution.
My muscle memory is set to the shortcuts I’ve developed. So, I kept experimenting with the examples in shortcuts.xml until I realized <command /> was just some escaped wrapper for a plain Run prompt. I had previously made the leap to use Start-Process to invoke URLs from the shell. So, I tried directly invoking a URL from NPP and it worked.
I did not realize there had been a recent discussion. My searches had centered around “default Web browser” thus the GitHub issue. I don’t remember seeing this other discussion.
Now, even my PowerShell function has encoding and a fixed protocol. – Since this literally executes whatever is selected, I threw in annoying warnings.