如何将控制台输出直接传送到记事本?

如何将控制台输出直接传送到记事本?

我在命令提示符下运行应用程序或Gitshell,并且我想将输出放入记事本中,以便以后更轻松地查看和编辑。

我尝试了以下操作,但总是得到一个空的记事本实例:

diff file1.txt file2.txt | notepad

我知道我可以将输出重定向到另一个文件,然后在记事本中打开该文件。我想避免额外的步骤,因为我只习惯于通过管道输入Vim或更少,在非 Windows 系统上。

答案1

据我所知,没有办法直接通过管道传输到记事本。

但是,您可以输入clip然后粘贴在记事本中,如下所示:

 diff file1.txt file2.txt | clip && notepad

然后只需在记事本中按下Ctrl+即可V

答案2

考虑使用Vim,或者如果您喜欢图形环境,则可以使用 gVim。

然后,您可以使用单个连字符作为参数将其输入到其中,指示 Vim/gVim 从标准输入读取。

diff file1.txt file2.txt | gvim -

答案3

这里'一个简短的 Windows 程序可以做到这一点适当地(不会破坏剪贴板)。它应该适用于 PowerShell,如果有时间,我可能会更新此答案,但您也可以直接使用该程序。

那么,PowerShell 怎么样?无需安装其他应用程序。不幸的是,你将要需要在您的某处创建一个脚本文件PATH...

您可以使用的简短版本

如果您创建一个包含以下内容的批处理文件(例如ShowInNotepad.bat)并将其放在PATH某处:

@echo off
clip
powershell -Command $process = Start-Process -PassThru notepad;$SW_SHOW = 5;$sig = '[DllImport("""user32.dll""")] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);';Add-Type -MemberDefinition $sig -name NativeMethods -namespace Win32;[Win32.NativeMethods]::ShowWindow($process.Id, $SW_SHOW) ^| Out-Null;Add-Type -AssemblyName System.Windows.Forms;[System.Windows.Forms.SendKeys]::SendWait('^^V');

然后您就可以echo blah | ShowInNotepad从任何地方拨打电话!

请注意假设您使用的是较新版本的 Windows(Vista+),并且没有禁用 PowerShell 或卸载 .NET 框架。换句话说,默认的 Windows 安装将起作用。


冗长的解释和替代方案

我能想到的最简单的方法是自动执行粘贴( Ctrl+ V)操作。至少还有一个其他答案已经在做这件事了,但是那个答案使用了 AHK - 您可能更幸运地让 PowerShell 在锁定的公司环境中工作。

我们继续写剧本吧?

#start notepad, get process object (to get pid later)
$process = Start-Process -PassThru notepad;

# activate Notepad window
# based on http://stackoverflow.com/a/4994020/1030702
# SW_SHOW activates and shows a window http://msdn.microsoft.com/en-us/library/windows/desktop/ms633548%28v=vs.85%29.aspx
$SW_SHOW = 5;
$sig = '[DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);';
Add-Type -MemberDefinition $sig -name NativeMethods -namespace Win32;
[Win32.NativeMethods]::ShowWindow($process.Id, $SW_SHOW) | Out-Null;

# send a "Ctrl+V" keystroke to the active window
# from http://stackoverflow.com/a/17851491/1030702
Add-Type -AssemblyName System.Windows.Forms;
[System.Windows.Forms.SendKeys]::SendWait('^V');

这很简单,所以我不会费心去解释脚本,只会解释评论已经解释的内容。

用法

要使用它,您只需将脚本放在一个.ps1文件中(例如ShowInNotepad.ps1),将其放在您的某个位置PATH,然后powershell ShowInNotepad.ps1在将要显示的文本放在剪贴板后调用。

例子:

echo blah | clip && powershell ShowInNotepad.ps1

不幸的是,执行 PowerShell 脚本有时可能很困难(执行策略等等)。因此,我将此脚本压缩为一行,您可以直接从命令提示符调用,甚至可以放入批处理文件中:

powershell -Command $process = Start-Process -PassThru notepad;$SW_SHOW = 5;$sig = '[DllImport("""user32.dll""")] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);';Add-Type -MemberDefinition $sig -name NativeMethods -namespace Win32;[Win32.NativeMethods]::ShowWindow($process.Id, $SW_SHOW) ^| Out-Null;Add-Type -AssemblyName System.Windows.Forms;[System.Windows.Forms.SendKeys]::SendWait('^^V');

如果您创建一个包含以下内容的批处理文件(例如ShowInNotepad.bat)并将其放在PATH某处:

@echo off
clip
powershell -Command $process = Start-Process -PassThru notepad;$SW_SHOW = 5;$sig = '[DllImport("""user32.dll""")] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);';Add-Type -MemberDefinition $sig -name NativeMethods -namespace Win32;[Win32.NativeMethods]::ShowWindow($process.Id, $SW_SHOW) ^| Out-Null;Add-Type -AssemblyName System.Windows.Forms;[System.Windows.Forms.SendKeys]::SendWait('^^V');

然后您就可以echo blah | ShowInNotepad从任何地方拨打电话!

答案4

这是完全可能的;我刚刚试过了。我假设记事本默认设置为打开 txt 文件:

diff file1.txt file2.txt > output.txt && start output.txt && timeout /T 3 && del output.txt

好的,从技术上讲您正在创建一个文件,但它并没有被保存。

通过管道传输更多内容也是一个选择。

相关内容