Running a cmd command from notepad++ and putting the output into a new buffer?

Solution 1:

It seems to be me the original Q is about Notepad++ supporting something similar to vi's ability to read the output (stdout) of a shell command into the current doc (general buffer in vi termonology),

vi Example

:r! ls

Description

: ( ex line editor command ) r ( read ) ! ( from the shell, the output (stdout) of the command ls into the current doc ( nothing specified, default is to read into general buffer )

Notepad++ v 7.7.1 ( 32-bit ) Example

F5

or navigate menu system, Run menu to Run.. menu-item

Enter the desired command into the textbox of the notepad++ Run dialog,

cmd.exe /c echo %DATE% | clip

Press ENTER key or Click Run button

Ctrl+V contents of clipboard into Notepad++ document.

Solution 2:

Is it possible to use Notepad++ to run a command from the Windows command prompt and place the output in a Notepad++ buffer?

As far as I am aware, not directly. That said, a batch file might help in this case e.g.:

@rem Redirects both STDOUT and STDERR to e.g. cli_tmp.txt
%* > C:\temp\cli_tmp.txt 2>&1 
"C:\Program Files (x86)\Notepad++\notepad++.exe" C:\temp\cli_tmp.txt
exit()

If you save this as ex. open_npp.bat and register it in your Windows Path, you could then use ex.:

open_npp echo hello

at a command prompt and the text output from echo hello (i.e. hello) would appear in a new Notepad++ tab labeled as cli_tmp.txt. Furthermore, you could use the Notepad++ Run... menu item to execute cmd /k open_npp echo hello to get the same effect as running open_npp echo hello from the command line.

Notes

  • The biggest drawback to this approach is that you'll likely want to copy the output or re-save your temp document with a different title each run, as your output will be typically overwritten each time the batch file is called.

  • As is, in the batch example given, output from STDERR is written to the temp file (vs. console) and the command window is closed immediately upon completion (exit()). However, this behavior can obviously be changed.

  • This method likely won't catch interactive command input/output properly. Likewise, if a process hangs or takes forever, you might be stuck guessing about what's happening.

  • I have never had an issues using this kind of batch file but there could always be edge cases. =P

Solution 3:

You can use pork2sausage. Just open Plugins Admin in newer Notepad++, or install Plugin Manager in older versions, then install the plugin from there. Otherwise go to the Plugin Central and download the zip file manually

The plugin transforms a selected text to whatever you want, once your transformer (a console program which takes inputs then generates an output) is set properly with Pork to Sausage plugin.

The attractive part of this plugin is its flexibility: one (or more) small program(s) can be written in any language (such in java or .Net), or usage of the other programs in order to transform your selected text in Notepad++.

Up to 20 commands are customizable.

Plugin Central


Edit: The old link doesn't work anymore so I've replaced with an archive link. You can find the new updated list here

After installing open Plugins > Port to Sausage > Edit User Commands and add whatever command you want to the ini file. If it's not there you'll need to create it first. After that restart Notepad++ for the plugin to reload the new config

Here's an example to convert the selected text to uppercase (of course in reality you should use the built-in case conversion from Notepad++)

[toupper]
progPath=C:\Windows\System32\cmd.exe
progCmd=cmd /c "echo $(SELECTION) | tr [:lower:] [:upper:]"
workDir=C:\Users\...\tmp

That assumes GNU tools are available in your path. You can also use PowerShell which is already available for everyone:

[toupper]
progPath=C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe
progCmd=powershell -C "Write-Host -NoNewline '$(SELECTION)'.ToUpper()"
workDir=C:\Users\...\AppData\Local\Temp

To make the above example work with newlines or special characters it's a little bit trickier because you need to add 2 more options progInput and progOutput to pass the input string and read output back as file like this

[toupper_multiline]
progPath=C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe
progCmd=powershell -C "$t = '$(TIMESTAMP)'; (Get-Content -Encoding ascii ('C:\Users\user\AppData\Local\Temp\in_' + $t)).ToUpper() | Set-Content -Encoding ascii ('C:\Users\user\AppData\Local\Temp\out_' + $t)"
workDir=C:\Users\user\AppData\Local\Temp
progInput=C:\Users\user\AppData\Local\Temp\in_$(TIMESTAMP)
progOutput=C:\Users\user\AppData\Local\Temp\out_$(TIMESTAMP)

Note that currently it doesn't work properly due to some bug in encoding handling in the plugin

You can also prevent the output to overwrite the selected text with replaceSelection. You can find more information in the plugin's comment or in pork2Sausage.ini

Solution 4:

In Notepad++ Console window is possible to execute a shell command and redirect the output into clipboard buffer.

For example, to copy current date and time into clipboard:

cmd /c Echo %DATE% %TIME% | clip