Search code examples
emacselisp

Get width of current monitor in Emacs Lisp


I have a two-monitor setup (running Ubuntu).

The Emacs Lisp function display-pixel-width gives me the combined width of the two monitors. How can I get the width of the current monitor (i.e., the monitor displaying the current frame)?


Solution

  • If you are using 24.3 or earlier:

    display-pixel-width is a compiled Lisp function in `frame.el'.

    (display-pixel-width &optional DISPLAY)

    Return the width of DISPLAY's screen in pixels. For character terminals, each character counts as a single pixel.

    Additionally, if you are using 24.4 or later:

    ** Multi-monitor support has been added.

    *** New functions display-monitor-attributes-list and frame-monitor-attributes can be used to obtain information about each physical monitor on multi-monitor setups.

    Use an external process

    You can also parse the output of xwininfo or xrandr (use call-process).

    Maximize emacs

    Finally, you can maximize emacs (either interactively or using modify-frame-parameters; version 24.4 also has toggle-frame-fullscreen and toggle-frame-maximized) and query its frame size using frame-pixel-height and frame-pixel-width.

    See also

    1. How do I find the display size of my system in Emacs?
    2. Can I detect the display size/resolution in Emacs?