How to set tab size for hunks in "git add -p"?
core.pager
settings doesn't affect git add -p
command because git add -p
doesn't go through less/pager. To change hunks tabs size you need change the tabs size of your terminal. Found solution here. Just added in .bashrc:
env TERM=linux setterm -regtabs 4
That settings works fine in xterm terminals. For other terminal types check manuals for proper solution.
Having an offset for the first tab accounts for the misalignment caused by
/-
/+
diff line prefixes. Take advantage of shell expansion to get stepped numbers:
tabs {5..300..4}
In the above, 5 is the first tab's width (line prefix width + regular tab width), the second number is greater than or equal to the terminal width, the last is the regular tab width. This gets silently expanded to:
tabs 5 9 13 17 21 25 29 33 37 41 45 49 53 57 61 65 69 73 77 81 85 89 93 97 101 105 109 113 117 121 125 129 133 137 141 145 149 153 157 161 165 169 173 177 181 185 189 193 197 201 205 209 213 217 221 225 229 233 237 241 245 249 253 257 261 265 269 273 277 281 285 289 293 297
You can see exactly what you're going to get by using the debug flag (e.g. tabs -d {5..300..4}
). Resetting tabs afterward can be done with tabs -4
or tabs -8
.
The same initial offset effect can be achieved in less
by using -x5,9
.
You can visualize your current tab stops with setterm --tabs
.
for "git add -p", you might want your tab stops to look like this:
10 20 30 40 50
12345678901234567890123456789012345678901234567890123456789
T T T T T T T T T T T T T T
not this:
10 20 30 40 50
12345678901234567890123456789012345678901234567890123456789
T T T T T T T T T T T T T T
To get the first one, you can use tabs -c3
(read here).
To get the second one, you can use tabs -4
.
However, you will break your cat
output if you use tabs -c3
(cat
needs tabs -4
).
So you need tabs -c3
only when running git add -p
.
Here is how I do it:
git() {
if [[ "$1" == "add" ]] &&
[[ "$2" == "-p" ]] || [[ "$2" == "--patch" ]]
then
tabs -c3
if [[ "$#" -eq 2 ]]
then
command git add -p
else
command git add -p "$3"
fi
tabs -4
else
command git "$@"
fi
}
put it in your .bashrc and/or .zshrc