Mastertronic
Mastertronic

Reputation: 27

emacs map several keystrokes and command to one key

I'm trying to map c-u m-x indent-pp-sexp to a single key, like F5, so that working with Emacs doesnt erode my fingerprints.

I use (global-set-key (kbd "C-u M-x indent-pp-sexp") "<f5>") but i'm getting the following error:

global-set-key: Key sequence C-u M-x i n d e n t - p p - s e x p starts with non-prefix key C-u

EDIT

With this lambda function (global-set-key (kbd "<f5>") (lambda (interactive) (universal-argument) (indent-pp-sexp t)))

Getting error:

recursive-edit: Wrong type argument: commandp, (lambda (interactive) (universal-argument) (indent-pp-sexp t))

Weird, because univeral-argument takes no parameters, and indent-pp-sexp takes boolean

Upvotes: 2

Views: 308

Answers (3)

Randy Morris
Randy Morris

Reputation: 40927

You're missing the argument list to the lambda. Additionally I think passing t to indent-pp-sexp negates the need to call universal-argument.

(global-set-key (kbd "<f5>") #'(lambda ()
                                 (interactive)
                                 (indent-pp-sexp t)))

Upvotes: 1

Boris Stitnicky
Boris Stitnicky

Reputation: 12578

I'm a noob like you, but I already happened to figure basic things like making macros. I don't really know what's wrong with your code, but here's walkthrough of how I do things at home. What you need to do first, is press F3. Then type your keystrokes, and when finished, press F4. Congratulations, you have defined an anonymous macro. You can replay it as many times you wish by pressing F4 again. When you have played enough, enter M-x name-last-keybord-macro, and name it eg. foobar. Go to your ~/.emacs.d/macros/ directory (make it if you don't have one) and visit a file that you will name foobar.el. In its buffer, M-x insert-kbd-macro. When asked about name, say foobar. You will see that emacs has entered the contents of your just recorded macro into the file. Save it. Open your .emacs file, and add lines:

 (load (expand-file-name "~/.emacs.d/macros/foobar.el"))
 (global-set-key (kbd "M-<f5>") 'foobar)

And things start working for me after restart, with M-F5 as the binding for foobar.el macro.

Upvotes: 0

tripleee
tripleee

Reputation: 189367

You have the arguments the wrong way around, and you bind keys to functions, not to other key sequences. Perhaps you are really looking for a named macro; or you can write some actual Lisp and bind that to F5:

(global-set-key (kbd "<f5>")
                (function (lambda () (interactive) (indent-pp-sexp t) )) )

The presence of an argument in the call form appears to be sufficient to select the prefix argument functionality.

Upvotes: 2

Related Questions