2

I am very new with emacs and Lisp, though from experience with other functional languages it's not too hard for me to mimic what I am seeing in useful code snippets. I've added some nice window toggling features in the .emacs file and they are working well.

But on start-up, I'd like to configure a specific arrangement of windows/frames. Basically, I want to do the following each time I launch emacs (which is generally at most once per day and then it is left open for days/weeks).

1. Split the screen in half (C-x 2)
2. Grow the top half bigger by 20 lines (C-u 20 C-x ^)
3. Open a second frame of emacs (C-x 5 2)

Ideally, I'd even like to maximize the first frame on my left monitor and the second frame on my right monitor, but I can do without that.

I am just wondering how you write the function equivalent of the key commands into the .emacs file.

Dale Hagglund
  • 16,074
  • 4
  • 30
  • 37
ely
  • 74,674
  • 34
  • 147
  • 228
  • 1
    The easiest way to go about this is to see what functions those keys are calling by pressing `C-h k` followed by the key combination (`C-x 2`) in your first example. This will give you the function name this key is calling which you can then call in your .emacs file. For `C-x 2` the function is `split-window-below`. – Randy Morris Sep 20 '12 at 12:40
  • Thank you! I didn't think to look for help like that, and it was surprisingly hard to find anything specific about doing this when Googling for it. – ely Sep 20 '12 at 12:53
  • I believe this is a duplicate of http://stackoverflow.com/questions/392314/saving-window-configurations-in-emacs – Squidly Sep 20 '12 at 14:26
  • I think it is definitely different. It's more than just window configurations, though that's what my specific case is. This question is about inputting things you normally do as key combinations as actual functions in the .emacs file. Your linked questions is *just* about saving window configs. – ely Sep 20 '12 at 16:41
  • possible duplicate of [Binding M- / M- in emacs 23.1.1](http://stackoverflow.com/questions/4351044/binding-m-up-m-down-in-emacs-23-1-1) – Thomas Matthews Sep 20 '12 at 19:17

3 Answers3

5

As a follow-up to event_jr's answer, it's interesting to note that the return value of (kbd) can be evaluated directly as a keyboard macro. This can be assigned to a key as an alternative to defining a regular keyboard macro, but in your case -- a one-off sequence you wish to place in your .emacs file, in which efficiency is not a concern -- you might find it nicer to write out the key sequence in the friendly kbd format (and with comments, because kbd deals with those) instead of the output of insert-kbd-macro, which is much harder to comprehend without invoking the macro editor.

(execute-kbd-macro (kbd "
 C-x 2           ;; split-window-below
 C-u 20 C-x ^    ;; enlarge-window
 C-x 5 2         ;; make-frame-command
"))

Here I've just copied and pasted the output from the keyboard macro editor as shown by event_jr, so note that you can use that facility to auto-generate the comments :)

That said, personally I would encourage you to write such things as real function calls:

(split-window-below)
(enlarge-window 20)
(make-frame-command)
Community
  • 1
  • 1
phils
  • 71,335
  • 11
  • 153
  • 198
3

The best feature in Emacs is the self documenting help, so you can easily figure out how to write the desired command in Emacs-lisp with experience in other languages.

But because what you want is a straight forward sqeuence of keys, a macro would serve you best, and it gives you a good place to start writing

Here is a keysequence I entered:

 C-x (  C-x 2 C-u 2 0 C-x ^ C-x 5 2 <switch-frame> C-x ) 

I've recorded a macro to do what you asked. Then M-x edit-last-kbd-macro, I see:

;; Keyboard Macro Editor.  Press C-c C-c to finish; press C-x k RET to cancel.
;; Original keys: C-x 2 C-u 20 C-x ^ C-x 5 2

Command: last-kbd-macro
Key: none

Macro:


C-x 2           ;; split-window-below
C-u 20 C-x ^        ;; enlarge-window
C-x 5 2         ;; make-frame-command

Then M-x name-last-kbd-macro "foo" M-x insert-kbd-macro "foo"

(fset 'foo
   [?\C-x ?2 ?\C-u ?2 ?0 ?\C-x ?^ ?\C-x ?5 ?2 (switch-frame #<frame  *Minibuf-1* 0x101855410>)])

Add the last chunk to your .emacs file, and call it with

(foo)
event_jr
  • 17,467
  • 4
  • 47
  • 62
  • Indeed, that can be a good way to go about it. Be warned that the `switch-frame` event (which is a complex event with a frame argument) will not work, because you can't save&reload a frame description (the `#` cannot be read back), so if you place the above in your .emacs, Emacs will complain at startup about this unknown # – Stefan Sep 20 '12 at 14:23
2

There is a more 'baked-in' way of configuring frame and window configurations. There are three relevant functions:

C-x r f         frame-configuration-to-register
C-x r w         window-configuration-to-register
C-x r j         jump-to-register

Jumping to a register which contains a frame / window configuration will load that frame / window configuration.

A window configuration is not per frame, it (appears to) apply to all frames. I've not tested frame configurations, because they appear not to work under xmonad.

However, I've no idea how you'd actually save them between sessions, so this is more of an FYI than anything else.

Squidly
  • 2,707
  • 19
  • 43