1

I would like to set core.pager in order to manage long lines wrapping text. In order to do it I use this command:

$ GIT_PAGER="fold -sw $COLUMNS" git log

I tried to set core.pager, but Bash evaluate $COLUMNS too early:

$ git config core.pager "fold -sw $COLUMNS"
$ grep pager .git/config
    pager = fold -sw 80

If I try to escape $COLUMNS, I get:

$ git config core.pager "fold -sw \$COLUMNS"
$ grep pager .git/config
    pager = fold -sw $COLUMNS
$ LC_ALL=en_US git log
fold: option requires an argument -- 'w'
Try 'fold --help' for more information.

If I try to use sh -c I get:

$ git config core.pager 'sh -c fold -sw $COLUMNS'
$ grep pager .git/config
    pager = sh -c fold -sw $COLUMNS

..but it doesn't evaluate $COLUMNS correctly (it's static).

How could I get the same behavior of the first command I wrote setting it in core.pager?

Edit: as Nick Bastin has noted git config core.pager "fold -sw \$COLUMNS" should work, but probably I've a problem with the child process.

Francesco Frassinelli
  • 3,145
  • 2
  • 31
  • 43
  • have you considered using a pager which gets the columns from your terminal emulator and doesnt need to be told explicitly? (like `less`) – Nevik Rehnel Mar 16 '13 at 09:44
  • @NevikRehnel less has only `less -r`, that it's good for diffs, but not for logs, where I would get a new line without splitting a word in two parts. – Francesco Frassinelli Mar 16 '13 at 09:48

2 Answers2

5

Solved setting core.pager like this:

$ git config core.pager "fold -sw \$(tput cols)"

Thanks to Nick Bastin (which shown the real problem) and this topic: LINES and COLUMNS environmental variables lost in a script

Community
  • 1
  • 1
Francesco Frassinelli
  • 3,145
  • 2
  • 31
  • 43
2

So, this isn't particularly an answer, but I've tested escaping $COLUMNS (as you do in your question), and it works fine as long as COLUMNS is actually exported in my shell (otherwise it fails as you have above). Are you sure that COLUMNS is defined in your shell, and that git isn't aliased to something that causes you to lose your environment (or aliased to --no-pager)?

Edit: Try a different environment variable. It turns out that there's a lot of things that want to write the COLUMNS variable, and one of them might be screwing you up (I noticed that fold would rewrite the variable after I ran it, so it only worked once anyhow). Try changing $COLUMNS to $FOO and seeing if that makes a difference.

And, indeed, grep itself rewrites the $COLUMNS variable on my system to the width of my terminal, so by using grep to check the value you may be breaking it:

$ export COLUMNS=10
$ echo $COLUMNS
10
$ grep foo
^C
$ echo $COLUMNS
150
Nick Bastin
  • 30,415
  • 7
  • 59
  • 78
  • `echo $COLUMNS` works and I've no shell alias. I've a very simple git configuration and I'm using Konsole (KDE 4.10.1) actually with Bash 4.2.42. – Francesco Frassinelli Mar 16 '13 at 09:51
  • @FrancescoFrassinelli: I was able to do the second thing you did above (escaping it such that .git/config was written properly) and then doing `export COLUMNS=5` would dynamically change the number of columns to 5 (which is amusingly unreadable, of course). You must have *something* messing with your environment in the child process. – Nick Bastin Mar 16 '13 at 09:54
  • @FrancescoFrassinelli: check my edited post - I think your problem is actually `grep` resetting the value of the variable. – Nick Bastin Mar 16 '13 at 09:59
  • You're right: If I do `export COLUMNS=$COLUMNS` it works, and `grep` is not a good way to check it, but it's not the main problem. Maybe the pager is executed in another bash process that can't see the original $COLUMNS? – Francesco Frassinelli Mar 16 '13 at 10:05
  • @FrancescoFrassinelli: the way to test this would be to assign the value you want to a variable that no one else will use, like `export KDFK=$COLUMNS`, and then using `$KDFK` in your config - if that works, then you just have someone screwing with your `COLUMNS` variable. If it has the same problem, then you have a problem in general with child process environments (which would be very strange). – Nick Bastin Mar 16 '13 at 10:07
  • With `export KDFK=$COLUMNS` works. It works also with `export COLUMNS=$COLUMNS` (probably this make the variable static). – Francesco Frassinelli Mar 16 '13 at 10:11