Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Centre Emacs buffer within window

Tags:

emacs

I wrap all my code at 80 columns, and there are times where the Emacs window is wider than 80 columns and there is a lot of unused whitespace on the right side.

I would like to position the Emacs buffer, so all the text is displayed in the middle of the window.

This is different to centre aligning text (more akin to the whitespace on either side of the text when viewing pdfs).

I think this can be achieved by dynamically adjusting the fringe mode widths, depending on the current window size, but I'm not sure where to start. Any ideas?

like image 332
fli Avatar asked Jul 25 '14 11:07

fli


People also ask

How do I switch between buffers in Emacs?

For conveniently switching between a few buffers, use the commands C-x <LEFT> and C-x <RIGHT> . C-x <RIGHT> ( previous-buffer ) selects the previous buffer (following the order of most recent selection in the current frame), while C-x <LEFT> ( next-buffer ) moves through buffers in the reverse direction.

Can you only have one buffer open in Emacs at a time?

Each Emacs window displays one Emacs buffer at any time. A single buffer may appear in more than one window; if it does, any changes in its text are displayed in all the windows where it appears. But the windows showing the same buffer can show different parts of it, because each window has its own value of point.

How do I close a buffer window in Emacs?

And a buffer can be closed by :q . Likewise in emacs, window can be split horizontally by C-x 2 , and vertically by C-x 3 . And close all other window by C-x 1 .

How do I use buffers in Emacs?

The text you are editing in Emacs resides in an object called a buffer. Each time you visit a file, a buffer is used to hold the file's text. Each time you invoke Dired, a buffer is used to hold the directory listing. If you send a message with C-x m , a buffer is used to hold the text of the message.


Video Answer


1 Answers

As demonstrated here this is indeed possible:

(set-fringe-mode
 (/ (- (frame-pixel-width)
       (* 80 (frame-char-width)))
    2))

However, as I am testing this I seem to have more luck with using margins, at least when also resizing my frame:

(defun my-resize-margins ()
  (let ((margin-size (/ (- (frame-width) 80) 2)))
    (set-window-margins nil margin-size margin-size)))

(add-hook 'window-configuration-change-hook #'my-resize-margins)
(my-resize-margins)
like image 96
ryuslash Avatar answered Oct 04 '22 18:10

ryuslash