Skip to content

PowerShellFar user commands

Roman Kuzmin edited this page Aug 12, 2024 · 2 revisions

Use Register-FarCommand in order to create custom commands invoked by prefixes from Far Manager command line, user menu, file associations, macros.

Such commands may be useful as shortcuts for more complex PowerShell commands. Commands may be registered dynamically or in your PowerShellFar profile script.

See some examples used by the author below.

see: for Go-Everything.ps1

Profile.ps1

Register-FarCommand -Prefix see -Name Go-Everything -Id f5ad6f53-859f-4bf0-95ee-f3feba3c7a20 {
    Go-Everything.ps1 $_.Command
}

The command see: calls Go-Everything.ps1 directly and passes the typed text as the parameter Filter.

NB Commands like see: <file-system-name> are suitable for keeping in external notes as normal readable text references. Later on they may be copy/pasted and invoked in Far Manager for navigating to targets.

ib: for Invoke-Build

Profile.ps1

Register-FarCommand -Prefix ib Invoke-Build fd44c660-ae10-4a36-b933-64aa74cef6c9 {
    $Far.InvokeCommand(('ps: Invoke-Build ' + $_.Command.Trim()))
}

The command ib: calls Invoke-Build by PowerShellFar ps:, not directly as see: does. This is simpler than the direct call and also provides the echo, i.e. the command is printed before its output.

The direct call to Invoke-Build is also possible, with more steps:

  • sync the current location with the panel directory
  • build the piece of PowerShell code to be invoked
  • invoke this code as the created script block
  • direct the expected output to the host
Register-FarCommand -Prefix ib Invoke-Build fd44c660-ae10-4a36-b933-64aa74cef6c9 {
    Set-Location -LiteralPath $Far.CurrentDirectory
    $Command = 'Invoke-Build ' + $_.Command.Trim()
    & ([scriptblock]::Create($Command)) | Out-Host
}

NB In addition, in your PowerShell profile you may define the alias ib:

Set-Alias ib: Invoke-Build

As a result, you get

  • PowerShell code completion as for Invoke-Build.
  • Ability to copy and reuse ib: commands in pwsh.

Using Lua macros

Alternatively, Lua macros may be used for similar commands.

see: for Go-Everything.ps1

CommandLine {
  id = "f5ad6f53-859f-4bf0-95ee-f3feba3c7a20";
  description = "Go-Everything";
  prefixes = "see";
  action = function(prefix, text)
    Plugin.SyncCall("10435532-9BB3-487B-A045-B0E6ECAAB6BC", "ps: Go-Everything.ps1 " .. text)
  end;
}

ib: for Invoke-Build

CommandLine {
  id = "fd44c660-ae10-4a36-b933-64aa74cef6c9";
  description = "Invoke-Build";
  prefixes = "ib";
  action = function(prefix, text)
    Plugin.SyncCall("10435532-9BB3-487B-A045-B0E6ECAAB6BC", "ps: Invoke-Build " .. text)
  end;
}