tags:

views:

3627

answers:

8

frequently, I've dug into apropos and docs looking for something like the following only to give up to get back to the task at hand:

(repeat-last-command)

do the last C- or M- command I just executed (to be rebound to a fn key)

or sometimes the related:

(describe-last-function)

what keystroke did I just mistakenly issue, the effect of which I'd like to add to my bag of tricks. describe-key is close, but requires knowing what I typed.

am I simply asking too much from my trusty sidekick?

+20  A: 

Repeat functionality is provided by the repeat.el Emacs Lisp package, which is included with standard Emacs distributions. From repeat.el's documentation:

This package defines a command that repeats the preceding command, whatever that was, including its arguments, whatever they were. This command is connected to the key C-x z. To repeat the previous command once, type C-x z. To repeat it a second time immediately after, type just z. By typing z again and again, you can repeat the command over and over.

To see additional information about the repeat command, type C-h F repeat RET from within Emacs.

Emerick Rogul
+2  A: 

I'm not really sure, but maybe you are searching for this one?

The command C-x z' (repeat') provides another way to repeat an Emacs command many times. This command repeats the previous Emacs command, whatever that was. Repeating a command uses the same arguments that were used before; it does not read new arguments each time.

Emacs Manual, 8.11 Repeating a Command

echox
+7  A: 

with regards to 'describe-last-function':

There's a variable last-command which is set to a symbol representative of the last thing you did. So this elisp snippet - (describe-function last-command) - ought to bring up the documentation for the thing that immediately happened.

So you could make a trivial working describe-last-function like so

(defun describe-last-function() 
  (interactive) 
  (describe-function last-command))

Put that elisp in .emacs or equivalent, and you'll have a M-x describe-last-function.

If you've banged on a few keys or done something that modified last-command since the thing you're interested in, the command-history function might be of interest. You can get that by M-x command-history

cms
n.b. to editor - code example was correct as I intended, I reverted your edit.
cms
Sorry, cms, I misunderstood the snippet.
Chris Conway
+7  A: 

Repeat last command

C-x z

Once you pressed it, just press only
z
after that, and it will repeat without having to press C-x again.

Johannes Schaub - litb
+5  A: 

Also, M-x view-lossage shows you the last hundred(?) keystrokes you entered. So, you'll be able to see where the command is. It's what i used until i just right now found out about M-x command-history which i think i'll be using with C-h w now.

quodlibetor
+3  A: 

A bit shocking nobody mentioned repeat-complex-command, available from the key binding C-x ESC ESC.

ashawley
similar to M-x M-p except that repeat-complex-command repeats previous arguments.
RamyenHead
A: 

May be this would help too... From emacs Help verbatim:

C-x M-ESC runs the command repeat-complex-command
  which is an interactive compiled Lisp function in `simple.el'.
It is bound to <again>, <redo>, C-x M-:, C-x M-ESC.
(repeat-complex-command ARG)

Edit and re-evaluate last complex command, or ARGth from last.
A complex command is one which used the minibuffer.
The command is placed in the minibuffer as a Lisp form for editing.
The result is executed, repeating the command as changed.
If the command has been changed or is not the most recent previous command
it is added to the front of the command history.
You can use the minibuffer history commands M-n and M-p
to get different commands to edit and resubmit.
Murali VP
A: 

Hi,

I used C-x ESC ESC for execute last command. It's kind of boring repeating that 'cause you need to press enter or feed with another argument every time. What I want is: "position the cursor and press the bind for repeat last command". So, I was reading something about lisp and emacs manual, I have read:

The list of previous minibuffer-using commands is stored as a Lisp list in the variable command-history. Each element is a Lisp expression which describes one command and its arguments. Lisp programs can reexecute a command by calling eval with the command-history element.

Then, I read something about lisp lists , but really I don't get it, I have tried something like this:

(defun repeat-last-command()
  (interactive)
  (eval (car (command-history)))
)

But seems not to work. How then we extract the elements from (command-history) list and feed then to eval?

Regards,

PS: new here, I didn't read on the FAQ nothing about asking on a question. I then take the decision not to start a new question about it, as it's related with this one. I apologize if this is not the behaviour but honestly I didn't read nothing about it.

Sebastian