187

I have emacs split horizontally - on top I'm editing Perl code, the bottom is the shell. By default emacs makes the two windows equal in size, but I'd like the shell buffer smaller (maybe half the size?). I was wondering how I could do that.

Drew Gaynor
  • 8,292
  • 5
  • 40
  • 53
Steve
  • 1,873
  • 2
  • 12
  • 4

7 Answers7

315

With the mouse, you can drag the window sizes around.

Click anywhere on the mode line that is not otherwise 'active' (the buffer name is safe, or any unused area to the right hand side), and you can drag up or down.

Side-to-side dragging requires a very precise click on the spot where the two mode lines join.

C-x - (shrink-window-if-larger-than-buffer) will shrink a window to fit its content.

C-x + (balance-windows) will make windows the same heights and widths.

C-x ^ (enlarge-window) increases the height by 1 line, or the prefix arg value. A negative arg shrinks the window. e.g. C-- C-1 C-6 C-x ^ shrinks by 16 rows, as does C-u - 1 6 C-x ^.

(There is no default binding for shrink-window.)

C-x } (enlarge-window-horizontally) does likewise, horizontally.
C-x { (shrink-window-horizontally) is also bound by default.

Following one of these commands with repeat (C-x z to initiate, and just z for continued repetition) makes it pretty easy to get to the exact size you want.

If you regularly want to do this with a specific value, you could record a keyboard macro to do it, or use something like
(global-set-key (kbd "C-c v") (kbd "C-u - 1 6 C-x ^"))

Or this:
(global-set-key (kbd "C-c v") (kbd "C-x o C-x 2 C-x 0 C-u - 1 C-x o"))

Which is a smidgen hacky, so this would be better:

(defun halve-other-window-height ()
  "Expand current window to use half of the other window's lines."
  (interactive)
  (enlarge-window (/ (window-height (next-window)) 2)))

(global-set-key (kbd "C-c v") 'halve-other-window-height)

Tangentially, I also love winner-mode which lets you repeatedly 'undo' any changes to window configurations with C-c left (whether the change is the size/number/arrangement of the windows, or just which buffer is displayed). C-c right returns you to the most recent configuration. Set it globally with (winner-mode 1)

phils
  • 71,335
  • 11
  • 153
  • 198
  • 1
    +1: thanks for the 'undo' winner-mode tip. btw, in my case a clicking on the buffer name opens the next buffer. But it is easy to see when you can resize looking at the mouse cursor shape. – jfs Mar 20 '12 at 05:28
38

I put these in my .emacs:

(global-set-key (kbd "<C-up>") 'shrink-window)
(global-set-key (kbd "<C-down>") 'enlarge-window)
(global-set-key (kbd "<C-left>") 'shrink-window-horizontally)
(global-set-key (kbd "<C-right>") 'enlarge-window-horizontally)
wilbeibi
  • 3,403
  • 4
  • 25
  • 44
  • 1
    First two does not work for me in Emacs 24.3.1 on Mac OSX 10.9.1 – Oskar Persson Jan 14 '14 at 15:55
  • 1
    @OskarPersson It might be the confliction with global shortcuts. Find it out in preference or remap control to caps lock(my way), it should work then. – wilbeibi Jan 14 '14 at 21:34
  • I had the same problem and found out that C-left and C-right conflicted with Mission Control shortcuts in OS X System Preferences, as described by @Wilbeibi. – Doug Richardson Aug 27 '14 at 15:36
  • 1
    These keys are already defined, first unset the keys with `(global-set-key (kbd "") nil)` before set it, but would be better use `M-left` instead – Mauricio Cortazar Dec 07 '17 at 21:04
  • @MauricioCortazar I haven't use Emacs for years. This answer may not apply now. Feel free to update it. – wilbeibi Dec 08 '17 at 00:34
  • 1
    I like this, but using `M-k` (enlarge), `M-j` (shrink), `M-l` (enlarge horizontally) and `M-l` (shrink horizontally). – goetz Aug 09 '18 at 00:13
17

let's try to use emacs help document.

C-h a

Then type "enlarge" or "window"

You will find what you want.

Enjoy!

maoyang
  • 1,067
  • 1
  • 11
  • 11
  • It's possible one is working in an environment in which `C-h` isn't available. What then? – Derrell Durrett May 22 '17 at 18:53
  • 1
    If you've clobbered the standard help map binding, then you should surely be aware of what you've clobbered it *with*. (Failing that, try `` which is the *other* standard help map binding.) – phils Sep 20 '17 at 04:23
7

C-x ^ takes positive and negative numerical arguments. In particular, if you would like to shrink the window where your cursor currently is by four lines, you can press C-u -4 C-x ^.

justinpc
  • 797
  • 6
  • 19
7

C-x o to the window whose size you want expanded. From there, C-x ^ to expand it.

vpit3833
  • 7,817
  • 2
  • 25
  • 25
  • 3
    In case bindings are different (mine are), the command is `enlarge-window`. An additional tip is to use a prefix arg to control the amount of expansion. – Joseph Gay Feb 14 '11 at 01:17
5

This is not the exact answer you're looking for, but I stumbled upon this question when looking out for the functionality in spacemacs, so I'll write my answer just for completeness' sake.

The option is to use <SPC> + w + . (it works in vanilla spacemacs)

This opens up a transient window where one can use []{} to resize at a granular level.

There's no need for C-x or Mouse...

Window manipulation transient state

Yesh
  • 976
  • 12
  • 15
0

I got the same question. Here is my solution.
First I define a new function:

(defun buffer-resize ()
  (delete-other-windows)
  (split-window-vertically (floor (* 0.68 (window-height))))
  (other-window 1)
  (switch-to-buffer buf)
  (other-window 1))

For example, I want to run-scheme in a buffer, So I rewrite it.
And here is the definition, with the function defined earlier:

(defun run-scheme-here ()
  "Run a new scheme process at the directory of the current buffer.
   If a process is already running, switch to its buffer."
  (interactive)
  (let* ((proc (format "scheme: %s" default-directory))
         (buf (format "*%s*" proc)))
    (unless (comint-check-proc buf)
      (let ((cmd (split-string scheme-program-name)))
        (set-buffer
         (apply 'make-comint-in-buffer proc buf (car cmd) nil (cdr cmd)))
        (inferior-scheme-mode)
        (buffer-resize)))
    (pop-to-buffer buf)))

So now when I enter: M-x run-scheme-here, the buffer is resized!
And here is my config file, hoping this will help. https://github.com/judevc/dotfiles/blob/master/.emacs.d/scheme-conf.el