如何设置Emacs窗口的大小?

111

我正在尝试检测我要在其中启动emacs的屏幕大小,并相应地调整窗口(在emacs中称为frame)的大小和位置。我试图设置我的.emacs文件,以便我始终获得一个“相当大”的窗口,其左上角靠近我的屏幕左上角。

我猜这对于一般情况来说是一个很大的要求,所以为了缩小范围,我最感兴趣的是Windows和Linux下的GNU Emacs 22。

10个回答

91

如果您想根据分辨率更改大小,可以像这样操作(根据您的具体需求调整首选宽度和分辨率):

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

请注意,在较新版本的emacs中,window-system已被弃用。一个适当的替代方案是(display-graphic-p)。有关更多背景信息,请参见此答案以回答问题如何检测emacs是否处于终端模式?


我喜欢不同的数字,所以(正如你建议的那样)我调整了我的。这很好用,谢谢! - lindes
再说,我实际上更喜欢这个版本——更简单(易于阅读和调整),并且很适合我的需求。虽然两个答案都很好!+1 - lindes
1
在 Emacs 24.2 上的 OS X 系统中,要小心高度设置。如果你将其设置得太高,窗口会陷入一种状态,其中缓冲区的第一行不可见 - 光标可以导航到那里,但窗口不会滚动以显示它。 - Greg Hendershott
2
这个方法存在一个小问题(以及其他在此列出的方法)。Emacs会以一定的大小打开,然后在一瞬间分裂后重新调整到我们设置的大小。但是过渡是可见的。有没有办法告诉Emacs从t=0秒开始以所需的大小绘制自己? - stathisk

55

我在我的.emacs文件中有以下内容:

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

您还可以查看函数set-frame-sizeset-frame-positionset-frame-width。使用C-h f(也称为M-x describe-function)以获取详细的文档。

我不确定是否有一种方法来计算当前窗口环境中框架的最大高度/宽度。


16
太好了! (if (window-system) (set-frame-size (selected-frame) 124 40)) 太棒了,简洁明了,可以让我获得终端的默认大小,非常熟悉。 :) (当然可以根据个人偏好进行调整。)谢谢! - lindes
3
只是为了澄清:如果你将这个放在 init.el 中,它只适用于初始框架。如果在此之后创建一个新的框架,上面的函数就没有效果了;新的框架将使用在 default-frame-alist 中设置的设置。 - David J.
这对我来说是有效的,除了(set-frame-size (selected-frame) 80 24)给了我一个161x48的框架;我在4K hidpi屏幕上,但我不希望它影响字符计数。 - charliegreen

22

摘自: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)))

第一个设置适用于所有的emacs框架,包括启动时弹出的第一个框架。第二个设置为第一个框架添加了额外的属性。这是因为有时候知道你在哪个原始框架中启动emacs很不错。


我曾经长时间使用这种方法来设置框架的高度和宽度((width . 80) (height . 40)),但由于我将Xubuntu版本从12.04升级到13.10后,出现了某些问题:启动时,框架大小是默认值。然后我转而使用Chris Conway的答案,这对我很有效。 - Teemu Leisti

19

尝试将以下代码添加到 .emacs 文件中

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

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

18

在X-Window环境中,我发现最简单的方法是使用X资源。 我的.Xdefaults文件中相关部分如下:

Emacs.geometry: 80x70

您可以在末尾加上+0+0的坐标来将其强制放置在显示器的左上角。(我不这样做的原因是,我偶尔会生成新的窗口,如果它们出现在与先前窗口完全相同的位置,会令人困惑)

根据手册,这种技术在 MS Windows 上也适用,将资源存储为键/值对在注册表中。我从未测试过。它可能非常好用,但与编辑文件相比,也可能更加麻烦。


1
是的,它也适用于Windows。Emacs维基提供了更多详细信息:http://www.emacswiki.org/emacs/MsWindowsRegistry - Sa'ad

12
您可以在启动emacs时使用-geometry参数:emacs -geometry 80x60+20+30,这将给您一个80个字符宽、60行高的窗口,顶部左侧角落比背景图像向右移动20个像素并向下移动30个像素。

7
在Ubuntu上执行以下操作:
(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)

5
在Windows系统中,您可以使用以下函数使emacs帧最大化:
(defun w32-maximize-frame ()
  "Maximize the current frame"
  (interactive)
  (w32-send-sys-command 61488))

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)

我更喜欢Bryan Oakley的设置。然而,在我的GNU Emacs 24.1.1中,'height无法正常工作。


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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接