I have this ~/.gitconfig alias:

b = "!r() { count=10; git for-each-ref --sort=-committerdate refs/heads --format='%(HEAD)%(color:bold green)%(committerdate:relative)|%(color:yellow)%(refname:short)|%(color:red)%(objectname:short)%(color:reset)|%(color:blue)%(subject)|%(color:magenta)%(authorname)%(color:reset)' --color=always --count=${count:=10} | column -c 10 -ts '|'; }; r"

My issue with this function is that it formats the string based on the longest row, i.e. if I have a table with the following values (This is not the output from git branch --format, just an example to illustrate column behavior):

a, b, c
x, yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy, z

then it will look like

a, b                                                  , c
x, yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy, z

This is a good behavior which column is responsible to, however I want to trim the string length, so, for example, no column will be longer > 4:

a, b   , c
x, yyyy, z

I managed to add a piped awk to do something like that (used 25 chars):

b = "!r() { count=10; git for-each-ref --sort=-committerdate refs/heads --format='%(HEAD)%(color:bold green)%(committerdate:relative)|%(color:yellow)%(refname:short)|%(color:red)%(objectname:short)%(color:reset)|%(color: blue)%(subject)|%(color:magenta)%(authorname)%(color:reset)' --color=always --count=${count:=10} | column -c 10 -ts '|' | awk '{for(i=1;i<NF;i++){$i=substr($i,1,25)}; print $0}'; }; r"

but it messes up the table :(

It also misbehaves with the * in the start of the current branch.

How can I fix this?

  • I tried to install a newer version of column which should have a -c option to control width but I couldn't :( (downloaded from here and then tried to ./configure && make install. Didn't work as expected).
  • Tried to format the function with \ and newlines - didn't work. Must gitconfig functions look so ugly?
$ git --version
git version 2.34.1

1 Answer 1


If you don't have access to a column implementation that understands output width limitation, one way would be to implement the "column-building" algorithm in awk. However, implementing the logic - while straighforward - adds quite a bit to the length of your gitconfig function:

awk -F'|' -v maxw=4 '{for (i=1;i<=NF;i++) {l=length($i); if (l>maxw) l=maxw; if (l>mw[i]) mw[i]=l; lines[NR]=$0}}
    END{for (j=1;j<=NR;j++) {n=split(lines[j],f,/\|/); for(i=1;i<=n;i++) {printf("%*s%s",mw[i],substr(f[i],1,maxw),i==n?ORS:OFS)}}}'

As you can see, this awk program splits the input on the | (which is what you stated to be the actual output of the git branch command).

  • For each line it iterates over all fields and stores the maximum field width in an array mw for each column, but limited to the maximum width as specified in the variable maxw.
  • It then stores the "original" content of each line (including the original field separators) in a buffering array lines.
  • At end-of-input, it iterates over the lines buffer and splits the line again at the | into an array f. For each entry of f, it prints at most maxw characters of the field via printf, where the desired field width is taken from the mw array for the respective column.

For your example (but assuming the actual |-separation instead of the , you showed), the output would be:

a    b c
x yyyy z
  • Yeah, now I see. Thanks! however, integrating this into my gitconfig is not working. I guess I need to escape something here, but can't find what (other than escaping " which didn't fix the issue) Jan 17, 2022 at 10:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .