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-uporC-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
F3orC-x (, then make something likeM-p C-a C-k C-u - C-x o C-y C-x o(i just translated your request to key sequence), then typeF4orC-x ). To execute macro, pressF4again, orC-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 holdingF4) 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, typingM-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-macrowhen you are in your.emacsfile. 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.emacsfile:(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 keverything around. Help keys likeC-h f,C-h a,C-h bwill also come in use.