简体   繁体   English

如何摆脱powershell中的当前工作目录

[英]how to get rid of present working directory in powershell

I am working on a project that is buried deep beneath the folders and directories and because of that my powershell's line is half filled with the path to the directory.我正在处理一个深埋在文件夹和目录下的项目,因此我的 powershell 行有一半是目录路径。 I want to know if it is possible to get rid of the long string of directory path that is constantly showing.我想知道是否有可能摆脱不断显示的一长串目录路径。

Building on the last answer I usually put the current path in the title bar of the console window and use the history ID for the prompt.基于上一个答案,我通常将当前路径放在控制台窗口的标题栏中,并使用历史 ID 作为提示。 Something like:就像是:

Function Prompt
{
    $cwd = (get-location).Path
    $LastHist = ( Get-History)[-1].Id + 1

    $Host.UI.RawUI.WindowTitle = "SPoSh_$($PSVersionTable.PSVersion.Major) - $cwd | User: $($env:USERNAME)"

    $Host.UI.Write("Green", $Host.UI.RawUI.BackGroundColor, "SPoSh_$($PSVersionTable.PSVersion.Major)")
    " #$LastHist : "
}

So this would look like:所以这看起来像:

"SPoSh_5 #1: " “SPoSh_5 #1:”

I leave a space at the end which makes it easier to double click select a previous command without capturing any of the prompt itself.我在末尾留了一个空格,这样可以更轻松地双击选择上一个命令,而无需捕获任何提示本身。

Note: If you are working in the regular console the title bar additions let you know where you are without crowding the prompt.注意:如果您在常规控制台中工作,则添加的标题栏可以让您知道自己的位置,而不会挤满提示。

The title bar stuff doesn't show in some of the other non-colsole PowerShell hosts, like VSCode's integrated console, but during script dev the location is usually fairly static, so it's not too much trouble.标题栏内容不会显示在其他一些非 colsole PowerShell 主机中,例如 VSCode 的集成控制台,但在脚本开发期间,该位置通常是相当静态的,因此不会太麻烦。

Add this to your script, then call the function after starting your script.将其添加到您的脚本中,然后在启动脚本后调用该函数。

function prompt {
  $p = Split-Path -leaf -path (Get-Location)
  "$p> "
}

This way only the "leaf" folder will be shown for the prompt placeholder.这样,提示占位符只会显示“叶”文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何摆脱Batch / Powershell中的安全警告 - How to get rid of security warning in Batch/Powershell cd命令后批处理文件获取当前工作目录 - Batch file get present working directory after cd command 如何使用正确的工作目录从BAT文件启动PowerShell脚本? - How to start PowerShell script from BAT file with proper Working Directory? R CMD检查错误:如何摆脱Windows上devel R包中的隐藏文件和目录? - R CMD check error : how to get rid of hidden files and directory in devel R package on windows? 如何通过Powershell从目录条目获取IADSUser本机对象? - How to get the IADSUser native object from directory entry through powershell? 如何使用Powershell从Active Directory上的计算机获取磁盘信息 - How to get disk information from computers on Active Directory with Powershell 使用 Regex 重命名文件名并使用 powershell 删除所有内容 - Rename file name with Regex and get rid of everything after with powershell 如何摆脱控制台 window - How to get rid of the console window 如何获取相对于工作目录的路径? - How to get path relative to the working directory? 如何获取当前工作目录的最大目录深度 - How to get the maximum directory depth of the current working directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM