Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enforce 79 characters per line in emacs?

I wonder if encorcing 79 characters per line in Python code can be done automatically and intelligently in emacs? By intelligently, I mean it can break a long line of code at a proper place.

If not possible to do that automatically, can it be done somehow semi-automatically, or more convenient than completely manually (e.g. counting characters per line manually)?

By the way, I assume 79 characters per line is a good practice. If I am wrong, feel free to correct me.

Thanks.

like image 896
Tim Avatar asked Oct 19 '25 13:10

Tim


1 Answers

Yes, it is good practice, there are a lot of solutions out there, but they are not "automatic", most rely on simply showing you when you'e passed the limit, not making the change for you.

One "automatic" solution is the built in auto-fill-mode.

For further ideas/discussion:

Read this article on the Emacs Wiki: EightyColumnRule

It offers several non-automatic ways to help you keep your lines less then 80 chars.

whitespace-mode is built in to emacs has the capacity to highlight all lines that are longer than a certain length.

column-marker can be used to draw a line down your buffer at a certain column.

column-enforce-mode (I am the author of this) can work like whitespace-mode except it will only highlight text further than 80 columns on a line, not the entire line.

There is a lot of discussion on this topic to go through and more solutions to find:

How can I make emacs highlight lines that go over 80 chars?

Have emacs highlight characters over 80?

http://emacsredux.com/blog/2013/05/31/highlight-lines-that-exceed-a-certain-length-limit/

Here is a custom solution I have in my init that sets the current buffer's margins such that the editable space in the buffer is only 80 columns wide. I.e you know when you've passed the limit when your text goes to the next line.

(defun toggle-80-editting-columns-balanced (&optional columns)
  "Set both window margins so the edittable space is only 80 columns."
  (interactive "p")
  (let ((margins (window-margins)))
    (if (or (car margins) (cdr margins))
        (set-window-margins nil 0 0)
      (let* ((change (max (- (window-width) (or columns 80)) 0))
             (left (/ change 2))
             (right (- change left)))
        (set-window-margins nil left right)))))
like image 141
Jordon Biondo Avatar answered Oct 21 '25 02:10

Jordon Biondo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!