M-x all-things-emacs

Maximize on Startup, Part 1

January 29th, 2007 by Ryan McGeary · 12 Comments

This is part one of a two-part series. Here we’ll discuss some straight forward options for maximizing and resizing the emacs window on startup. In part two, we’ll explore a more advanced alternative.

I like to maximize my emacs window (aka frame) to fit the entire screen when I really want to focus on what I’m doing. On larger displays, this allows me to split windows every which way and view multiple buffers at once. The other day, I realized I was manually maximizing my emacs window after every startup, and I decided I had to put an end to that. There are several ways to accomplish this with elisp, and I am going to touch on a few.

Option 1 (Windows only)

If you’re on Windows, you’ve got a pretty straight forward and decent option. You can access the Windows system commands to maximize the containing window1. Add the following to your .emacs file to maximize on startup:

1
2
3
4
5
6
(defun w32-maximize-frame ()
  "Maximize the current frame"
  (interactive)
  (w32-send-sys-command 61488))
 
(add-hook 'window-setup-hook 'w32-maximize-frame t)

It’s best to maximize the frame after initialization is complete; hence, the need for the window-setup-hook on line 6. This prevents font size and window customizations from affecting the size of the maximized frame.

Option 2 (simple but hard-coded)

If you’re on a Mac or running under X Windows, Option 1 is pretty useless. This next method for handling maximization works under any window system (including Windows). Add the following to your .emacs file to maximize on startup:

1
2
3
(setq initial-frame-alist
      `((left . 0) (top . 0)
        (width . 237) (height . 65)))

This sets the parameters for creating the initial X Windows frame. It positions the emacs frame in the top left corner of the display [0, 0] (measured in pixels) and then resizes the frame to 237 columns by 65 rows. Unfortunately, the number of columns and rows vary depending on font-size, scroll bar visibility, etc. The dimensions of 237×65 just happen to work for my 1440×900 display, 6×13 font, scroll-bars off, etc.

Here is another (hard-coded) alternative. This is especially good if you need to resize the frame after startup.

1
2
(set-frame-position (selected-frame) 0 0)
(set-frame-size (selected-frame) 237 65)

If you don’t necessarily want to maximize your window, but instead want a window with specific dimensions at a specific location, you can skip Option 3 because Option 2 is your best bet2.

Option 3

We will describe Option 3 in part 2 of the series. There, we will use the display-pixel-width and display-pixel-height functions to automatically determine the proper size of the emacs frame.

Bibliography

1 GNU Emacs FAQ, Windows Operations — explains how to use Win32 system commands in elisp to manipulate a window.

2 GNU Emacs FAQ, How do I change the size, position, font, and color attributes of Emacs?

Tags: elisp · osx · tips · windows · x

12 responses so far ↓

  • 1 piyo // Jan 31, 2007 at 5:01 am

    Is it possible to figure out on Windows if the frame is maximized already or not?

  • 2 Ryan McGeary // Feb 3, 2007 at 7:13 pm

    Piyo, I’m not sure how. Maybe someone else can chime in on that, but since you have access to the discrete system command codes for maximize and restore, I’m not sure what the benefit there is towards checking if the window is already maximized. You can always just call w32-maximize-frame before doing whatever it is that requires a maximized frame. Here’s the elisp for telling Windows to restore the frame:

    1
    2
    3
    4
    
    (defun w32-restore-frame ()
      "Restore a minimized/maximized frame"
      (interactive)
      (w32-send-sys-command 61728))
  • 3 Maximize on Startup, Part 2 | M-x all-things-emacs // Feb 22, 2007 at 7:00 am

    [...] part 1 of the series, we discussed two ways to resize the emacs frame on startup. Here, we will use the [...]

  • 4 troels // Jun 4, 2007 at 7:39 pm

    Does anybody have a solution for the rendering-bug, when a window is maximised? The lower part of the screen doesn’t seem to repaint, so the last application, which was open is shining through.

  • 5 Ryan McGeary // Jun 4, 2007 at 11:41 pm

    Troels, That sounds somewhat like a video driver or video card issue.

  • 6 troels // Jun 10, 2007 at 7:33 pm

    It does indeed, but I have no problems in any other application, besides emacs. If I minimise/maximise the window a couple of times it goes away, and seems to be fixed for the remainder of the session. So I put a call to (iconify-frame) in before (w32-send-sys-command 61488)

  • 7 ? Windows ??? Emacs ?????? // Aug 7, 2007 at 9:38 am

    [...] http://www.emacsblog.org/2007/01/29/maximize-on-startup-part-1/ [...]

  • 8 Harvey // Aug 9, 2007 at 3:00 pm

    1. Thank you.
    2. For others using method #2, resize your window and put it on screen where you want it. Now use the following to get your four parameters:
    M-x eval-expression
    (frame-parameter nil ‘top)

    repeat with ‘left, ‘width, and ‘height

  • 9 Austin // Apr 16, 2008 at 4:08 am

    Hmm..
    Wanting to check it out, there are so many

  • 10 shjk // Jun 17, 2008 at 9:11 pm

    I’m using the following to toggle window maximization. It will not notice when your maximize the frame using another method but you can simply hit M-enter twice and it works again.

    (defvar shk-w32-maximized nil)
     
    (defun w32-toggle-maximized ()
      (interactive)
      (if shk-w32-maximized
          (w32-restore-frame)
        (w32-maximize-frame))
      (setq shk-w32-maximized (not shk-w32-maximized)))
     
    (global-set-key [(meta return)] 'w32-toggle-maximized)
  • 11 Frederik // Jun 18, 2009 at 1:36 pm

    emacs -fs

  • 12 Frederik // Jun 18, 2009 at 1:37 pm

    emacs -fh -fw