Notepad++ Run current line in Command Prompt

command linenotepad

There is a similar command that I use to open the currently selected word on command prompt:

cmd /D "%windir%/system32" /K ""$(CURRENT_WORD)""

Is there any command for opening the current line without selecting any text on that line manually?

Best Answer

Neither How to Run an external program from Notepad++ nor the documentation of the plugin NppExec mention a environment variable containing the current line.

Likewise, macros can only action Scintilla messages (list), search and replace recordings and some Notepad++ defined commands (undocumented?), so there's no way to achieve this with macros.

Aside from writing your own plugin, to only way I know how to do this is using an external program. An AutoHotkey script will do what you want.

AutoHotkey script:

^F5::
WinGetActiveTitle, Title
if RegExMatch(Title, "- Notepad\+\+$")
{
    SendPlay {Home}+{End}{F5}
    SendPlay cmd /D "%windir%/system32" /K "$(CURRENT_WORD)"
    SendPlay {Enter}
}
else
    SendPlay ^{F5}
return

What it does:

  • If Notepad++ is the active window, the key combination Ctrl + F5 will select the current line and execute the external command

    cmd /D "%windir%/system32" /K ""$(CURRENT_WORD)""

    $(CURRENT_WORD) now contains the whole line, since it was selected. The only drawback is that you will lose the current cursor position, as the script will leave the current line selected.

  • If Notepad++ is not the active window, the key combination Ctrl + F5 will behave normally.

How to use:

  1. Download and install the latest version.

  2. Save the above script as npp_runline.ahk, using your favorite text editor.

  3. Double-click the file to run the script.

  4. If you wish, copy the script (or a link to it) in the Startup folder.

How it works:

  • ^F5:: specifies the used hotkey (Ctrl + F5).

  • WinGetActiveTitle, Title stores the title of the active window in the variable Title.

  • RegExMatch(Title, "- Notepad\+\+$") returns the position of the leftmost occurrence of the regular expression - Notepad\+\+$ (where \+ is a literal + and $ in the end of the string) in Title, or zero if there is no match.

    • If there is a match, the block follwing if... gets executed:

      • SendPlay {Home}+{End}{F5} simulates pressing Home and Shift + End (selecting the current line without leading whitespace) and then F5 (opening the Run... dialogue).

      • SendPlay cmd /D "%windir%/system32" /K "$(CURRENT_WORD)" enters just that in the Run... dialogue.

      • SendPlay {Enter} simulates pressing Enter (finalizing the Run... dialogue).

    • If there is no match, the block following else gets executed:

      • SendPlay ^{F5} simulates pressing Ctrl + F5 (fallback if we need the shortcut outside of Notepad++).
  • return terminates the execution of the script at the current point.


See also: