Search code examples
emacsstartupkey-bindings

how to create a specific window setup on in .emacs


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.


Solution

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