How to bind a key to kill repl and yank in a buffer on emacs - emacs

You can assume that I'm in repl using the slime mode.
How can I make a function key (for example, f4), to do this:
kill the last history item (the ones that you get with C-up or C-down);
move to the upper buffer;
yank, Save buffer to file;
move back to the repl.
Please, make it a step by step guide, because I'm a complete beginner to Emacs and Lisp.

The easiest way to make what you ask would be using emacs macros.
Why?
Because you have just said exactly what you want to do.
And macros save the sequence of keys you typed.
You can do it in emacs for one time, and save the sequence of pressed keys.
So, start recording a macro (when you are in the repl buffer) using F3 or C-x (, then make something like M-p C-a C-k C-u - C-x o C-y C-x o(i just translated your request to key sequence), then type F4 or C-x ). To execute macro, press F4 again, or C-x e.
You can interrupt recording a macro if you made a mess with C-g. The reverse is applied, if you made a mess and error message is send, your macro recording(sometimes frustrating) or evaluating(and this is feature, since you can make macro that will work good by just holding F4) would be interrupted.
If you want to use this macro later, you can name it with M-x name-last-kbd-macro. This will allow you to use as a command, typing M-x <your macro name> (<your macro name> - name of your macro). This will not save it for future sessions, just name it.
To save your named macro, use M-x insert-kbd-macro when you are in your .emacs file. This will insert elisp code at current point, executing which you will get your macro binded to your command name(and it will be executed every time you start emacs).
To bind it to some key, rather start it every time from M-x, insert this in your .emacs file: (global-set-key [f12] '<your-macro-name>). You can read more about setting comands to keys there and there.
The bad thing about macro is that you will undo every step, not the whole macro in one time(but someone may bring solution here, if he have one). If you want to make something more serious, using conditions or iterations, you have to forward your path to elisp. Just C-h k everything around. Help keys like C-h f, C-h a, C-h b will also come in use.

Related

emacs with nrepl.el for Clojure - how to hide a popup buffer?

I'm using emacs 23 (acquamacs) and have installed nrepl.el.
I've evaluated some Clojure code with
C-c C-p
(this key combo is to Evaluate the form preceding point and display the result in a popup buffer.)
Running it has produced a nice popup buffer for me to see the results.
How do I hide this popup buffer? (I don't want to kill it - just get my fullscreen back).
FYI Re this command - I've run this with
C-h k C-c C-p
and got
^C ^P runs the command nrepl-pprint-eval-last-expression, which is an interactive compiled Lisp function in `nrepl.el'.
It is bound to ^C ^P, <menu-bar> <nREPL> <Eval last expression in popup buffer>.
(nrepl-pprint-eval-last-expression)
Evaluate the expression preceding point and pprint its value in a popup buffer.
You haven't provided enough information for anyone to test (that's not a standard global binding), and you haven't even indicated whether the new window is selected; but there's a generic solution which will work regardless.
Add (winner-mode 1) to your init file, and then you can always use C-c<left> to undo the most recent window configuration change (or changes plural, if you repeat the command).
Either that or just press q. I just tried it out with the help buffer and it dismisses the window but not the buffer. However, popup buffers in the style of ac-nrepl does for its autocompletion are meant to simply be dismissed
Always, when asking Emacs questions, say what the key sequence does. C-c C-p does nothing here. TO find out what it does press C-h k C-c C-p and report back. In this case we're invoking 'describe-key and then telling it to describe what C-c C-p does.

Is there a way to go back to a keyboard macro from elisp?

I have been creating macros and saving them for later use with the following process.
Create keyboard macro
Edit the macro: C-x C-k C-e
Name the macro: M-x name-last-kbd-macro
Create an .el file then insert the macro definition: M-x insert-kbd-macro
With the lisp code saved for later, I'll load it up in my .emacs file. Perhaps it is my unfamiliarity with elisp, but the elisp code which is generated seems quite unwieldy. I much prefer the syntax in the macro editor, and I'd like to make updates and refinements to the code that I've created, so I ask:
Is there a way to take my elisp code back to a reasonable rendition in the macro editor?
You can use C-x C-k e M-x followed by the name of the macro you want to edit. (Or C-x C-k e (edit-kbd-macro) followed by the key sequence a macro is bound to; M-x here is just a kind of mnemonic, as the help for the command explains).
Maybe it's also helpful to know that all the keyboard macros you've run are saved in a global ring, so you can cycle backwards and forwards through them using C-x C-k C-n and C-x C-k C-p.
BTW, it's not just unfamiliarity with Elisp that makes those macro definitions hard to read; insert-kbd-macro basically just writes out the macro as a vector of key sequences. Elisp written by humans is a much pleasanter language ;-)

Simple Emacs keybindings

I have two operations that I do all the time in Emacs:
Create a new buffer and paste the clipboard. C-S-n
Close the current buffer. C-S-w
Switch to the last viewed buffer. C-TAB
I feel like a keyboard acrobat when doing the first two operations. I think it would be worth trying some custom key bindings and macros.
A few questions about this customization:
How would I make a macro for #1?
Are these good key bindings? (I know this is a bit subjective, but they might be used by something popular that I don't use.)
Has anyone found a C-TAB macro that will act like Alt+Tab in Linux/Windows? Specifically, I want to have a stack of buffers according to the last viewed timestamp (most recent on top). I want to continue cycling through the stack until I let go of the Ctrl key. When the Ctrl key is released, I want the current buffer to get an updated position on the stack.
Have you tried using vertically or horizontally split windows for this (via C-x 3 or C-x 2)? It seems like it would give you fewer steps - even if you implement something like you're talking about.
I find split windows really speed up copy and pasting operations. I use the arrow keys on my num pad to switch among windows (windmove-left/-right/-up/-down), so it's only one key to press and you go to the window you want.
I guess this is a little different from what you're asking for, but it sounds like it might help speed things along a bit.
C-x left and C-x right cycle through buffers, but you have to hit it multiple times, you can't just keep the key pressed down.
For creating a macro for #1, you just start a macro, hit the keys you usually do to create a new buffer, and stop the macro.
So it would be something like:
C-x ( C-x b NEW RET C-x )
You can then save NEW to a file once you're done pasting, so you can use the macro again to create a new buffer. C-x e to try out the macro. If it works you can save it into your init.el file. This is done with:
M-x name-last-kbd-macro
Then you'll get a prompt to enter the name of your choice. This is only good for the current session. Then you save the named macro to your initialization file. First you open your .emacs or init.el file. Then you place point where you want the macro definition to go, then you type:
M-x insert-kbd-macro
Now you can run your macro using its name via M-x <macroname> . You can bind your macro to keys too (in your .emacs or init.el file):
(global-set-key (kbd "C-c a") '<macroname>)
For example this is how your init.el would look after creating a macro that opens a new buffer called NEW that is not associated with a file and binding this macro to C-c n:
;; Creates a new unassociated buffer called NEW
(fset 'new-buffer "\C-xbNEW\C-m");
;; Shortcut for new-buffer
(global-set-key (kbd "C-c n") 'new-buffer)
You can also throw in the paste, buffer close, and buffer switching operations. I guess you'd have to save the buffer to a file manually.
Some resources
Information about macros on EmacsWiki
Possibly useful: Swap text between buffers
start by invoking start-kbd-macro, finish by with end-kbd-macro. Afterwards you may immediately test the new macro with call-last-kbd-macro. If you're happy with the result you might want to save the macro.
Emacs generally doesn't use C-S keybindings and they are easy to use, so I'd call them good. They might cause problems if you're using the terminal version of Emacs, but I assume that's not the case with you.
I use this simple snippet:
(global-set-key (kbd "<C-tab>") 'bury-buffer)
bury-buffer basically makes the current buffer the last in the buffer-list so you'll be able to cycle buffers in a predictable order.
I wouldn't make a macro for that but write a function like someone else posted on this page. Instead of (cua-paste nil) you could also use (yank). I'm not sure which one's better and why.
I don't like them that much. For things that I use often I'd like to do as little finger acrobatics as possible, so that would mean modifier+key instead of modifier1+modifier2+key.. or use a function key if you don't feel tied to the homerow.
no comment

How to save all functions I entered in LispBox/Slime?

Situation: I entered several functions while working with REPL in Emacs.
Problem: There is junk like "; Evaluation aborted" when I'm simply saving buffer.
What I want: clear descriptions of all the functions I entered in their latest revision.
Can I do that? Thanks.
I don't get it. Are you entering definitions at the REPL and expecting to recover them later? Just save a source file as you would in any other language. Use C-x 2 to split your Emacs window in two. Open a source file in one of them C-x C-f foo.lisp. Use C-c C-k, C-c C-r and friends (see SLIME menu) to compile / evaluate regions of your source code in the REPL.
I've looked for something like this in the past and have been unable to find it. You're best off writing all your definitions in a separate buffer and using SLIME's extensive evaluation/compilation functions (C-c C-k loads an entire file, C-x C-e evaluates the last expression, C-c C-r evaluates a region, etc.), only directly entering into the REPL things you don't want to save.
Um, C-x o or C-x b to get to the SLIME REPL buffer, then C-x w or C-x C-s to save it to a file. All the SLIME/CL stuff is a reader comment; you can either write a reader hack to reload the file treating the prompts as comments, or you can go through the file yourself to capture the pieces you want to save.
I agree that the best work flow method is to write your code in a separate buffer and evaluate in that, rather than enter the functions in the repl.
Assuming you have gone the repl way, I guess, C. Martin's solution to save the repl log and manually go through it are your only options.
If you entered the functions and vars into a separate package, you could go through the symbols in the package to help you decide what you want to keep.
E.g. to see all symbols created in the cl-user package:
(let ((p (find-package :cl-user)))
(loop
for s being the symbols in p
when (eq p (symbol-package s))
do (format t "~a~%" s)))

Is there a (repeat-last-command) in 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.