Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn on emacs auto-fill-mode only for code comments?

I have tried

(set (make-local-variable 'comment-auto-fill-only-comments) t)

and also

(auto-fill-mode 0)

Though amazingly, neither of those work after emacs is restarted.

I am using eschulte's emacs starter kit

Toggling it works fine with M-x auto-fill-mode.


UPDATE

Using a combination of (thanks Rémi):

(auto-fill-mode 1)
(setq comment-auto-fill-only-comments t) 

It works perfectly within programming files, where there are comments. However, in text mode, it auto-fills everywhere.

How can I turn off auto-fill-mode completely when inside text documents?

like image 336
tobeannounced Avatar asked Dec 18 '10 08:12

tobeannounced


People also ask

How to enable Auto complete in emacs?

The key to triggering the auto-completion in emacs is the Tab key. You will get a list of suggestions from the compiler. To select something from the list of suggestions, we recommend you to use C-n and C-p, but the down and up arrow keys can be used as well.

What is Auto Fill mode in Emacs?

Auto Fill mode is a minor mode which is enabled or disabled for each buffer individually. See section Minor Modes. In Auto Fill mode, lines are broken automatically at spaces when they get longer than the desired width. Line breaking and rearrangement takes place only when you type SPC or RET .

What is autofill mode?

Auto Fill mode is a buffer-local minor mode (see Minor Modes) in which lines are broken automatically when the line becomes too wide and you type SPC or RET . M-x auto-fill-mode. Enable or disable Auto Fill mode.


1 Answers

If you want Emacs to auto-fill comments you must not make comment-auto-fill-only-comments a local variable:

(setq comment-auto-fill-only-comments t)

If you want it only in some mode but not all you have to add it to the correct hook:

(add-hook 'ruby-mode-hook 
          (lambda () ((set (make-local-variable 'comment-auto-fill-only-comments) t)))

UPDATE answer

To remove auto-fill from text mode, you have to use hook:

(add-hook 'text-mode-hook 
          (lambda () (auto-fill-mode -1)))

Note that this will change also the auto-fill state in mode deriving off text-mode (latex-mode is one examples, there are a lot of other such mode)

like image 114
Rémi Avatar answered Oct 01 '22 14:10

Rémi