Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A general solution to this type of problem are <a href="http://www.gnu.org/software/emacs/manual/html_node/emacs/Keyboard-Macros.html#Keyboard-Macros" rel="noreferrer">keyboard macros</a> (not to be confused with (Emacs) LISP macros). Basically Emacs allows you to record a sequence of keystrokes and "play them back" afterwards. This can be a very handy tool in situations where writing custom LISP code seems overkill.</p> <p>For instance you could create the following keyboard macro (type the key combinations on the left hand side, the right hand side shows explanations for each key stroke):</p> <pre><code>C-x ( ; start recording a keyboard macro C-x h ; mark whole buffer C-w ; kill region (require 'file-name) ; insert a require statement into the buffer C-x C-s ; save buffer C-x C-f ; find file file-name.el &lt;RET&gt; ; specify the name of the file M-&lt; ; move to the beginning of the buffer C-u C-y ; insert the previously killed text, leaving point where it is (provide 'file-name) &lt;RET&gt; &lt;RET&gt; ; insert a provide statement into the buffer C-x ) ; stop recording the keyboard macro </code></pre> <p>Now you can re-play that macro in some other buffer by typing <kbd>C-x e</kbd>, or <a href="http://www.gnu.org/software/emacs/manual/html_node/emacs/Save-Keyboard-Macro.html#Save-Keyboard-Macro" rel="noreferrer">save it</a> for later use. You can also bind a macro to a shortcut just like a function.</p> <p>However, there is one weakness with this approach: you want to be able to actually specify the file-name, and not just use the string "file-name" every time. That is a bit difficult - by default, keyboard macros provide no general facility for querying the user (except the very minimal <kbd>C-x q</kbd>, as documented <a href="http://www.gnu.org/software/emacs/manual/html_node/emacs/Keyboard-Macro-Query.html#Keyboard-Macro-Query" rel="noreferrer">here</a>).</p> <p>The <a href="http://www.emacswiki.org/emacs/KeyboardMacros#toc5" rel="noreferrer">Emacs Wiki</a> has some work-arounds for that, however, instead of prompting the user in the minibuffer, it can sometimes be sufficient to start the macro by killing the current line and saving its text to a register.</p> <pre><code>C-x ( C-e C-&lt;SPC&gt; C-a ; mark current line C-x r s T ; copy line to register T C-k C-k ; kill current line ... ; actual macro C-x ) </code></pre> <p>Now when you want to use your macro, you would first write the desired file-name in an otherwise empty line, and then do <kbd>C-x e</kbd> in that line. Whenever the value of the file-name is needed in the macro you can retrieve it from the register T:</p> <pre><code>C-x r i T ; insert file-name into buffer </code></pre> <p>For instance, for the <code>provide</code> statement in the above macro, you could write: <kbd>(provide ' C-x r i T )</kbd>. Note that this technique (inserting) also works in the minibuffer, and of course you could save multiple lines to different registers.</p> <p>May sound complicated, but is actually quite easy in practice.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload