Notepad++ Run current line in Command Prompt

2

2

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?

TMRW

Posted 2012-05-15T00:20:16.543

Reputation: 974

Answers

1

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:

Dennis

Posted 2012-05-15T00:20:16.543

Reputation: 42 934

Sorry but that just gives me the line number. What i need are the contents from that line. – TMRW – 2012-05-21T19:34:00.163

As per this topic: http://superuser.com/questions/424559/remove-delete-current-line-in-notepad i figured i could do HOME, HOME, SHIFT+END and then run Open in CMD via shortcut key (in my case CTRL+SHIFT+C) and put all this in the macro and the macro in context menu. Unfortunately it did not work for some reason. The text on the current line becomes selected but never opens in CMD window...

– TMRW – 2012-05-21T19:37:00.377

I missed the word number. The updated answer is tested and should work. – Dennis – 2012-05-22T02:55:00.843