Page 1 of 2

Keep new window open after execution of a command with Shift-Enter

Posted: Mon 28 Jan, 2019 10:43
by jo.navy
When I execute a command from the command line with Shift-Enter I would like to keep the new console window open after the command terminates its execution. I use Shift-Enter, for example, when i run a build from the command line so I need to examine the output to check for build errors.
Keeping the window open was default behaviour in Far 1.65 but not in v1.75 nor in v3 (tested with build 4949). I really need this feature, is there any way to enable it? I would like to get it in v1.75. Please, help!

Keep new window open after execution of a command with Shift-Enter

Posted: Mon 28 Jan, 2019 11:23
by HaRT
jo.navy wrote:
Mon 28 Jan, 2019 10:43
is there any way to enable it
You can easily assign a script (“macro” — see docs) to the ShiftEnter shortcut, which will prepend the command with “cmd.exe /k ” and only then “hit” ShiftEnter — this will give the effect you ask for.

Keep new window open after execution of a command with Shift-Enter

Posted: Mon 28 Jan, 2019 12:03
by Shmuel
HaRT wrote:
Mon 28 Jan, 2019 11:23
You can easily assign a script

Code: Select all

Macro {
  description="Keep the external console window open";
  area="Shell QView Info"; key="ShiftEnter";
  flags="NotEmptyCommandLine";
  action=function()
    Far.DisableHistory(0xF)
    panel.SetCmdLine(nil, "cmd.exe /k "..panel.GetCmdLine())
    Keys("ShiftEnter")
  end;
}

Keep new window open after execution of a command with Shift-Enter

Posted: Mon 28 Jan, 2019 12:46
by jo.navy
Yes, I was already examining the docs about macros and available examples. I also created a simple macro recently.
I know that I can get the content of the command line with CmdLine.Value and I how to hit "hit" Enter, but I'm not sure about the syntax to prepend CmdLine.Value with cmd.exe /k ; furthermore I'm not sure how to handle the case that the command line is empty: in this case Shift-Enter operates on the selected file in panel (if any).

So far I have tried the following with Far v1.75:

Code: Select all

REGEDIT4

[-HKEY_CURRENT_USER\Software\Far\KeyMacros\Shell\ShiftEnter]
[HKEY_CURRENT_USER\Software\Far\KeyMacros\Shell\ShiftEnter]
"Sequence"="$If (!CmdLine.Empty) \"%COMSPEC% /k \" + CmdLine.Value $Else ShiftEnter $End"
"DisableOutput"=dword:00000000
"Description"="Keep new window open after execution of a command with Shift-Enter"
but it has no effect!

Keep new window open after execution of a command with Shift-Enter

Posted: Mon 28 Jan, 2019 12:55
by jo.navy
Shmuel wrote:
Mon 28 Jan, 2019 12:03
panel.SetCmdLine(nil, "cmd.exe /k "..panel.GetCmdLine())
Thank you, Shmuel. Is panel.SetCmdLine available in v1.75 too? I don't see it in "Far Manager Encyclopedia" provided with v1.75.
And what about the case the command line is empty? It's not considered by your script.

Keep new window open after execution of a command with Shift-Enter

Posted: Mon 28 Jan, 2019 13:00
by Shmuel
In the era of Far 1.x I did not use macros so it'd be better you ask somebody else.
My macro is for Far 3.0.
The case when command line is empty is handled by flags="NotEmptyCommandLine" - reread the macro.

Keep new window open after execution of a command with Shift-Enter

Posted: Mon 28 Jan, 2019 13:03
by jo.navy
After Shmuel's Far3 example I have tried also the following:

Code: Select all

REGEDIT4

[-HKEY_CURRENT_USER\Software\Far\KeyMacros\Shell\ShiftEnter]
[HKEY_CURRENT_USER\Software\Far\KeyMacros\Shell\ShiftEnter]
"Sequence"="$If (!CmdLine.Empty) CmdLine.Value =\"%COMSPEC% /k \" + CmdLine.Value; $End ShiftEnter"
"DisableOutput"=dword:00000000
"Description"="Keep new window open after execution of a command with Shift-Enter"
but, again, no effect (the command executes and the the window gets closed).

Keep new window open after execution of a command with Shift-Enter

Posted: Mon 28 Jan, 2019 13:06
by jo.navy
Shmuel wrote:
Mon 28 Jan, 2019 13:00
The case when command line is empty is handled by flags="NotEmptyCommandLine" - reread the macro.
But it means that your macro has no effect when the command line is empty (right?), on the contrary the file selected in active panel should be runned with cmd.exe /k so that the window remains open in this case too.

Keep new window open after execution of a command with Shift-Enter

Posted: Mon 28 Jan, 2019 13:09
by Shmuel
jo.navy wrote:
Mon 28 Jan, 2019 13:06
But it means that your macro has no effect when the command line is empty (right?)
Right. I wrote the macro taking into account exactly your requirements in the original message.

Keep new window open after execution of a command with Shift-Enter

Posted: Mon 28 Jan, 2019 13:22
by HaRT
jo.navy wrote:
Mon 28 Jan, 2019 12:46
it has no effect
Try .
Yet I prefer the & Fast Prefix.

Keep new window open after execution of a command with Shift-Enter

Posted: Mon 28 Jan, 2019 14:28
by jo.navy
HaRT wrote:
Mon 28 Jan, 2019 13:22
"Sequence"="%e = CmdLine.Empty; CtrlHome $Text \"%COMSPEC% /k \" $If(%e) CtrlEnter $End ShiftEnter"
Great! It works. The only drawback is that %COMSPEC% /k appears in the command history but it is acceptable.

Keep new window open after execution of a command with Shift-Enter

Posted: Mon 28 Jan, 2019 15:04
by jo.navy
Minor improvement of HaRT's solution:

Code: Select all

"Sequence"="%e = CmdLine.Empty; %cs=\"%COMSPEC% /k \"; %i=index(CmdLine.Value, %cs); $If(%i != 0) CtrlHome $Text %cs $End $If(%e) CtrlEnter $End ShiftEnter"
In this way if I recall a "ShiftEnter-ed" command from history and hit ShiftEnter again I don't get two %COMSPEC% /k occurrences.

Keep new window open after execution of a command with Shift-Enter

Posted: Mon 28 Jan, 2019 15:51
by jo.navy
Further improvement: replaced %COMSPEC% /k with start . Result is:

Code: Select all

"Sequence"="%e = CmdLine.Empty; %s=\"start \"; %i=index(CmdLine.Value, %s); $If(%i != 0) CtrlHome $Text %s $End $If(%e) CtrlEnter $End ShiftEnter"
In this way if I shift-enter over a RAR file (for example) I get the WinRAR window only, without the console window; all other use-cases work as with previous solution.

Keep new window open after execution of a command with Shift-Enter

Posted: Mon 28 Jan, 2019 16:39
by HaRT
jo.navy wrote:
Mon 28 Jan, 2019 15:51
Further improvement: replaced %COMSPEC% /k with start .
I'd recommend adding empty double quotes as start's first argument — it fixes at least starting .lnk shortcut files for me.

Keep new window open after execution of a command with Shift-Enter

Posted: Mon 28 Jan, 2019 17:17
by jo.navy
HaRT wrote:
Mon 28 Jan, 2019 16:39
I'd recommend adding empty double quotes as start's first argument — it fixes at least starting .lnk shortcut files for me.
Yes, more precisely it fixes any ShiftEnter over a file or directory containing spaces. I've found a vague reference to this fact here. Since start's first argument is the title of the new window, I prefer using "%COMSPEC%" instead of an empty string. The macro, with regedit escapes, becomes:

Code: Select all

"Sequence"="%e = CmdLine.Empty; %s=\"start \\\"%COMSPEC%\\\" \"; %i=index(CmdLine.Value, %s); $If(%i != 0) CtrlHome $Text %s $End $If(%e) CtrlEnter $End ShiftEnter"
I think that the Far3 equivalent of this macro should be added to the Macro folder of future releases.
If there was a way to hide the "start" in command history it would be really perfect.

Keep new window open after execution of a command with Shift-Enter

Posted: Mon 28 Jan, 2019 17:29
by HaRT
jo.navy wrote:
Mon 28 Jan, 2019 17:17
it fixes any ShiftEnter over a file or directory containing spaces. I've found a vague reference to this fact here.
That page is imprecise. As per start /?, the Title argument is optional. Now I see why I needed it in my use cases. I think it is a good idea to quote the file path/name instead of providing a fake window title.

P.S.
Hmm, the start "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Far Manager 3\Far Manager 3.lnk" command doesn't start FAR (I get a cmd window instead), whereas start "" "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Far Manager 3\Far Manager 3.lnk" does.