How do I set the size of Emacs' window?

EmacsElisp

Emacs Problem Overview


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.

Emacs Solutions


Solution 1 - Emacs

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 https://stackoverflow.com/questions/5795451/how-to-detect-that-emacs-is-in-terminal-mode for a little more background.

Solution 2 - Emacs

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.

Solution 3 - Emacs

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.

Solution 4 - Emacs

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.

Solution 5 - Emacs

Try adding the following code to .emacs

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

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

Solution 6 - Emacs

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.

Solution 7 - Emacs

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)

Solution 8 - Emacs

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

Solution 9 - Emacs

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

Solution 10 - Emacs

(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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTom DunhamView Question on Stackoverflow
Solution 1 - EmacsBryan OakleyView Answer on Stackoverflow
Solution 2 - EmacsChris ConwayView Answer on Stackoverflow
Solution 3 - EmacsJustinView Answer on Stackoverflow
Solution 4 - EmacsJB.View Answer on Stackoverflow
Solution 5 - EmacsnorbuxView Answer on Stackoverflow
Solution 6 - EmacsGraeme PerrowView Answer on Stackoverflow
Solution 7 - EmacsftraversView Answer on Stackoverflow
Solution 8 - EmacsJérôme RadixView Answer on Stackoverflow
Solution 9 - EmacsPeter MiehleView Answer on Stackoverflow
Solution 10 - EmacsWisdomFusionView Answer on Stackoverflow