5

How can I visually truncate the lines of a buffer at a specific column instead at the last column of the buffer?

Is this even possible in Emacs?

If, so, is it possible to also choose at which column the line starts, so that when I have a big Emacs window (let's say with 200 columns), the text occupies only the central (80 columns)?

EDIT: Error message with debug on after trying JEanPierre answer:

Debugger entered--Lisp error: (wrong-type-argument number-or-marker-p nil)
  +(4 nil 198)
  (- (+ cur-l cur-r cur-width) width)
  (let* ((cur-width (window-width)) (cur-m (window-margins)) (cur-l (if (and cur-m (car cur-m)) (car cur-m) 0)) (cur-r (if (and cur-m (car cur-m)) (cdr cur-m) 0)) (lr (- (+ cur-l cur-r cur-width) width)) (left (/ lr 2)) (right (if (= 0 (% lr 2)) left (1+ left)))) (set-window-margins nil (max left 0) (max right 0)))
  (if (= 0 width) (set-window-margins nil 0 0) (let* ((cur-width (window-width)) (cur-m (window-margins)) (cur-l (if (and cur-m (car cur-m)) (car cur-m) 0)) (cur-r (if (and cur-m (car cur-m)) (cdr cur-m) 0)) (lr (- (+ cur-l cur-r cur-width) width)) (left (/ lr 2)) (right (if (= 0 (% lr 2)) left (1+ left)))) (set-window-margins nil (max left 0) (max right 0))))
  my/change-window-width(80)
  call-interactively(my/change-window-width record nil)
  command-execute(my/change-window-width record)
  helm-M-x(nil "my/change-window-width")
  call-interactively(helm-M-x nil nil)
  command-execute(helm-M-x)
2

You can do that by setting display margins. The following code is an attempt at that:

(defun my/change-window-width (width)
  "Adjust margins so that window is centered"
  (interactive "NWindow width: ")
  (if (= 0 width)
      (set-window-margins nil 0 0)
    (let* ((cur-width (window-width))
           (cur-m (window-margins))
           (cur-l (if (and cur-m (car cur-m)) (car cur-m) 0))
           (cur-r (if (and cur-m (cdr cur-m)) (cdr cur-m) 0))
           (lr (- (+ cur-l cur-r cur-width) width))
           (left (/ lr 2))
           (right (if (= 0 (% lr 2)) left (1+ left))))
      (set-window-margins nil (max left 0) (max right 0)))))

Giving 0 as width means remove the margins. Giving too large a width has the same effect.

  • I can't use this. Perhaps I'm doing something wrong, but I tried this: M-x my/change-window-width I get a message asking for the width and I try 80. I then get the error message: -: Wrong type argument: number-or-marker-p, nil – jbssm Mar 17 '16 at 18:41
  • @jbssm Well, does work for me on emacs 24.4.1 and 25.1.50.1. Can you M-x toggle-debug-on-error before running the command and provide the debugger backtrace? – JeanPierre Mar 17 '16 at 19:36
  • I tried that and put the output in the initial answer since it didn't fit here. From what I can understand the problem is in some variable that the code takes from the environment that has the value nil not exactly from the inputed number of columns to use. – jbssm Mar 17 '16 at 20:25
  • 2
    @jbssm Based on your backtrace, cur-r is the culprit. Indeed, I changed the car to cdr on the cur-r defining line. – JeanPierre Mar 17 '16 at 21:14
  • This now works, but the right margin and its contents is also being cut. For instance, the git status, from hl-diff is now being taken from the border of the buffer to the end of the line. But I still thank you for the help and I'll accept the answer since it looks this is the only possible way to do it. Thanks again. – jbssm Mar 18 '16 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.