Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding the full file path in a PowerShell command prompt in VSCode

So I've just downloaded Visual Studio Code to use as my default IDE for learning Python. I'm running on a 64-bit machine so I made the default terminal windows powershell.

The place where I'll be saving most of my files is about 8 folders deep which all show up in the terminal before any commands can be written. Is there any way to hide or shorten the file path in the terminal?

like image 368
tarruda23 Avatar asked Aug 31 '18 00:08

tarruda23


People also ask

How do I get the full path of a file in PowerShell?

To get the full path of the file in PowerShell, use the Get-ChildItem to get files in the directory and pass the output to foreach-object to iterate over the file and get the full name of the file.

How do I get the path of a file in VS Code?

You can invoke the vscode window property to retrieve the file path or name depending on what you are looking for. This will give you the name of the file open in the current Tab when you execute the command.

How do I hide a path in Windows Terminal?

If you want to go back to previous status (showing full path), just type prompt without any arguments, and press Enter . If you just want to see current working directory (the same as pwd in linux), type chdir or cd without any arguments and press Enter .


2 Answers

Great Question and Great Answers.

But this information is dated, and Windows is currently updating from Windows 10 to Windows 11. In addition, the base Windows PowerShell has been incorporated into the new Windows Terminal Preview app. which is being used here.

The solution provided above by Mark and Tarruda23 (above) almost works. But Windows throws an error - described below.

The steps:

First, it was necessary to determine whether a profile existed. Using the Windows Explorer, the following path was checked. If a profile already exists, this path shows where an existing profile should be found. On this PC, no profile ( .ps1 ) file existed and this folder was empty. Don't close the Explorer.

C:\Users\prior\OneDrive\Documents\WindowsPowerShell

Since no file exists, a new file needed to be created. This new file must be saved with a specific name - shown below.

Navigate to the empty folder and open PowerShell. The .ps1 profile must be created and saved in this folder. Use the Powershell's build-in text editor to create the new file. Type:

ISE

Then type or paste the following into the empty text file:

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

Save this file with the following name:

Microsoft.PowerShell_profile.ps1

Use the PowerShell to open Notepad and check that .ps1 file. This demonstrates the Windows system has found the new .ps1. Next close the Notepad.

Notepad $profile

Now the PowerShell is probably displaying an error message in red text. This error message reads in part:

\Microsoft.PowerShell _profile.ps1 cannot be loaded because running scripts is disabled on this system.

Run the PowerShell as the Administrator. Type the following.

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

Windows will prompt with a question:

Do you want to change the execution policy?

Type y for yes. This will change and remove the Windows default settings that prevents running script files. Once done, this will remove that error message.

All should be good now. PowerShell will now start and run with shorter and abbreviated PS> prompt that shows either the User name, or the name of the folder where the PowerShell is running.

like image 123
Gray Avatar answered Oct 02 '22 22:10

Gray


As @Biclops suggested, there is good info here: configure PowerShell to only show the current folder in the prompt

However, I needed more basic info to get this to work. This is a very good resource to get started: Windows PowerShell Profiles. So I first followed the steps suggested there:

[always using vscode's integrated terminal using PowerShell]

  1. test-path $profile (is there a profile set up?)
  2. new-item -path $profile -itemtype file -force (assuming the answer to the above is false)
  3. notepad $profile (opens notepad)
  4. paste in (from the SuperUser answer above)

    function prompt {
      $p = Split-Path -leaf -path (Get-Location)
      "$p> "
    }
    
  5. save (you shouldn't have to chose a location, it is already done for you)
  6. reload vscode - you will probably get an error message about running scripts (or just do next step before reload)
  7. Set-ExecutionPolicy RemoteSigned -Scope CurrentUser (at your integrated terminal PS prompt, also from the SuperUser answer)
  8. reload vscode
  9. You should be good to go!
like image 21
Mark Avatar answered Oct 02 '22 22:10

Mark