设置Emacs并排拆分缓冲区


90

许多Emacs功能会自动拆分屏幕。但是,它们都这样做,以使窗口彼此重叠。有什么办法可以使它们分开,从而默认情况下它们并排放置?


12
我将在这个问题中交换水平和垂直的用法-我想说的默认行为是水平拆分(拆分是屏幕上的水平线)。
Skilldrick 2010年

22
Cx 3水平运行命令split-window-horizo​​ntal,该命令提供并排窗口,因此我正在使用相同的命令。
Nikwin 2010年

@Skilldrick“垂直”和“水平”是模棱两可的,可以用不同的方式解释。他们可以描述分隔符的方向或分区的方向。我的一般倾向是同意原始问题的措辞(也就是说,我通常将“垂直拆分”解释为“拆分垂直空间”)。
jamesdlin

Answers:


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

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


8
请注意,它们之所以起作用,是因为它们如何影响split-window-preferred-function和设置为split-window-sensible的函数-如果您阅读该文档,则可以了解所设置的这些变量如何影响事物。对于那些默认情况下希望垂直分割的人,我们可以使用(setq split-width-threshold nil),它不允许系统水平分割窗口。
肯德尔·赫尔姆斯特·盖尔纳

5
在阅读了文档并进行了一些尝试之后,我将split-height-threshold设置为nil,并将split-width-threshold设置为80,以便它首先查看它是否可以水平拆分,然后再垂直尝试。仅将其垂直拆分通常会随着窗口变窄而变得难看。
Nikwin '01

这听起来很合理。但是,这不适用于emacs中的GDB / GUD集成。如果我只有一个窗口可以启动调试器,则emacs总是垂直拆分。是否有特定于GUD / GDB的设置?
mefiX 2010年

@Nikwin:就我而言,此解决方案将“ Cx 4 b”限制为并排两个80列窗口(我当前的屏幕空间只能容纳这么多)。第二次调用“ Cx 4 b”不会打开新的垂直拆分的“其他”窗口。而是在“其他”当前可用窗口上打开缓冲区。如您所描述的,我该如何使其表现(水平然后垂直尝试)?
avendael 2011年

这不是我所熟悉的Cx某种格式。如何执行这些命令?
user1552512 '10 -10-4

6

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

答:默认为垂直(左/右):

(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不cluse缓冲
阿尔珀

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

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



1

将2个变量设置为nil和0的简单答案对我不起作用,所以我写了2个简单的函数:一个只是将窗口拆分为NX垂直缓冲区,然后打开名为(例如)file.1 file.2的文件。每个文件中的.file.NX都具有相同的想法,只是在2D中(NX列的NX行用于打开文件f.1 f.2 ... f。[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)
          )
        )
      )

然后使用垂直的,我去* scratch *(C-x b *scratch* RETC-x 1),输入(grid-files-h 3 20 "file.")then C-x C-e,或者如果您想测试方形qrid的一个C-x 1,输入(grid-files-sq 2 15 3 20 "f."),然后C-x C-e您应该会看到类似 2x3网格

可能可以更好/更有效地完成此操作,但这只是一个开始,它可以完成我需要做的事情(显示一堆顺序命名的小文件)。随时进行改进或重复使用。


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")
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.