gokcehan / lf

Terminal file manager
MIT License
7.57k stars 322 forks source link

Yank path w/filename, filename, and file (As in Ranger) in Powershell #1745

Closed bushnerd closed 2 months ago

bushnerd commented 2 months ago

My envs: Windows 11 Powershell 7 Window terminal lf r32。 lfrc as follows:

# 使用 PowerShell 的 Set-Clipboard 命令

# 复制当前对象名称
cmd yn ${{
    pwsh -c '
    $filename = $env:fx -replace ".+\\", ""
    Set-Clipboard -Value $filename
    echo "Filename copied to clipboard: $filename"'
}}

# 复制当前对象完整路径
cmd yp ${{
    pwsh -c '
    Set-Clipboard -Value $env:fx
    echo "Full path copied to clipboard: $env:fx"'
}}

# 复制当前所在目录路径
cmd yd ${{
    pwsh -c '
    Set-Clipboard -Value $env:PWD
    echo "Directory path copied to clipboard: $env:PWD"'
}}

# 键绑定
map yn :yn
map yp :yp
map yd :yd

But the three shortcuts do not work, even there is nothing displayed

joelim-work commented 2 months ago

The default shell used on Windows is CMD, which doesn't support multiline commands. Since you're using PowerShell for scripting, you can configure lf to use that instead of CMD:

set shell pwsh

cmd yd ${{
    Set-Clipboard -Value $env:PWD
    echo "Directory path copied to clipboard: $env:PWD"
}}
bushnerd commented 2 months ago

Thank you very much, but I experience a delay of about 2 seconds after clicking on yd. Do you have the same issue?

joelim-work commented 2 months ago

I notice a small delay too, but I suspect that is just pwsh being slow, nothing to do with lf. Try running pwsh directly, outside of lf.

bushnerd commented 2 months ago

Thanks for your reply. I have try another way to get shorter time to get the path and dir like this:

cmd yank-dir ${{  echo %PWD:~1,-1% | clip}}
map yd :yank-dir

cmd yank-path ${{  echo %f:~1,-1% | clip}}
map yp :yank-path

cmd yank-name ${{
    for %i in ("%f%") do @(echo %~nxi) | clip
    echo File name copied to clipboard: %~nxi
}}
map yn :yank-name

But yank-name does not work well, anyone have try this?

joelim-work commented 2 months ago

I don't really have any proper experience with batch scripting, but I took what you have and ended up with the following which works for me:

cmd yank-name ${{ for %i in ("%f:~1,-1%") do @(echo %~nxi) | clip }}
map yn :yank-name
bushnerd commented 2 months ago

I don't really have any proper experience with batch scripting, but I took what you have and ended up with the following which works for me:

cmd yank-name ${{ for %i in ("%f:~1,-1%") do @(echo %~nxi) | clip }}
map yn :yank-name

Thanks a lot for your reply, it has been solved.