2

Is there any way to show in Terminal of VS Code to show in brackets current branch? I saw it somewhere but not sure how it can be done. By some extension or whatever..

C:/myUser/project> git status

I would like to see it something like:

C:/myUser/project>(master) git status
Henry
  • 537
  • 1
  • 9
  • 22

2 Answers2

1

Open ~/.zshrc in an editor of your choice.

nano ~/.zshrc

Add this text in the end of zshrc file

autoload -Uz vcs_info
precmd() { vcs_info }
zstyle ':vcs_info:git:*' formats 'on branch %b'
setopt PROMPT_SUBST
PROMPT='%n in ${PWD/#$HOME/~} ${vcs_info_msg_0_} > '

Source zshrc file

Reload your .zshrc (or restart your terminal)

source ~/.zshrc
greybeard
  • 2,249
  • 8
  • 30
  • 66
  • 1
    This is not an answer to the question.The question was on a Windows OS. Your answer looks as it explains how to do on a UNIX OS. – ChristianB Jan 04 '21 at 17:04
0

For Linux Terminal

You can modify the PS1 variable. PS1 is a Bash Environment Variable that represents the primary prompt string which is displayed when the shell is ready. You can achieve your result by modifying this variable with a script. First, get the output of your current value of the variable by running

$ echo $PS1

Sample output:[\u@\h \W]$

Now you save the following code in a bash file(Remember to replace the initial string of export PS1 with the output of the above command).

#!/bin/bash
source ~/.bashrc
get_cur_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
 }
export PS1="[\u@\h \W]\$(get_cur_branch)\$ "

Let's say path of the file is "/home/samar/Documents/my_vs_script.sh"

Now change your VS code settings by adding the following lines in 'settings.json'

"terminal.integrated.shellArgs.linux": [
"--init-file",
"/home/samar/Documents/my_vs_script.sh"
]

Now each time you open a new terminal in VS code, script file "my_vs_script.sh" will execute and you get the desired output.

For Windows-Powershell

The solution above works well for the Linux terminal. But if you want to do it for another command-line shell-like Powershell, you can change the 'setting.json' to

{
"terminal.integrated.shellArgs.windows": [
    "-NoExit",
    "-Command", "c:/scripts/myscript.ps1"
  ]
}

where 'myscript.ps1' must have a function 'prompt' definition to add git branch to your prompt.

You can refer this question for your 'myscript.ps1' code. You don't need to change 'Microsoft.PowerShell_profile.ps1'. Defining it in another file works too.

I hope it helps.

samkan
  • 101
  • 1
  • 5