summary
If one captures diff output to a file name=*.diff and opens that file in a buffer, one gets nicely-formatted mode=Diff. How to get that directly, without first capturing to a file?
details
I use diff a lot, not only via ediff-buffers (which I love) but also for more obscure purposes, like diffing outputs of processes. I've also been using Emacs for several decades, but continue to learn new aspects and uses. So I was not so much surprised as curious to find that, when I did (from the dir/folder of a local git repo)
$ git diff master origin/master > /tmp/foo__local_vs_remote.diff
and opened the file in a buffer, it displayed in a nicely/usefully colorized (à la ediff) mode=Diff. I had just never before simultaneously
- captured
diffoutput to a file - named the file
*.diff - opened the file in Emacs
But now I'm trying to capture diff output directly to a buffer and get output like that of the happy mode=Diff, and am failing.
seeking genericity
Note also that, since I use diff in lots of contexts, I'm looking for a solution to the general problem, and not, e.g., just to the git diff problem (which I suspect Magit etc have solved). Seeking genericity, I'll test 3 usecases: "normal" file-vs-file diff, git diff, and process-substitution diff.
platform
$ lsb_release -ds
LMDE 2 Betsy
$ cat /etc/debian_version
8.3
$ uname -rv
3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt20-1+deb8u4 (2016-02-29)
$ emacs --version
GNU Emacs 24.4.1
options
shell-command
If I do the following (possibly after navigating to a buffer with an appropriate working directory)
M-! <diff command/>C-x b*Shell Command Output*(where the output ofM-!==M-x shell-commandgoes by default)M-x diff-mode
... I get the nicely-formatted output. Furthermore
- this works for all 3 testcases
- if one runs
M-!with anotherdiffcommand, the new output just overwrites the old, but the buffer maintainsdiff-modewithout user intervention - no keybinding changes (as with term-mode)
Chosen answer.
shell-mode
If I either
M-x shell, goto the correct dir/folder, and rungit diff master origin/master- am in a buffer on the correct dir/folder, and run
M-& git diff master origin/master
I get the dreaded WARNING: terminal is not fully functional.
term-mode
As Emacs User points out, M-x term performs better for this usecase than M-x shell: if I M-x term, goto the correct dir/folder, and run git diff master origin/master, I get nicely colorized output. It's not exactly mode=Diff, but it does the job. Unfortunately
- That only works for
git diff: I get no colorization for either process-substitution or filediff. - I find the term-mode keybinding changes highly annoying.
- I also found term-mode navigation odd: when editing history, my cursor would sometimes bounce around oddly. solved).