How to run a HTML file from Notepad++?

7

1

I'd like to run a HTML page so I configured the Execute / F5 command as following:

chrome.exe http://localhost:8080/$(FILE_NAME)

It works when the HTML file is located in the root of my Workspace (because my HTTP Web server is set up to do so). But it doesn't work when the file is in a subfolder.

Is there a predefined variable (i.e. $(RELATIVE_PATH)) that can be used to get the file path relative to the Workspace directory?

Thank you by advance for your help.

Supersharp

Posted 2016-05-03T12:35:21.963

Reputation: 83

3Does Run -> Launch in Chrome not help you? – Burgi – 2016-05-03T12:44:20.987

4

If you are using PHP, you could use http://localhost:8080/redirect.php?file=/$(FULL_CURRENT_PATH) where redirect.php takes off the excessive part. See the article Configuring Notepad++ to run php on localhost.

– harrymc – 2016-05-11T11:56:36.303

1A similar answer would be to use a .bat file to do the string manipulation and call Chrome. Simple to do if you are interested. – harrymc – 2016-05-11T14:52:24.130

@Supersharp Ctrl+Shift+Alt+R – rahuldottech – 2016-05-12T17:46:58.720

Answers

1

As of the current version of Notepad++ 6.9.1, the problem with the files in sub-folders path in a web-server has not been fixed yet. The only variable for files path is $(FULL_CURRENT_PATH).

Check: Configuring Notepad++ to run php on localhost.

Maybe this issue will be fixed in the future releases of Notepad++.

iSR5

Posted 2016-05-03T12:35:21.963

Reputation: 1 633

@Supersharp didn't read the comments section, for the environment variables check here: http://docs.notepad-plus-plus.org/index.php/Defining_User_Commands

– iSR5 – 2016-05-12T08:55:04.303

1

@Supersharp also this is helpful : http://docs.notepad-plus-plus.org/index.php/External_Programs

– iSR5 – 2016-05-12T08:56:45.907

5

Instead of using Chrome directly, you could use a .bat file.

Below is one version of such a .bat file that should be executed in Notepadd++ like this :

"path\to\file.bat" "$(FULL_CURRENT_PATH)"

For Windows, where the document root is in C:\inetpub\wwwroot, the .bat file is :

@echo off
set "param=%~1"
set "url=http://localhost/%param:C:\inetpub\wwwroot\=%"
start "" chrome.exe "%url%"

The general DOS syntax used above for replacing strings in a variable is :

"%variable-name:search-string=replacement-string%"

where in our case replacement-string is empty.

This simple .bat file can easily be improved to accept the string C:\inetpub\wwwroot\ as second parameter, and more.

The batch file may cause a black DOS window to appear momentarily, to disappear immediately once Chrome is launched. If it is still too annoying, see this answer.

harrymc

Posted 2016-05-03T12:35:21.963

Reputation: 306 093

1

Python Solution

With Np++ plugins even miracles can be achieved. I wrote a simple Np++ Python Script that achieves exactly this functionality. This solution only requires Notepad++ and the PythonScript plugin.

  1. Install Python Script from Plugins > Plugin Manager
  2. Plugins > Python Script > New Script
  3. Name it "OpenInBrowser.py" (for example) and paste the script: http://pastebin.com/wS4jThcp
  4. In the script, remember to configure your browserExeFullPath, browserUrl and your webpage's rootDir (under which the toplevel index.html would be located).
  5. Plugins > Python Script > Configuration and add your user script to the Menu (left hand list)
  6. Restart Notepad++, Settings > Shortcut Mapper > Plugin commands and map your script to a key, for instance F9. Now pressing that key will open your active document in the browser. No external solutions required.

pKami

Posted 2016-05-03T12:35:21.963

Reputation: 163

I wasn't able to install the "Python Script" plugin on NPP 6.9.1. – Supersharp – 2016-05-12T09:45:54.457

What exactly was the problem? I had no issue installing it on 6.9.1 with the Plugin Manager - just checked. Alternatively, you can try installing it manually from http://npppythonscript.sourceforge.net/download.shtml

– pKami – 2016-05-12T09:59:16.257

1I failed 4 times during download. I won't follow this solution because I don't want to install yet another interpreter (already hold JVM, CLR, Node, Windows' shell...) but I upvoted because it could be a preffered solution for Pyhton users. – Supersharp – 2016-05-12T14:11:14.047

0

Node.js Solution

Based on @harrymc's first comment, I ported the PHP workaround to my Node server.

1° In Notepad++:

Change the <Command> content in the file shortcuts.xml (you can find it under the folder %appdata%/Notepad++), for example:

chrome http://localhost:8080/npp?path=$(FULL_CURRENT_PATH) 

2° In Node.js:

Add a route in the server script (server.js) to handle HTTP GET request:

//redirection for Notepad++
app.get( '/npp', function ( req, res ) 
{
    res.redirect( req.query.path.substr( __dirname.length + 8 ) )
} )

where:

  • app is my Express component (app = express()),
  • +8 is needed because my web pages are located in a subfolder /public/.

Supersharp

Posted 2016-05-03T12:35:21.963

Reputation: 83