Czipperz
Czipperz

Reputation: 3336

Emacs hotkey for diffing buffer from file without reading (confirming) buffer name

I have made a hotkey that calls the diff-buffer-with-file command but then you have to hit enter to confirm that the file you are on is the one to correctly diff: (global-set-key (kbd "C-c e") 'diff-buffer-with-file).

I looked up the documentation and saw that you also give &optional BUFFER, so how do I specify the file name?

I saw online that the file name is stored in buffer-file-name so I tried (global-set-key (kbd "C-c e") '(diff-buffer-with-file (buffer-file-name))) but it fails with the error: Wrong type argument: commandp, (diff-buffer-with-file (quote (buffer-file-name))).

Upvotes: 1

Views: 47

Answers (1)

Drew
Drew

Reputation: 30701

You can only bind a command (or a keyboard macro) to a key. (diff-buffer-with-file (buffer-file-name)) is not a command. If that's the code you want, then you either need to use defun to define a named command that uses that code or you need to use an anonymous command (lambda form) that uses it. And you do not need to pass the file name as argument.

(global-set-key (kbd "C-c e") (lambda () (interactive) (diff-buffer-with-file)))

The main thing you were missing was the interactive spec, which makes a function into a command.

Upvotes: 2

Related Questions