ChatGPT解决这个技术问题 Extra ChatGPT

将 Emacs 设置为并排拆分缓冲区

很多 Emacs 功能会自动分屏。但是,它们都这样做是为了使窗户一个在另一个之上。有没有办法让它们分开,默认情况下它们是并排的?

我会在这个问题中交换水平和垂直的使用 - 我会说默认行为是水平分割(分割是屏幕上的水平线)。
Cx 3 运行命令 split-window-horizontally,用于提供并排窗口的命令,所以我使用的是相同的。
@Skilldrick“垂直”和“水平”是模棱两可的,可以有不同的解释;他们可以描述分隔线的方向或分区的方向。我的正常倾向是同意原始问题的措辞(也就是说,我通常会将“垂直分割”解释为“分割垂直空间”)。

E
Eric Drechsel
(setq split-height-threshold nil)
(setq split-width-threshold 0)

GNU Emacs Lisp 参考手册:Choosing Window Options


请注意,这些之所以起作用,是因为它们如何影响 split-window-preferred-function 和设置为的 split-window-sensably 函数 - 如果您为此阅读文档,您可以了解这些变量的设置如何影响事物。对于我们这些默认情况下喜欢垂直分割的人,我们可以使用 (setq split-width-threshold nil) ,它不允许系统水平分割窗口。
在阅读了文档并玩了一下之后,我将 split-height-threshold 设置为 nil 并将 split-width-threshold 设置为 80 以便它首先查看它是否可以水平拆分,然后才尝试垂直拆分。当窗户变窄时,让它只垂直分割通常会变得很难看。
这听起来很有道理。但是,这不适用于 emacs 中的 GDB/GUD 集成。如果我只有一个窗口并启动调试器,emacs 总是垂直拆分。是否有任何特定于 GUD/GDB 的设置?
@Nikwin:就我而言,此解决方案将“Cx 4 b”限制为并排的两个 80 列窗口(我当前的屏幕空间只能容纳这么多)。第二次调用“Cx 4 b”不会打开一个新的垂直分割的“其他”窗口。相反,它会在“其他”当前可用的窗口上打开缓冲区。我怎样才能让它像你描述的那样表现(水平然后垂直尝试)?
这不是我所知道的熟悉的 Cx 格式。如何执行这些命令?
C
CodyChan

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

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 切换窗口的布局。


在我使用 undo-tree 时按 q 的解决方案视图不会包含缓冲区
默认水平(顶部/底部):(setq split-height-threshold 0) (setq split-width-threshold nil)
C
CoolBeans
(setq split-height-threshold 0) (setq split-width-threshold 0)

是我必须用来获得所需行为的东西(没有水平分割)


w
whunmr

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

我推荐伟大的 ToggleWindowSplit,并将密钥绑定到“Cc y”

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


transpose-frame,更优雅。
a
alexey

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

https://i.stack.imgur.com/2YPmw.png

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


J
Jason

我经常在 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")

p
poleguy

直接的答案是按 C-c 3

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