Configure powerline to display git status

Viewed 8521

I've installed powerline and the default configuration displays my current git branch. I'd also like to display the status of that branch (number of staged/unstaged files etc...) I found powerline-gitstatus which seems to do the job but I can't figure out how to configure it..

I have copied the configuration entries displayed at https://github.com/jaspernbrouwer/powerline-gitstatus#configuration to ~/.config/powerline/themes/shell/default.json as per the instructions provided there.

The next section states

Then you can activate the Gitstatus segment by adding it to your segment configuration, for example in .config/powerline/themes/shell/default.json:

{  
   "function": "powerline_gitstatus.gitstatus",  
   "priority": 40  
}

That file (.config/powerline/themes/shell/default.json) does not exist so I created it, added that entry, then restarted my shell. It didn't have the desired effect though. I thought maybe I should add that same entry to .config/powerline/config.json but I don't know where in there to put it.

If someone can guide me through this I'd be very grateful.

2 Answers

For me, the default theme did not show any VCS info at all, so I switched to default_leftonly. Using that theme, the following worked for me:

Make sure you have the git_status python module installed:

python3 -m pip install powerline_gitstatus

Create a file ~/.config/powerline/colorschemes/default.json containing

{
  "groups": {
    "gitstatus":                 { "fg": "gray8",           "bg": "gray2", "attrs": [] },
    "gitstatus_branch":          { "fg": "gray8",           "bg": "gray2", "attrs": [] },
    "gitstatus_branch_clean":    { "fg": "green",           "bg": "gray2", "attrs": [] },
    "gitstatus_branch_dirty":    { "fg": "gray8",           "bg": "gray2", "attrs": [] },
    "gitstatus_branch_detached": { "fg": "mediumpurple",    "bg": "gray2", "attrs": [] },
    "gitstatus_tag":             { "fg": "darkcyan",        "bg": "gray2", "attrs": [] },
    "gitstatus_behind":          { "fg": "gray10",          "bg": "gray2", "attrs": [] },
    "gitstatus_ahead":           { "fg": "gray10",          "bg": "gray2", "attrs": [] },
    "gitstatus_staged":          { "fg": "green",           "bg": "gray2", "attrs": [] },
    "gitstatus_unmerged":        { "fg": "brightred",       "bg": "gray2", "attrs": [] },
    "gitstatus_changed":         { "fg": "mediumorange",    "bg": "gray2", "attrs": [] },
    "gitstatus_untracked":       { "fg": "brightestorange", "bg": "gray2", "attrs": [] },
    "gitstatus_stashed":         { "fg": "darkblue",        "bg": "gray2", "attrs": [] },
    "gitstatus:divider":         { "fg": "gray8",           "bg": "gray2", "attrs": [] }
  }
}

create a file ~/.config/powerline/themes/shell/default_leftonly.json containing

{
  "segments": {
    "left": [
      {
        "function": "powerline.segments.common.net.hostname",
        "priority": 10
      },
      {
        "function": "powerline.segments.common.env.user",
        "priority": 30
      },
      {
        "function": "powerline.segments.common.env.virtualenv",
        "priority": 50
      },
      {
        "function": "powerline_gitstatus.gitstatus",
        "priority": 40
      },
      {
        "function": "powerline.segments.shell.cwd",
        "priority": 10
      },
      {
        "function": "powerline.segments.shell.jobnum",
        "priority": 20
      },
      {
        "function": "powerline.segments.shell.last_pipe_status",
        "priority": 10
      }
    ]
  }
}

and create a file ~/.config/powerline/config.json containing

{
  "ext": {
    "shell": {
      "theme": "default_leftonly"
    }
  }
}

I also struggled quite a lot to figure out the right configuration. Once getting it right, I wrote this Article guiding one through the setup of powerline. Including installing the right font and configuring Visual Studio Code. I hope it helps.

To summarize do the following:

Create the directories:

mkdir ~/.config/powerline
mkdir ~/.config/powerline/colorschemes   
mkdir ~/.config/powerline/themes
mkdir ~/.config/powerline/themes/shell

Copy the default config files (maybe path has another python version):

cp ~/.local/lib/python3.7/site-packages/powerline/config_files/colorschemes/default.json ~/.config/powerline/colorschemes/
cp ~/.local/lib/python3.7/site-packages/powerline/config_files/colorschemes/solarized.json ~/.config/powerline/colorschemes/
cp ~/.local/lib/python3.7/site-packages/powerline/config_files/themes/shell/default.json ~/.config/powerline/themes/shell/

Add the following lines to both files in ~/.config/powerline/colorschemes

The lines need to be added to the "groups" section, and don't forget the comma in the last line before appending the lines.

"gitstatus":                 { "fg": "gray8",           "bg": "gray2", "attrs": [] },
"gitstatus_branch":          { "fg": "gray8",           "bg": "gray2", "attrs": [] },
"gitstatus_branch_clean":    { "fg": "green",           "bg": "gray2", "attrs": [] },
"gitstatus_branch_dirty":    { "fg": "gray8",           "bg": "gray2", "attrs": [] },
"gitstatus_branch_detached": { "fg": "mediumpurple",    "bg": "gray2", "attrs": [] },
"gitstatus_tag":             { "fg": "darkcyan",        "bg": "gray2", "attrs": [] },
"gitstatus_behind":          { "fg": "gray10",          "bg": "gray2", "attrs": [] },
"gitstatus_ahead":           { "fg": "gray10",          "bg": "gray2", "attrs": [] },
"gitstatus_staged":          { "fg": "green",           "bg": "gray2", "attrs": [] },
"gitstatus_unmerged":        { "fg": "brightred",       "bg": "gray2", "attrs": [] },
"gitstatus_changed":         { "fg": "mediumorange",    "bg": "gray2", "attrs": [] },
"gitstatus_untracked":       { "fg": "brightestorange", "bg": "gray2", "attrs": [] },
"gitstatus_stashed":         { "fg": "darkblue",        "bg": "gray2", "attrs": [] },
"gitstatus:divider":         { "fg": "gray8",           "bg": "gray2", "attrs": [] }

Next, edit the file ~/.config/powerline/themes/shell/default.json

Change:

"function": "powerline.segments.shell.jobnum",
"priority": 20

To:

"function": "powerline_gitstatus.gitstatus",
"priority": 40

Finally, restart the powerline daemon:

powerline-daemon --replace

After that the GIT specific information should be visible within powerline.

Related