4

Color codes are not working on my Windows 7 box. I have working colors in Git Bash that comes with the Git install.

Here is what I get:

enter image description here

  • I can't test this, but I believe adding "--no-color" to the magit-diff-options list would remove the ansi coloring and magit would handle the coloring just fine without. – Jordon Biondo Oct 20 '14 at 16:04
  • Or if that's not used for inline diffs, perhaps setting git config --global color.diff false might do it – Jordon Biondo Oct 20 '14 at 16:07
  • I tried (setq magit-diff-options "--no-color") which led to break magit diff display although --no-color works in Git Bash. – syl20bnr Oct 20 '14 at 17:16
3

Except for "log" Magit never tells Git to --color its output. It also doesn't --no-color explicitly, because that usually is not necessary. In my experience Git is usually capable of figuring out when to use colors and when not. So the most likely cause for this issue that you have (or some tool you use has) explicitly told Git to always color its output.

Check the values of color.ui and color.diff (git config --list | grep color).

color.ui
    This variable determines the default value for variables such as
    color.diff and color.grep that control the use of color per
    command family. Its scope will expand as more commands learn
    configuration to set a default for the --color option. Set it to
    false or never if you prefer Git commands not to use color unless
    enabled explicitly with some other configuration or the --color
    option. Set it to always if you want all output not intended for
    machine consumption to use color, to true or auto (this is the
    default since Git 1.8.4) if you want such output to use color
    when written to the terminal.

I am a bit confused about the "since Git 1.8.4", I was pretty sure that was always the case.

Anyway, if you cannot set this in your Git configuration, then appending ("-c" "core.ui=false") to magit-git-standard-options should also work.

I will add that to the FAQ. The above is actually a copy of my answer to a similar question.

| improve this answer | |
1

You can use the ansi-color package (bundled with Emacs) to handle the output properly without having to turn off colors.

M-x load-library RET ansi-color RET
M-: (ansi-color-apply-on-region (point-min) (point-max)) RET

To automatically apply the function, use the following in your .emacs file:

;; load package
(require 'ansi-color)

;; function for colorizing
(defun colorize-buffer ()
  (interactive)
  (toggle-read-only)
  (ansi-color-apply-on-region (point-min) (point-max))
  (toggle-read-only))

;; add hook to apply the function when magit mode is enabled
(add-hook 'magit-mode-hook 'colorize-buffer)

See also Cucumber's ANSI colors messing up emacs compilation buffer

| improve this answer | |
  • Except that my answer doesn't turn off colors, it just doesn't force them on in cases where it doesn't make sense. Also do you expect users to run this manually every time they see a diff? While this is a good answer to the general question "how do I turn control sequences into colors", it doesn't really make sense in response to the question that was actually asked. – tarsius Jan 8 '15 at 14:15
  • @tarsius Having colors enabled is a very helpful aid and I wouldn't recommend turning them off if they can be handled properly. I have updated the answer so that the output is handled automatically. – mandark Jan 10 '15 at 18:16
  • You want diffs to be colorful in the terminal and you don't want to see control characters when looking at diffs in Magit, right? The easiest way to do that is git config color.ui auto, Git then figures out whether it should output control sequences (in a terminal) or not (everywhere else). Unless that does not work for you, there is no reason to do what you are doing (and even then it would be better to investigate why it doesn't do so). I firmly believe in doing the right thing in the first place, instead of doing the wrong thing (color.ui=true) and then working around that mistake. – tarsius Jan 10 '15 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.