Is there a (repeat-last-command) in Emacs? - emacs

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?

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.

Repeat last command
C-xz
Once you pressed it, just press only
z
after that and it will repeat (without having to press C-x again).

Yes, there is a repeat command. It's called repeat:
You can repeat commands with C-x z, and hit z to keep repeating.
A bit shocking nobody mentioned repeat-complex-command, available from the key binding C-x ESC ESC.

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

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.

I'm not really sure, but maybe you are searching for this one?
The command C-xz (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

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.

Personally I found Sebastian's idea useful. Here is a working version
(global-set-key "\C-r" #'(lambda () (interactive)
(eval (car command-history))))

This is old, but Google pops post this up first when I was looking to retrieve the last command I typed at the Emacs prompt. None of these answers worked for me so I decided to put in my two cents for those who might stumble upon this later on as I did. I'm using Portacle, but I found what I was looking for in here so I'm hoping it's generic enough to work with different setups. Anyway, what worked for me is using C-&uparrow; and C-&downarrow; to cycle through the history. Using M-p and M-n worked as well, but I prefer using the arrows since I use Bash quite a bit.

dot-mode is a way to repeat the last command(s).
From its commentary:
It emulates the vi `redo' command, repeating the
immediately preceding sequence of commands. This is done by
recording input commands which change the buffer, i.e. not motion
commands.

Related

How can I open multiple minibuffers in emacs?

Say I'm entering a command in the minibuffer, and I realize that I need to remember the path to some file as a param to my command. Can I instead of cancelling the command I started entering to do C-x d or to go to a shell, click (click? what's that?) on a secondary mini buffer to run such command?
You are looking for "recursive editing", specifically the bit discussed in the Recursive Minibuffer docs:
(setq enable-recursive-minibuffers t)
(minibuffer-depth-indicate-mode 1)
The latter line makes things the recursive editing less confusing, by showing the level of recursion. E.g C-x C-f then C-x b will appear like this:

ajax-like command helper for EMACS?

I am new to Emacs. I find it is powerful and very convenient IF one can master its set of commands. I also believe anyone can do this with patience, and the eagerness to learn and USE the software. However it will be much nicer for beginners, and seasonal users to have some command helper plugin that do following thing:
When the user start typing any CONTROL or META, COMMAND, or any special keys, a "ajax-like" or "smart search" window appear below or in the minibuffer showing help text like, for example: If I hold "CONTROL", command helper windows shows:
C-p Up one line
C-n Down one line
C-f Forward one character
C-b Backward one character
C-a Beginning of line
C-e End of line
C-x ...
Then if I continue to type "x" while holding "CONTROL", the window will update and show something like:
C-x C-a add-mode-abbrev
C-x C-b list-buffers
C-x C-c save-buffers-kill-emacs
C-x C-d list-directory
C-x C-e eval-last-sexp
C-x C-f find-file
C-x C-h inverse-add-mode-abbrev
C-x TAB indent-rigidly
C-x C-l downcase-region
C-x C-n set-goal-column
C-x C-o delete-blank-lines
I have tried googling it, but it doesn't seem exist yet. So I guess this is more a feature request for EMACS rather then a question. But it is great if somebody can write a plugin/module to do this.
I do not believe this is out there, but I also believe that it is not really needed. It would just clutter the screen, though, maybe for a rookie it might be indeed useful.
For now, you might consider printing a cheatsheet.
Do not lose hope, I learned it without any sort of help, the key is indeed to just use it often.
Also, there is quite a good logic behind it all, so if you read up the structure in a cheatsheet or anywhere else, then it might help you "guess" what is the right key combo sooner than later.
EDIT: I actually myself asked a similar question, at least the same functionality was required. I accepted an answer which apparently mentions that Icicles offers this functionality.
Also, one advise is that after you type the prefix key, e.g. C-c, typing C-h afterwards will show you the possible completions.
This leads me to the following:
I believe we could use code like:
"If last key is a prefix key, run C-h*, switch back to previous buffer and rehit the last prefix key"
*this is a general method, working even for my own poorly designed minor mode
I also don't know an existing package that would do that, but here's a couple of tips.
To get an overview of all available bindings, press C-h b. The displayed buffer looks fairly raw, because it includes all ways used to enter special characters (and they are usually at the top), but you can search it with C-s if you remember at least a part of the command name.
If you know the prefix (e.g. C-x) but don't remember what comes next, you can follow it with C-h instead (type C-x C-h). That will bring up a similar buffer that only contains bindings that start with the same prefix. It's one of the more obscure features, I think.
Speaking of "if I hold Ctrl", though, showing hints when you've just pressed Ctrl and nothing else if currently impossible to implement at the Lisp level. That would require some lower-level changes.
There are too many keybindings in emacs for this to be practically useful.
I agree with the other responses and just wanted to add that I found the built-in tutorial very useful for getting past the basics. You can reach the tutorial with C-h t.
You'll probably end up changing several of the default bindings, but learning them will help you find your way around new modes (assuming that they work in an "emacsy" way).
I come back to this be cause I found a perfect solution and can't live without it: The emacs-helm and which-key

Emacs Macro to Start in Shell Mode and Run a Command

I have a confession: I don't know Lisp. Despite that fact, with a bit of help from some co-workers, I managed to write an emacs macro/script which:
switched to shell mode (ie. M-x shell-mode)
disabled truncating lines (ie. M-x toggle-truncate-lines)
started a database console (ie. "mysql")
I was then able to start emacs with that macro using the --script option, and suddenly I had a way to start mysql in a much friendlier environment with a single command :-)
But here's the problem: I changed jobs and left that script behind. Now I'd very much like to re-create that script at my new job, but I no longer have any emacs experts to help me write it like I did at the old job.
Now, I really hate SO posts where someone basically says "please write my code for me", so I don't want to do that. However, if any emacs macro experts could at least give me some pointers (like "here's how you invoke a M-x command in a macro"), or point me to an emacs-macro-writing guide, or otherwise "teach me to fish" on this issue, I would greatly appreciate it.
... and if someone just happened to have a similar script already lying around that they wanted to post, I certainly wouldn't complain ;-)
Most emacs commands (i.e., M-x toggle-truncate-lines) can be translated directly to elisp by wrapping them in parentheses:
(toggle-truncate-lines)
The rumours are true, in lisp you just scatter parentheses around and they make magic.
Now in this case, you can do better. Toggling makes sense for an interactive function, but in a program you don't really want to toggle truncate-lines, you want to turn on truncate-lines. Its the same thing if truncate-lines was turned off to begin with, but you don't know when your program will be run next. Anyways, in Emacs, features are often controlled by a variable. In this case, the variable is truncate-lines, and to turn that feature on, you set the variable to t (which means true).
To do this, use:
(setq truncate-lines t)
We use setq instead of = for assignment, because they made lisp before = had been invented.
For the real scoop you should take a look at Robert Chassel's excellent "An introduction to to Programming in Emacs Lisp". It comes built-in with your emacs, you can get to it with C-h i m Emacs Lisp Intro.
A good way (I think) to start writing elisp functions is to record keyboard macros, and then to analyse them using edit-kbd-macro
For example, if you start recording a keyboard macro using f3, then do interactively all the things you want and terminate the macro using f4, you can see the underlying emacs-lisp commands using M-xedit-kbd-macrof4 (this last f4 is the key binding you'd have used to execute the keyboard macro)
<<shell>> ;; shell
<<toggle-truncate-lines>> ;; toggle-truncate-lines
mysql ;; self-insert-command * 5
RET ;; comint-send-input
Now you can write a script using these functions, looking up the documentation (e.g. C-h ftoggle-truncate-lines) to see if you should call them with special arguments in non-interactive mode.
You should also replace self-insert-command by calls to insert.
This should give you something like the following script, which you can call using emacs --load myscript.el
(shell)
(toggle-truncate-lines 1)
(insert "mysql")
(comint-send-input)
Of course, this might not work as expected the first time, so you might have to eval (setq debug-on-error t) to get debugging information.
What version of Emacs are you using?
In Emacs 24, I have M-x sql-mysql, which does everything you ask and has font-locking.

How to work out which package is unsetting keybindings?

Something in my config is unsetting/overwriting some standard keybindings for C-c C-e so that commands in various major modes don't work properly. In AUCTeX C-c C-e should start the "insert environment" dialogue and in org-mode the same keys should start the export/publish dialogue, but in both of these modes C-c C-e is undefined and emacs just waits for more input.
I know I could start emacs with -Q and load packages until something breaks (and if I load emacs that way and open a file in org-mode, the keybindings work as they should) but, like lots of people, at some point my init file became monstrously big. Is there a quicker, smarter way?
[EDIT] Nothing in my config is unsetting C-c C-e explicitly.
The debugger might give you enough clues to sort it out. Try adding the following to the beginning of your .emacs:
(debug-on-entry 'global-unset-key)
You might need to try 'local-unset-key as well. For details see the debugger manual.
Binary search is your friend.
Go to the middle of your .emacs and add (error "here"), then launch Emacs and see if the problem persists.
If it does, then insert another error line half way between the current and the top of the file, if not, comment out the current error and add one half way between the current and the bottom.
Repeat the bisection process until you isolate the line or two which are responsible for causing the problem.
You could use C-h k C-c C-e to find out which function is bound to your combination.
In the popped up help buffer, you will get the function name, its documentation and a link to the file where it is defined. Follow that link and you will end up at the place of the function definition.
At this point, it should be simple to identify which package that is responsible for redefinition of your keybinding using the name of the file.
If this does not help, you could M-x rgrep in your customization directory for 'C-c C-e' or 'C-e' and manually check everything.

Does Emacs has word and line completion (like Vim's insert mode completion)?

Vim completes words and lines with CTRL-X P and CTRL-L. There's a Emacs plugin called Company mode but this plugin interfere and cause conflicts with lots of things within Emacs (with global linum and yasnippets). I know that I can complete words with CTRL-/ in Emacs. But it is possible to take previously written lines to complete code?
Maybe you're looking for hippie-expand? From that web page (as of this writing, anyway):
HippieExpand looks at the word before
point and tries to expand it in
various ways including expanding from
a fixed list (like expand-abbrev),
expanding from matching text found in
a buffer (like dabbrev-expand) or
expanding in ways defined by your own
functions. Which of these it tries and
in what order is controlled by a
configurable list of functions.
For a comprehensive list of completion options visit the emacs wiki page on completion.
There are a gazillion ways to do completion in Emacs. Some are mode specific, some inline, some configurable and what not. Here is a list of modes that might help you.
Use numberic argument to complete by line, say M-5 M-/ will complete by line, while M-/ alone still complete the normal way.
hippe-expend function has a very useful feature which is :
With a positive numeric argument, jumps directly to the ARG next function in this list. With a negative argument or just C-u, undoes the expansion.
Customize the expansion functions in hippie-expand-try-functions-list and put the function try-expand-line as 5th list element, then you could use M-5 M-/ to complete by line.
This tip is very handy and useful and I highly recommend it.
Also worth noting: if your window manager does not steal Alt-tab, emacs will auto-complete with Alt-tab (I set up my window manager to user the "windows key" instead of alt for this very reason).
If you are using evil, this is the most vim-like solution I use:
(defun my-expand-lines ()
(interactive)
(let ((hippie-expand-try-functions-list
'(try-expand-line-all-buffers)))
(call-interactively 'hippie-expand)))
(define-key evil-insert-state-map (kbd "C-x C-l") 'my-expand-lines)
This way you can use our old friend C-x C-l in insert mode for whole line all buffers completion.
Thanks #ymln for the suggestion of using try-expand-line-all-buffers.