16

I'm trying to figure out Elisp, and I've hit a roadblock.

I want a function that will Indent the entire file. Right now, I'm selecting the whole file (C-x h) and then doing M-x indent-region (which does have a shortcut key).

I'd like to combine that in to a single keypress, but can't figure out how to do C-x h in a function.

Thanks

2 Answers 2

23

To find what Emacs will do when you invoke a certain key combination, prefix that with: C-h k. In your case, you'd type:

C-h k C-x h 

which yields

C-x h runs the command mark-whole-buffer, which is an interactive compiled Lisp function in `simple.el'.

It is bound to C-x h, . (mark-whole-buffer)

Put point at beginning and mark at end of buffer. You probably should not use this function in Lisp programs; it is usually a mistake for a Lisp function to use any subroutine that uses or sets the mark.

Note: You can also use C-h K (note the K is capitalized), which will jump you to the documentation for the command.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you sir, C-h k is what I could not find
and C-h c returns just the function, without any documentation, and without opening a help buffer. And, C-h C-h is nice for these situations.
12

It is worth noting that you don't want to use the mark and point in non-interactive code; you want (indent-region (point-min) (point-max)), not (save-excursion (mark-whole-buffer) (call-interactively indent-region)), even though the effects are similar.

(Not to ruin your fun, but the whole sequence will look something like (global-set-key (kbd "C-M-r") (lambda () (interactive) (indent-region (point-min) (point-max))).)

1 Comment

Why is your method preferred over marking the whole buffer / indent region? I'm not sure what the difference is.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.