Notes on showing only current directory name in VSCode Terminal window prompt on Windows

Last updated on 26 Apr. 2024
Quick-Info: To show only cur-dir in a particular VSCode Terminal: Run following command in terminal:
Function Prompt { "$( ( get-item $pwd).Name )> " }

In Powershell to retrieve function commands history, use:
func[F8] ... F8 can be pressed multiple times to go further back in commands history

-------

Sometimes my directory path for a VSCode project is very long and so when VSCode Terminal shows the whole path as part of its prompt, it is a distraction and tends to come in the way of viewing the terminal window contents. The notes below capture my efforts to solve the issue. So far, it is not fully successful. 

Ref: Hiding the full file path in VSCode Terminal, https://anjikeesari.com/articles/hide-full-path/
But following above procedure to edit $Profile resulted in this error in VSCode every time I opened a Terminal window:
. : File C:\Users\Ravi-user\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 cannot be loaded
https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:3
+ . 'C:\Users\Ravi-user\Documents\WindowsPowerShell\Microsoft.PowerShel ...
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

---


Launch PowerShell as administrator
Set-ExecutionPolicy -ExecutionPolicy Unrestricted
----

I am hesitant to do above stuff as I don't know what side effects that might have.

....
Meanwhile the temporary fix for a particular Terminal window is to run the command:
Function Prompt { "$( ( get-item $pwd).Name )> " }
...
Alternatives that work:
  • function prompt {>}
  • function prompt {"> "}
Reset to show whole working directory:
  • function prompt {$pwd }
  • function prompt {"$pwd> "}
...

A nice reasonable/mid-way solution is to use the function commands we want once in a VSCode terminal. Later we can simply use 'func' followed by F8 to retrieve commands starting with func (F8 can be pressed multiple times to go further back in history). As VSCode Terminal seems to remember commands history across VSCode sessions/invocations, this seems to be quite a workable option.
Ref: [Windows Powershell] Describes how to get and run commands in the command history., https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_history?view=powershell-7.4


Comments

Archive