111

I'm trying to detect the size of the screen I'm starting emacs on, and adjust the size and position the window it is starting in (I guess that's the frame in emacs-speak) accordingly. I'm trying to set up my .emacs so that I always get a "reasonably-big" window with it's top-left corner near the top-left of my screen.

I guess this is a big ask for the general case, so to narrow things down a bit I'm most interested in GNU Emacs 22 on Windows and (Debian) Linux.

10 Answers 10

91

If you want to change the size according to resolution you can do something like this (adjusting the preferred width and resolutions according to your specific needs):

(defun set-frame-size-according-to-resolution ()
  (interactive)
  (if window-system
  (progn
    ;; use 120 char wide window for largeish displays
    ;; and smaller 80 column windows for smaller displays
    ;; pick whatever numbers make sense for you
    (if (> (x-display-pixel-width) 1280)
           (add-to-list 'default-frame-alist (cons 'width 120))
           (add-to-list 'default-frame-alist (cons 'width 80)))
    ;; for the height, subtract a couple hundred pixels
    ;; from the screen height (for panels, menubars and
    ;; whatnot), then divide by the height of a char to
    ;; get the height we want
    (add-to-list 'default-frame-alist 
         (cons 'height (/ (- (x-display-pixel-height) 200)
                             (frame-char-height)))))))

(set-frame-size-according-to-resolution)

Note that window-system is deprecated in newer versions of emacs. A suitable replacement is (display-graphic-p). See this answer to the question How to detect that emacs is in terminal-mode? for a little more background.

4
  • I like different numbers, so (as you suggest) I tweaked mine. This works great, though; thank you!
    – lindes
    Mar 5, 2011 at 10:42
  • Then again, I actually like this version better -- much simpler (easy to read and tweak), and suits my needs well. Both are good answers, though! +1
    – lindes
    Mar 5, 2011 at 10:48
  • 1
    Be careful with height, at least on Emacs 24.2 on OS X. If you set it one row too high, the windows will be stuck in a state where the first row of the buffer isn't visible -- point can navigate up there, but the window won't scroll to show it. Jan 1, 2013 at 16:05
  • 2
    There's a minor artifact with this method (and with others listed here). Emacs will open up with a certain size and after a split of a second will resize to w/e size we set. But the transition is visible. Is there a way to tell emacs draw itself at the desired size from t=0 sec?
    – stathisk
    Apr 20, 2014 at 11:51
55

I've got the following in my .emacs:

(if (window-system)
  (set-frame-height (selected-frame) 60))

You might also look at the functions set-frame-size, set-frame-position, and set-frame-width. Use C-h f (aka M-x describe-function) to bring up detailed documentation.

I'm not sure if there's a way to compute the max height/width of a frame in the current windowing environment.

3
  • 16
    Yay! (if (window-system) (set-frame-size (selected-frame) 124 40)) for the win -- so nice and concise, and gets me the size my Terminal defaults to, which is nice and familiar. :) (Adjust per preferences, of course.) Thanks!
    – lindes
    Mar 5, 2011 at 10:50
  • 3
    Just to clarify: if you put this in init.el it only applies to the initial frame. If you create a new frame after that, the above function will have not effect; the new frame will have the settings set in default-frame-alist.
    – David J.
    Jan 22, 2013 at 15:01
  • This works for me except that (set-frame-size (selected-frame) 80 24) gives me a frame 161 by 48; I'm on a 4K hidpi screen, but I wouldn't expect that to affect character counts. Dec 20, 2017 at 18:48
22

Taken from: http://www.gnu.org/software/emacs/windows/old/faq4.html

(setq default-frame-alist
      '((top . 200) (left . 400)
        (width . 80) (height . 40)
        (cursor-color . "white")
        (cursor-type . box)
        (foreground-color . "yellow")
        (background-color . "black")
        (font . "-*-Courier-normal-r-*-*-13-*-*-*-c-*-iso8859-1")))

(setq initial-frame-alist '((top . 10) (left . 30)))

The first setting applies to all emacs frames including the first one that pops up when you start. The second setting adds additional attributes to the first frame. This is because it is sometimes nice to know the original frame that you start emacs in.

1
  • I'd used that method for a long while for setting the frame's height and width ((width . 80) (height . 40)), but for some reason, it stopped working after I upgraded my Xubuntu version from 12.04 to 13.10: upon launch, the frame size was the default. I then switched to using Chris Conway's answer, which works for me. Feb 19, 2014 at 12:48
19

Try adding the following code to .emacs

(add-to-list 'default-frame-alist '(height . 24))

(add-to-list 'default-frame-alist '(width . 80)) 
18

The easiest way I've found to do that in an X-Window environment is through X resources. The relevant part of my .Xdefaults looks like this:

Emacs.geometry: 80x70

You should be able to suffix it with +0+0 location coordinates to force it to the upper-left corner of your display. (the reason I don't do it is that I occasionnally spawn new frames, and it makes things confusing if they appear in the exact same location as the previous one)

According to the manual, this technique works on MS Windows too, storing the resources as key/value pairs in the registry. I never tested that. It might be great, it might be much more of an inconvenience compared to simply editing a file.

1
12

You can also the -geometry parameter when firing up emacs: emacs -geometry 80x60+20+30 will give you a window 80 characters wide, 60 rows high, with the top left corner 20 pixels to the right and 30 pixels down from the top left corner of the background.

7

On ubuntu do:

(defun toggle-fullscreen ()
  (interactive)
  (x-send-client-message nil 0 nil "_NET_WM_STATE" 32
                 '(2 "_NET_WM_STATE_MAXIMIZED_VERT" 0))
  (x-send-client-message nil 0 nil "_NET_WM_STATE" 32
                 '(2 "_NET_WM_STATE_MAXIMIZED_HORZ" 0))
)
(toggle-fullscreen)
0
5

On windows, you could make emacs frame maximized using this function :

(defun w32-maximize-frame ()
  "Maximize the current frame"
  (interactive)
  (w32-send-sys-command 61488))
2
(setq initial-frame-alist
        (append '((width . 263) (height . 112) (top . -5) (left . 5) (font . "4.System VIO"))
                initial-frame-alist))

(setq default-frame-alist
        (append '((width . 263) (height . 112) (top . -5) (left . 5) (font . "4.System VIO"))
                default-frame-alist))
2
(defun set-frame-size-according-to-resolution ()
  (interactive)
  (if window-system
  (progn
    ;; use 120 char wide window for largeish displays
    ;; and smaller 80 column windows for smaller displays
    ;; pick whatever numbers make sense for you
    (if (> (x-display-pixel-width) 1280)
           (add-to-list 'default-frame-alist (cons 'width 120))
           (add-to-list 'default-frame-alist (cons 'width 80)))
    ;; for the height, subtract a couple hundred pixels
    ;; from the screen height (for panels, menubars and
    ;; whatnot), then divide by the height of a char to
    ;; get the height we want
    (add-to-list 'default-frame-alist 
         (cons 'height (/ (- (x-display-pixel-height) 200)
                             (frame-char-height)))))))

(set-frame-size-according-to-resolution)

I prefer Bryan Oakley's settings. However the 'height not work properly in my GNU Emacs 24.1.1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.