emacslispslime

How to bind a key to kill repl and yank in a buffer on 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:

  1. kill the last history item (the ones that you get with C-up or C-down);
  2. move to the upper buffer;
  3. yank, Save buffer to file;
  4. move back to the repl.

Please, make it a step by step guide, because I'm a complete beginner to Emacs and Lisp.


Solution

  • 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.