设置 Emacs 并排分割缓冲区

问题描述 投票:0回答:8

很多 Emacs 功能会自动分割屏幕。然而,它们的做法都是使窗户一个在另一个之上。有什么方法可以让它们分开,以便默认情况下它们是并排的吗?

emacs
8个回答
98
投票
(setq split-height-threshold nil)
(setq split-width-threshold 0)

GNU Emacs Lisp 参考手册:选择窗口选项


8
投票

这里有两种解决方案,使用您喜欢的任何一种:

A:默认垂直(左/右):

(setq split-height-threshold nil)
(setq split-width-threshold 0)

B:如果当前窗口足够宽,则自动垂直(左/右)分割窗口

(defun display-new-buffer (buffer force-other-window)
  "If BUFFER is visible, select it.
If it's not visible and there's only one window, split the
current window and select BUFFER in the new window. If the
current window (before the split) is more than 100 columns wide,
split horizontally(left/right), else split vertically(up/down).
If the current buffer contains more than one window, select
BUFFER in the least recently used window.
This function returns the window which holds BUFFER.
FORCE-OTHER-WINDOW is ignored."
  (or (get-buffer-window buffer)
    (if (one-window-p)
        (let ((new-win
               (if (> (window-width) 100)
                   (split-window-horizontally)
                 (split-window-vertically))))
          (set-window-buffer new-win buffer)
          new-win)
      (let ((new-win (get-lru-window)))
        (set-window-buffer new-win buffer)
        new-win))))
;; use display-buffer-alist instead of display-buffer-function if the following line won't work
(setq display-buffer-function 'display-new-buffer)

将任意一项放入您的

.emacs/init.el
文件中。 您可以根据您的屏幕将“100”更改为您喜欢的值。

如果一个框架中有两个窗口,并且您想将布局从垂直更改为水平或反之亦然,这里有一个解决方案:

(defun toggle-window-split ()
  (interactive)
    (if (= (count-windows) 2)
      (let* ((this-win-buffer (window-buffer))
            (next-win-buffer (window-buffer (next-window)))
            (this-win-edges (window-edges (selected-window)))
            (next-win-edges (window-edges (next-window)))
            (this-win-2nd
             (not (and (<= (car this-win-edges)
                        (car next-win-edges))
                    (<= (cadr this-win-edges)
                        (cadr next-win-edges)))))
         (splitter
          (if (= (car this-win-edges)
                 (car (window-edges (next-window))))
              'split-window-horizontally
            'split-window-vertically)))
    (delete-other-windows)
    (let ((first-win (selected-window)))
      (funcall splitter)
      (if this-win-2nd (other-window 1))
      (set-window-buffer (selected-window) this-win-buffer)
      (set-window-buffer (next-window) next-win-buffer)
      (select-window first-win)
      (if this-win-2nd (other-window 1))))))
;; C-x 4 t 'toggle-window-split
(define-key ctl-x-4-map "t" 'toggle-window-split)

将其放入您的

.emacs/init.el
文件中,使用
C-x 4 t
切换窗口的布局。


4
投票
(setq split-height-threshold 0) (setq split-width-threshold 0)

我必须使用它来获得所需的行为(无水平分割)


4
投票

有时我们需要根据当前显示和我们的要求(更多行或更多列)在水平和垂直之间进行更改。

我推荐很棒的ToggleWindowSplit,我将按键绑定到“C-c y”

http://www.emacswiki.org/emacs/ToggleWindowSplit


1
投票

将 2 个变量设置为 nil 和 0 的简单答案对我来说不起作用,所以我编写了 2 个简单的函数:一个只是将窗口拆分为 NX 垂直缓冲区并打开名为(例如) file.1 file.2 的文件。 .. 每个文件中的 file.NX 和另一个文件中的 NX 的想法相同,除了它是 2D 形式的(用于打开文件 f.1 f.2 ... f.[NX*NY] 的 NY 行乘 NX 列)。要安装,请将此代码添加到 .emacs:

    (defun grid-files-h (nx wx pfx)
  "Using dotimes, split the window into NX side-by-side buffers of width WX and load files starting with prefix PFX and ending in numbers 1 through NX"
  (let (ox fn k)  ; ox is not used, but fn is used to store the filename, and k to store the index string
    (dotimes (x (- nx 1) ox) ; go through buffers, x goes from 0 to nx-2 and ox is not used here
;     (print x)
      (setq k (number-to-string (+ x 1) ) )  ; k is a string that goes from "1" to "nx-1"
;     (print k)
      (setq fn (concat pfx k) ) ; fn is filename - concatenate prefix with k
;     (print fn)
      (find-file fn) ; open the filename in current buffer
      (split-window-horizontally wx) ; split window (current buffer gets wx-columns)
      (other-window 1) ; switch to the next (right) buffer
      )
    (setq k (number-to-string nx )) ; last (rightmost) buffer gets the "nx" file
    (setq fn (concat pfx k) ) ; fn = "pfx"+"nx"
    (find-file fn ) ; open fn
    (other-window 1) ; go back to the first buffer
    )  
  )

   (defun grid-files-sq (ny wy nx wx pfx)
      "Using dotimes, split the window into NX columns of width WX and NY rows of height WY and load files starting with prefix PFX and ending in numbers 1 through NX*NY"
      (let (oy ox fn k)  
        (dotimes (y ny oy) ; go through rows, y goes from 0 to ny-1 and oy is not used here
          (split-window-vertically wy) ; create this row
          (dotimes (x (- nx 1) ox) ; go through columns, x goes from 0 to nx-2 and ox is not used here
        (setq k (number-to-string (+ 1 (+ x (* y nx) ) ) ) ) ; k must convert 2 indecies (x,y) into one linear one (like sub2ind in matlab)
        (setq fn (concat pfx k) ) ; filename
        (find-file fn ) ; open
        (split-window-horizontally wx) ; create this column in this row (this "cell")
        (other-window 1) ; go to the next buffer on the right 
        )
          (setq k (number-to-string (+ nx (* y nx) ) ) ) ; rightmost buffer in this row needs a file too
          (setq fn (concat pfx k) ) ; filename
          (find-file fn ) ; open
          (other-window 1) ; go to next row (one buffer down)
          )
        )
      )

然后要使用垂直的,我转到*scratch*(

C-x b *scratch* RET
C-x 1
),输入
(grid-files-h 3 20 "file.")
,然后输入
C-x C-e
,或者如果您想测试方形qrid,
C-x 1
,输入
(grid-files-sq 2 15 3 20 "f.")
,然后输入
C-x C-e
,您应该会看到类似的内容 2x3 grid

这可能可以做得更好/更有效,但它只是一个开始,它做了我需要它做的事情(显示一堆按顺序命名的小文件)。请随意改进或重复使用。


1
投票

我经常在 emacs 中为不同的项目使用多个框架(OSX 窗口)。以下是我如何设置最初分割到左右窗口的几个框架。

  (defun make-maximized-split-frame (name)
    (let (( f (make-frame (list (cons 'name  name))) ))
      (maximize-frame f)
      (split-window (frame-root-window f) nil t)
      ))

  (make-maximized-split-frame "DocRaptor")
  (make-maximized-split-frame "Gauges")
  (make-maximized-split-frame "Instrumental")

0
投票

直接的答案是按

C-c 3

从问题中不清楚您是否想要永久更改设置,但我发现这个问题正在寻找这个答案,但没有找到。 (答案实际上在过去 11 年里一直存在于评论中)


0
投票

我经常使用的 emacs ediff 模式总是显示彼此上方和下方的两个缓冲区,今天我真的想要并排显示。上面的建议只对我有用,但后来我看到,如果您单击“?”,弹出的 ediff 窗口会给出答案:点击“|”字符来切换分割方向。它一直在那里,但我从未看过。

© www.soinside.com 2019 - 2024. All rights reserved.